Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix actions convertion and handle disabled actions #5

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading