Skip to content

Commit

Permalink
fix definition interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
dpfaffenbauer committed May 27, 2024
1 parent d7abbef commit 76b2847
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 53 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
"require": {
"php": ">=8.1",
"ext-json": "*",
"openspout/openspout": "^4.23",
"coreshop/pimcore-bundle": "^4.0",
"coreshop/resource-bundle": "^4.0",
"coreshop/rule-bundle": "^4.0",
"jms/serializer": "^3.17",
"league/csv": "^9.7",
"nyholm/psr7": "^1.5",
"pimcore/admin-ui-classic-bundle": "^1.0",
"openspout/openspout": "^4.23",
"pimcore/admin-ui-classic-bundle": "^1.4",
"pimcore/pimcore": "^11.0",
"psr/http-client-implementation": "^1.0",
"psr/http-factory-implementation": "^1.0",
Expand Down
9 changes: 2 additions & 7 deletions src/DataDefinitionsBundle/Command/ImportAsyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,14 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output)
{
$eventDispatcher = $this->eventDispatcher;

$params = json_decode($input->getOption('params'), true);

if (!isset($params['userId'])) {
$params['userId'] = 0;
}

try {
$definition = $this->repository->find($input->getOption('definition'));
} catch (InvalidArgumentException $e) {
$definition = $this->repository->findByName($input->getOption('definition'));
}
$inputDefinition = $input->getOption('definition');
$definition = $this->repository->findByName((string)$inputDefinition);

if (!$definition instanceof ImportDefinitionInterface) {
throw new Exception('Import Definition not found');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use CoreShop\Bundle\ResourceBundle\Controller\ResourceController;
use CoreShop\Component\Resource\Model\ResourceInterface;
use Instride\Bundle\DataDefinitionsBundle\Repository\DefinitionRepository;
use Pimcore\Model\DataObject;
use Pimcore\Tool;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand All @@ -28,6 +29,9 @@
use Instride\Bundle\DataDefinitionsBundle\Model\ExportDefinitionInterface;
use Instride\Bundle\DataDefinitionsBundle\Model\ExportMapping\FromColumn;

/**
* @property DefinitionRepository $repository
*/
abstract class AbstractDefinitionController extends ResourceController
{
public function getAction(Request $request): JsonResponse
Expand All @@ -38,4 +42,15 @@ public function getAction(Request $request): JsonResponse

return $this->viewHandler->handle(['data' => $resources, 'success' => true], ['group' => 'Detailed']);
}

protected function findOr404(int|string $id): ResourceInterface
{
$model = $this->repository->findByName((string)$id);

if (null === $model || !$model instanceof ResourceInterface) {
throw new NotFoundHttpException(sprintf('The "%s" has not been found', $id));
}

return $model;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Instride\Bundle\DataDefinitionsBundle\Controller;

use CoreShop\Bundle\ResourceBundle\Controller\ResourceController;
use Instride\Bundle\DataDefinitionsBundle\Repository\DefinitionRepository;
use Pimcore\Model\DataObject;
use Pimcore\Tool;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand All @@ -27,6 +28,9 @@
use Instride\Bundle\DataDefinitionsBundle\Model\ExportDefinitionInterface;
use Instride\Bundle\DataDefinitionsBundle\Model\ExportMapping\FromColumn;

/**
* @property DefinitionRepository $repository
*/
class ExportDefinitionController extends AbstractDefinitionController
{
public function getConfigAction(): JsonResponse
Expand Down Expand Up @@ -56,10 +60,10 @@ public function getConfigAction(): JsonResponse

public function exportAction(Request $request): Response
{
$id = (int)$request->get('id');
$id = $request->get('id');

if ($id) {
$definition = $this->repository->find($id);
$definition = $this->repository->findByName($id);

if ($definition instanceof ExportDefinitionInterface) {

Expand Down Expand Up @@ -88,7 +92,7 @@ public function exportAction(Request $request): Response
public function importAction(Request $request): JsonResponse
{
$id = (int)$request->get('id');
$definition = $this->repository->find($id);
$definition = $this->repository->findByName($id);

if ($id && $definition instanceof ExportDefinitionInterface && $request->files->has('Filedata')) {
$uploadedFile = $request->files->get('Filedata');
Expand Down Expand Up @@ -116,8 +120,8 @@ public function importAction(Request $request): JsonResponse

public function duplicateAction(Request $request): JsonResponse
{
$id = (int)$request->get('id');
$definition = $this->repository->find($id);
$id = $request->get('id');
$definition = $this->repository->findByName($id);
$name = (string)$request->get('name');

if ($definition instanceof ExportDefinitionInterface && $name) {
Expand All @@ -137,7 +141,7 @@ public function duplicateAction(Request $request): JsonResponse
public function getColumnsAction(Request $request): JsonResponse
{
$id = $request->get('id');
$definition = $this->repository->find($id);
$definition = $this->repository->findByName($id);

if (!$definition instanceof ExportDefinitionInterface || !$definition->getClass()) {
return $this->viewHandler->handle(['success' => false]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use CoreShop\Component\Registry\ServiceRegistryInterface;
use Exception;
use Instride\Bundle\DataDefinitionsBundle\Repository\DefinitionRepository;
use Pimcore\Model\DataObject;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand All @@ -32,6 +33,9 @@
use Instride\Bundle\DataDefinitionsBundle\Service\FieldSelection;
use function is_array;

/**
* @property DefinitionRepository $repository
*/
class ImportDefinitionController extends AbstractDefinitionController
{
public function getConfigAction(): JsonResponse
Expand Down Expand Up @@ -66,7 +70,7 @@ public function getConfigAction(): JsonResponse
public function testDataAction(Request $request): JsonResponse
{
$id = $request->get('id');
$definition = $this->repository->find($id);
$definition = $this->repository->findByName($id);

if ($definition instanceof ImportDefinitionInterface) {
try {
Expand All @@ -88,7 +92,7 @@ public function testDataAction(Request $request): JsonResponse
public function getColumnsAction(Request $request): JsonResponse
{
$id = $request->get('id');
$definition = $this->repository->find($id);
$definition = $this->repository->findByName($id);

if ($definition instanceof ImportDefinitionInterface && $definition->getClass()) {
$customFromColumn = new FromColumn();
Expand Down Expand Up @@ -189,10 +193,10 @@ public function getColumnsAction(Request $request): JsonResponse

public function exportAction(Request $request): Response
{
$id = (int)$request->get('id');
$id = $request->get('id');

if ($id) {
$definition = $this->repository->find($id);
$definition = $this->repository->findByName($id);

if ($definition instanceof ImportDefinitionInterface) {

Expand Down Expand Up @@ -220,8 +224,8 @@ public function exportAction(Request $request): Response

public function importAction(Request $request): JsonResponse
{
$id = (int)$request->get('id');
$definition = $this->repository->find($id);
$id = $request->get('id');
$definition = $this->repository->findByName($id);

if ($id && $definition instanceof ImportDefinitionInterface && $request->files->has('Filedata')) {
$uploadedFile = $request->files->get('Filedata');
Expand Down Expand Up @@ -249,8 +253,8 @@ public function importAction(Request $request): JsonResponse

public function duplicateAction(Request $request): JsonResponse
{
$id = (int)$request->get('id');
$definition = $this->repository->find($id);
$id = $request->get('id');
$definition = $this->repository->findByName($id);
$name = (string)$request->get('name');

if ($definition instanceof ImportDefinitionInterface && $name) {
Expand Down
3 changes: 1 addition & 2 deletions src/DataDefinitionsBundle/DataDefinitionsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Pimcore\Bundle\AdminBundle\PimcoreAdminBundle;
use Pimcore\Bundle\SimpleBackendSearchBundle\PimcoreSimpleBackendSearchBundle;
use Pimcore\Extension\Bundle\Installer\InstallerInterface;
use Pimcore\Extension\Bundle\PimcoreBundleInterface;
use Pimcore\HttpKernel\BundleCollection\BundleCollection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Instride\Bundle\DataDefinitionsBundle\DependencyInjection\Compiler\CleanerRegistryCompilerPass;
Expand All @@ -50,7 +49,7 @@ public static function registerDependentBundles(BundleCollection $collection): v

$collection->addBundles([
new PimcoreAdminBundle(),
new PimcoreSimpleBackendSearchBundle()
new PimcoreSimpleBackendSearchBundle(),
]);

$collection->addBundles([
Expand Down
14 changes: 6 additions & 8 deletions src/DataDefinitionsBundle/Form/Type/DefinitionChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@

namespace Instride\Bundle\DataDefinitionsBundle\Form\Type;

use CoreShop\Component\Resource\Repository\RepositoryInterface;
use Instride\Bundle\DataDefinitionsBundle\Model\DataDefinitionInterface;
use Instride\Bundle\DataDefinitionsBundle\Repository\DefinitionRepository;
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Instride\Bundle\DataDefinitionsBundle\Model\DataDefinitionInterface;

final class DefinitionChoiceType extends AbstractType
{
private RepositoryInterface $definitionRepository;

public function __construct(RepositoryInterface $definitionRepository)
{
$this->definitionRepository = $definitionRepository;
public function __construct(
private readonly DefinitionRepository $definitionRepository
) {
}

public function buildForm(FormBuilderInterface $builder, array $options): void
Expand All @@ -52,7 +50,7 @@ public function configureOptions(OptionsResolver $resolver): void
}, $this->definitionRepository->findAll());
},
'choice_label' => function ($val) {
$def = $this->definitionRepository->find($val);
$def = $this->definitionRepository->findByName($val);

return $def !== null ? $def->getName() : null;
},
Expand Down
20 changes: 9 additions & 11 deletions src/DataDefinitionsBundle/Interpreter/DefinitionInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@

namespace Instride\Bundle\DataDefinitionsBundle\Interpreter;

use CoreShop\Component\Resource\Repository\RepositoryInterface;
use Pimcore\Model\DataObject;
use Instride\Bundle\DataDefinitionsBundle\Context\InterpreterContextInterface;
use Instride\Bundle\DataDefinitionsBundle\Importer\ImporterInterface;
use Instride\Bundle\DataDefinitionsBundle\Model\ImportDefinitionInterface;
use Instride\Bundle\DataDefinitionsBundle\Repository\DefinitionRepository;
use Pimcore\Model\DataObject;

class DefinitionInterpreter implements InterpreterInterface
{
private RepositoryInterface $definitionRepository;
private ImporterInterface $importer;

public function __construct(RepositoryInterface $definitionRepository, ImporterInterface $importer)
{
$this->definitionRepository = $definitionRepository;
$this->importer = $importer;
public function __construct(
private readonly DefinitionRepository $definitionRepository,
private readonly ImporterInterface $importer
) {
}

public function interpret(InterpreterContextInterface $context): mixed {
$subDefinition = $this->definitionRepository->find($context->getConfiguration()['definition']);
public function interpret(InterpreterContextInterface $context): mixed
{
$subDefinition = $this->definitionRepository->findByName($context->getConfiguration()['definition']);

if (!$subDefinition instanceof ImportDefinitionInterface) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/DataDefinitionsBundle/Model/ImportDefinition/Dao.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function getByName(string $id = null): void
$this->model->setName($data['id']);
} else {
throw new Model\Exception\NotFoundException(sprintf(
'Thumbnail with ID "%s" does not exist.',
'Import Definition with ID "%s" does not exist.',
$this->model->getName()
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@

namespace Instride\Bundle\DataDefinitionsBundle\ProcessManager;

use CoreShop\Component\Resource\Repository\RepositoryInterface;
use Instride\Bundle\DataDefinitionsBundle\Repository\DefinitionRepository;
use ProcessManagerBundle\Model\ExecutableInterface;
use ProcessManagerBundle\Process\ProcessStartupFormResolverInterface;
use Instride\Bundle\DataDefinitionsBundle\Form\Type\ProcessManager\ExportDefinitionObjectStartupForm;
use Instride\Bundle\DataDefinitionsBundle\Model\ExportDefinitionInterface;

final class ExportDefinitionStartupFormResolver implements ProcessStartupFormResolverInterface
{
private $definitionRepository;

public function __construct(RepositoryInterface $definitionRepository)
public function __construct(
private readonly DefinitionRepository $definitionRepository
)
{
$this->definitionRepository = $definitionRepository;
}

public function supports(ExecutableInterface $executable): bool
Expand All @@ -37,7 +36,7 @@ public function supports(ExecutableInterface $executable): bool
return false;
}

$definition = $this->definitionRepository->find($executable->getSettings()['definition']);
$definition = $this->definitionRepository->findByName($executable->getSettings()['definition']);

if (!$definition instanceof ExportDefinitionInterface) {
return false;
Expand Down
5 changes: 3 additions & 2 deletions src/DataDefinitionsBundle/Provider/CsvProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ public function getColumns(array $configuration): array
$enclosure = $configuration['enclosure'];

$returnHeaders = [];
$rows = str_getcsv($csvHeaders ?: $csvExample, "\n"); //parse the rows
$csv = $csvHeaders ?: $csvExample;
$rows = str_getcsv($csv ?: '', "\n"); //parse the rows

if (count($rows) > 0) {
$headerRow = $rows[0];

$headers = str_getcsv($headerRow, $delimiter ?? ',', $enclosure ?: chr(8));
$headers = str_getcsv($headerRow ?: '', $delimiter ?? ',', $enclosure ?: chr(8));

if (count($headers) > 0) {
//First line are the headers
Expand Down
5 changes: 5 additions & 0 deletions src/DataDefinitionsBundle/Repository/DefinitionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

class DefinitionRepository extends PimcoreDaoRepository
{
public function find($id)
{
return $this->findByName($id);
}

public function findByName(string $name): ?DataDefinitionInterface
{
$class = $this->metadata->getClass('model');
Expand Down

0 comments on commit 76b2847

Please sign in to comment.