Skip to content

Commit

Permalink
make generic (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Jan 16, 2024
1 parent f77daa6 commit 53db50c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rector\Symfony\Tests\Rector\Closure\StringExtensionToConfigBuilderRector\Fixture;

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {

$containerConfigurator->extension('security', [
'entity' => [
'class' => 'App\SomeClass',
'property' => 'apiKey',
],
]);
};

?>
-----
<?php

namespace Rector\Symfony\Tests\Rector\Closure\StringExtensionToConfigBuilderRector\Fixture;

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (\Symfony\Config\SecurityConfig $securityConfig): void {

$securityConfig->entity()->class('App\SomeClass')->property('apiKey');
};

?>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Rector\Symfony\Configs\Enum\SecurityConfigKey;
use Rector\Symfony\NodeAnalyzer\SymfonyClosureExtensionMatcher;
use Rector\Symfony\NodeAnalyzer\SymfonyPhpClosureDetector;
use Rector\Symfony\Utils\StringUtils;
use Rector\Symfony\ValueObject\ExtensionKeyAndConfiguration;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -162,10 +163,13 @@ private function createMethodCallStmts(Array_ $configurationArray, Variable $con
$methodCallName = $this->createCamelCaseFromUnderscored($key);
}

if ($key === SecurityConfigKey::ACCESS_DECISION_MANAGER) {
if (in_array($key, [SecurityConfigKey::ACCESS_DECISION_MANAGER, 'entity'])) {
$mainMethodName = StringUtils::underscoreToCamelCase($key);

$accessDecisionManagerMethodCalls = $this->securityAccessDecisionManagerConfigArrayHandler->handle(
$configurationArray,
$configVariable
$configVariable,
$mainMethodName
);

if ($accessDecisionManagerMethodCalls !== []) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class SecurityAccessDecisionManagerConfigArrayHandler
/**
* @return array<Expression<MethodCall>>
*/
public function handle(Array_ $array, Variable $configVariable): array
public function handle(Array_ $array, Variable $configVariable, string $mainMethodName): array
{
if (! $array->items[0] instanceof ArrayItem) {
return [];
Expand All @@ -31,7 +31,7 @@ public function handle(Array_ $array, Variable $configVariable): array
}

// build accessControl() method call here
$accessDecisionManagerMethodCall = new MethodCall($configVariable, 'accessDecisionManager');
$accessDecisionManagerMethodCall = new MethodCall($configVariable, $mainMethodName);

foreach ($nestedArray->items as $nestedArrayItem) {
if (! $nestedArrayItem instanceof ArrayItem) {
Expand Down
16 changes: 16 additions & 0 deletions src/Utils/StringUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Utils;

final class StringUtils
{
public static function underscoreToCamelCase(string $value): string
{
$value = ucwords(str_replace(['-', '_'], ' ', $value));
$singleWord = str_replace(' ', '', $value);

return lcfirst($singleWord);
}
}

0 comments on commit 53db50c

Please sign in to comment.