Skip to content

Commit

Permalink
minor #31828 [TwigBundle] mark TemplateIterator as internal (Tobion)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

[TwigBundle] mark TemplateIterator as internal

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets |
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

This class is an implementation detail and should not be relied on as an extension point. This is also why TemplateCacheWarmer does not typhint this class but iterable. By making it internal we can remove the rootDir argument in #31823

Commits
-------

9b46c17 [TwigBundle] mark TemplateIterator as internal
  • Loading branch information
fabpot committed Jun 4, 2019
2 parents 1768c93 + 9b46c17 commit 957a0b8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Bundle/TwigBundle/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.4.0
-----

* marked the `TemplateIterator` as `internal`

4.2.0
-----

Expand Down
Expand Up @@ -28,7 +28,7 @@ class TemplateCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInte
private $twig;
private $iterator;

public function __construct(ContainerInterface $container, \Traversable $iterator)
public function __construct(ContainerInterface $container, iterable $iterator)
{
// As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
$this->container = $container;
Expand Down
40 changes: 24 additions & 16 deletions src/Symfony/Bundle/TwigBundle/TemplateIterator.php
Expand Up @@ -18,6 +18,8 @@
* Iterator for all templates in bundles and in the application Resources directory.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @internal since Symfony 4.4
*/
class TemplateIterator implements \IteratorAggregate
{
Expand All @@ -31,7 +33,7 @@ class TemplateIterator implements \IteratorAggregate
* @param KernelInterface $kernel A KernelInterface instance
* @param string $rootDir The directory where global templates can be stored
* @param array $paths Additional Twig paths to warm
* @param string $defaultPath The directory where global templates can be stored
* @param string|null $defaultPath The directory where global templates can be stored
*/
public function __construct(KernelInterface $kernel, string $rootDir, array $paths = [], string $defaultPath = null)
{
Expand All @@ -50,40 +52,46 @@ public function getIterator()
return $this->templates;
}

$this->templates = array_merge(
$this->findTemplatesInDirectory($this->rootDir.'/Resources/views'),
$this->findTemplatesInDirectory($this->defaultPath, null, ['bundles'])
);
$templates = $this->findTemplatesInDirectory($this->rootDir.'/Resources/views');

if (null !== $this->defaultPath) {
$templates = array_merge(
$templates,
$this->findTemplatesInDirectory($this->defaultPath, null, ['bundles'])
);
}
foreach ($this->kernel->getBundles() as $bundle) {
$name = $bundle->getName();
if ('Bundle' === substr($name, -6)) {
$name = substr($name, 0, -6);
}

$this->templates = array_merge(
$this->templates,
$templates = array_merge(
$templates,
$this->findTemplatesInDirectory($bundle->getPath().'/Resources/views', $name),
$this->findTemplatesInDirectory($this->rootDir.'/Resources/'.$bundle->getName().'/views', $name),
$this->findTemplatesInDirectory($this->defaultPath.'/bundles/'.$bundle->getName(), $name)
$this->findTemplatesInDirectory($this->rootDir.'/Resources/'.$bundle->getName().'/views', $name)
);
if (null !== $this->defaultPath) {
$templates = array_merge(
$templates,
$this->findTemplatesInDirectory($this->defaultPath.'/bundles/'.$bundle->getName(), $name)
);
}
}

foreach ($this->paths as $dir => $namespace) {
$this->templates = array_merge($this->templates, $this->findTemplatesInDirectory($dir, $namespace));
$templates = array_merge($templates, $this->findTemplatesInDirectory($dir, $namespace));
}

return $this->templates = new \ArrayIterator(array_unique($this->templates));
return $this->templates = new \ArrayIterator(array_unique($templates));
}

/**
* Find templates in the given directory.
*
* @param string $dir The directory where to look for templates
* @param string|null $namespace The template namespace
*
* @return array
* @return string[]
*/
private function findTemplatesInDirectory($dir, $namespace = null, array $excludeDirs = [])
private function findTemplatesInDirectory(string $dir, string $namespace = null, array $excludeDirs = []): array
{
if (!is_dir($dir)) {
return [];
Expand Down

0 comments on commit 957a0b8

Please sign in to comment.