Skip to content

Commit

Permalink
возможность указать дополнительные директории бандлов (#25)
Browse files Browse the repository at this point in the history
Co-authored-by: Андрей Артаханов <[email protected]>
  • Loading branch information
azgalot and Андрей Артаханов authored Dec 23, 2024
1 parent a88d5f0 commit ef7bb93
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
10 changes: 9 additions & 1 deletion Builder/EnvironmentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function setOptions(array $options): void
'cache_filename' => 'IntaroTwigSandboxPolicy',
'bundles' => [],
'debug' => false,
'additional_paths' => [],
];

// check option names and live merge, if errors are encountered Exception will be thrown
Expand Down Expand Up @@ -147,11 +148,18 @@ public function getPolicyRules(): SecurityPolicyRules

foreach ($this->options['bundles'] as $bundle) {
$refl = new \ReflectionClass($bundle);

$dir = dirname((string) $refl->getFileName()) . '/Entity';
if (file_exists($dir) && is_dir($dir)) {
$rules->merge($this->loader->load($dir));
}

$additionalPaths = $this->options['additional_paths'][$refl->getShortName()] ?? [];
foreach ($additionalPaths as $path) {
$additionalDir = dirname((string) $refl->getFileName()) . '/' . $path;
if (file_exists($additionalDir) && is_dir($additionalDir)) {
$rules->merge($this->loader->load($additionalDir));
}
}
}

$cache->write($this->dumper->dump($rules), $rules->getResources());
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,20 @@ class TwigSandboxPass implements CompilerPassInterface
}
```

### Other parameters

You can specify additional directories for each bundle to scan (using parameter `intaro.twig_sandbox.additional_paths`).

Example:
```yml
# app/config/config.yml
parameters:
intaro.twig_sandbox.additional_paths:
AcmeDemoBundle:
- 'Model'
```

## Development ##

### Run tests ###
Expand Down
6 changes: 4 additions & 2 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ parameters:
- 'object'
- 'string'

intaro.twig_sandbox.additional_paths: []

services:
_defaults:
public: false
Expand Down Expand Up @@ -106,10 +108,10 @@ services:
cache_filename: "%intaro.twig_sandbox.cache.file_name%"
bundles: "%kernel.bundles%"
debug: "%kernel.debug%"

additional_paths: "%intaro.twig_sandbox.additional_paths%"

Intaro\TwigSandboxBundle\CacheWarmer\TwigSandboxCacheWarmer:
tags:
tags:
- { name: kernel.cache_warmer }

Intaro\TwigSandboxBundle\Validator\Constraints\TwigSandboxValidator:
Expand Down
6 changes: 3 additions & 3 deletions tests/BundleInitializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpKernel\KernelInterface;
use Twig\Sandbox\SecurityNotAllowedFilterError;
use Twig\Sandbox\SecurityNotAllowedMethodError;
use Twig\Sandbox\SecurityNotAllowedPropertyError;

class BundleInitializationTest extends KernelTestCase
{
Expand Down Expand Up @@ -76,8 +76,8 @@ public function testRenderWithFilter(): void

public function testRenderError(): void
{
$this->expectException(SecurityNotAllowedMethodError::class);
$this->expectExceptionMessageMatches('/Calling "getquantity" method on a ".*Product" object is not allowed in/');
$this->expectException(SecurityNotAllowedPropertyError::class);
$this->expectExceptionMessageMatches('/Calling "quantity" property on a ".*Product" object is not allowed in/');

self::bootKernel();
$container = property_exists(__CLASS__, 'container') ? self::$container : self::getContainer();
Expand Down
1 change: 1 addition & 0 deletions tests/SimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private function createTwigEnv()
'cache_filename' => 'IntaroTwigSandboxPolicy',
'bundles' => ['Intaro\TwigSandboxBundle\Tests\fixtures\FixtureBundle'], // %kernel.bundles%
'debug' => false, // %kernel.debug%
'additional_paths' => [],
]
);

Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/empty-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ parameters:
intaro.twig_sandbox.policy_filters:
- 'escape'
intaro.twig_sandbox.policy_functions: []
intaro.twig_sandbox.additional_paths: []

0 comments on commit ef7bb93

Please sign in to comment.