Skip to content

Commit

Permalink
Merge pull request #5 from JBDevLabs/fix_action_convert
Browse files Browse the repository at this point in the history
fix actions convertion and handle disabled actions
  • Loading branch information
mamazu authored Jan 5, 2024
2 parents 71c4e84 + 45cc63b commit 6b768ec
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
8 changes: 7 additions & 1 deletion src/ClassConfigConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public function makeQuiet(): void

public function convert(string $fileName, string $outputDirectory): void
{
$outputDirectoryResolved = realpath($outputDirectory);
if ($outputDirectoryResolved === false) {
throw new InvalidArgumentException('The output directory is invalid: '.$outputDirectory);
}
$outputDirectory = $outputDirectoryResolved;

$allGrids = Yaml::parse(file_get_contents($fileName))['sylius_grid']['grids'] ?? [];

if (!is_array($allGrids)) {
Expand All @@ -96,7 +102,7 @@ public function convert(string $fileName, string $outputDirectory): void
$new_content = $this->codeOutputter->printCode($phpCode);

$newFileName = $className . '.php';
$newFilePath = realpath($outputDirectory . DIRECTORY_SEPARATOR . $newFileName);
$newFilePath = $outputDirectory . DIRECTORY_SEPARATOR . $newFileName;
if ($this->verbose) {
echo "==============================$newFilePath================" . PHP_EOL;
echo $new_content;
Expand Down
60 changes: 37 additions & 23 deletions src/GridBuilderCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,38 @@ public function getGridBuilderBody(Expr $gridBuilder, array $gridConfiguration):

// Handle actions
if (array_key_exists('actions', $gridConfiguration)) {
if ($gridConfiguration['actions'] instanceof \iterable) {
foreach ($gridConfiguration['actions'] as $type => $configuredTypes) {
$actions = $gridConfiguration['actions'];
if (is_array($actions) || $actions instanceof \iterable) {
foreach ($actions as $type => $configuredTypes) {
$mappings = [
'main' => 'MainActionGroup',
'item' => 'ItemActionGroup',
'subitem' => 'SubItemActionGroup',
'bulk' => 'BulkActionGroup',
];

[$add, $remove] = $this->convertActionsToFunctionParameters($configuredTypes);
$gridBuilder = new MethodCall(
$gridBuilder,
'addActionGroup',
[
new Arg(new Node\Expr\StaticCall(
new Name($mappings[$type]),
'create',
$this->convertActionsToFunctionParameters($configuredTypes)
$add
)),
]
);
foreach ($remove as $item) {
$gridBuilder = new MethodCall(
$gridBuilder,
'removeAction',
[
new Arg(new Node\Scalar\String_($item)),
new Arg(new Node\Scalar\String_($type)),
]
);
}
}
}
unset($gridConfiguration['actions']);
Expand All @@ -111,41 +123,43 @@ public function getGridBuilderBody(Expr $gridBuilder, array $gridConfiguration):
/** * @return array<Node\Expr> */
public function convertActionsToFunctionParameters(array $actions): array
{
$handleCustomGrid = function (string $actionName, array $configuration): Node {
$field = new Node\Expr\StaticCall(new Name('Action'), 'create', [
$this->convertValue($actionName),
$this->convertValue($configuration['type'])
]);
$this->convertToFunctionCall($field, $configuration, 'label');
$this->convertToFunctionCall($field, $configuration, 'icon');
$this->convertToFunctionCall($field, $configuration, 'enabled');
$this->convertToFunctionCall($field, $configuration, 'position');
$this->convertToFunctionCall($field, $configuration, 'options');

return $field;
};

$removedField = [];
$field = [];
foreach ($actions as $actionName => $actionConfiguration) {
if (($actionConfiguration['enabled'] ?? true) === false) {
$removedField[] = $actionName;
continue;
}
switch ($actionConfiguration['type']) {
case 'create':
$field[] = new Node\Expr\StaticCall(new Name('CreateAction'), 'create');
$currentField = new Node\Expr\StaticCall(new Name('CreateAction'), 'create');
break;
case 'show':
$field[] = new Node\Expr\StaticCall(new Name('ShowAction'), 'create');
$currentField = new Node\Expr\StaticCall(new Name('ShowAction'), 'create');
break;
case 'delete':
$field[] = new Node\Expr\StaticCall(new Name('DeleteAction'), 'create');
$currentField = new Node\Expr\StaticCall(new Name('DeleteAction'), 'create');
break;
case 'update':
$field[] = new Node\Expr\StaticCall(new Name('UpdateAction'), 'create');
$currentField = new Node\Expr\StaticCall(new Name('UpdateAction'), 'create');
break;
default:
$field[] = $handleCustomGrid($actionName, $actionConfiguration);
$currentField = new Node\Expr\StaticCall(new Name('Action'), 'create', [
$this->convertValue($actionName),
$this->convertValue($actionConfiguration['type'])
]);
}

$this->convertToFunctionCall($currentField, $actionConfiguration, 'label');
$this->convertToFunctionCall($currentField, $actionConfiguration, 'icon');
$this->convertToFunctionCall($currentField, $actionConfiguration, 'enabled');
$this->convertToFunctionCall($currentField, $actionConfiguration, 'position');
$this->convertToFunctionCall($currentField, $actionConfiguration, 'options');

$field[]=$currentField;
}

return $field;
return [$field, $removedField];
}

private function handleDriver(Expr $gridBuilder, array $driverConfiguration): Expr
Expand Down

0 comments on commit 6b768ec

Please sign in to comment.