<commit_before><?php

/*
 * This file is part of the Integrated package.
 *
 * (c) e-Active B.V. <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Integrated\Bundle\PageBundle\Locator;

use Integrated\Bundle\ThemeBundle\Templating\ThemeManager;
use Symfony\Component\Finder\Finder;

/**
 * @author <NAME> <<EMAIL>>
 */
class LayoutLocator
{
    /**
     * @var ThemeManager
     */
    protected $themeManager;

    /**
     * @var array
     */
    private $layouts;

    /**
     * @param ThemeManager $themeManager
     */
    public function __construct(ThemeManager $themeManager)
    {
        $this->themeManager = $themeManager;
    }

    /**
     * @param string $theme
     *
     * @return array
     */
    public function getLayouts($theme, $directory = null)
    {
        if (null === $this->layouts) {
            $this->layouts = [];
            foreach ($this->themeManager->getThemes() as $id => $theme2) {
                if ($theme === $id) {
                    foreach ($theme2->getPaths() as $resource) {
                        $path = $this->themeManager->locateResource($resource).$directory;
                        if (is_dir($path)) {
                            $finder = new Finder();
                            $finder->files()->in($path)->depth(0)->name('*.html.twig');

                            /** @var \Symfony\Component\Finder\SplFileInfo $file */
                            foreach ($finder as $file) {
                                $this->layouts[] = $file->getRelativePathname();
                            }
                        }
                    }
                }<commit_msg>Allow fallback and default theme when finding a layout<commit_after><?php

/*
 * This file is part of the Integrated package.
 *
 * (c) e-Active B.V. <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Integrated\Bundle\PageBundle\Locator;

use Integrated\Bundle\ThemeBundle\Templating\ThemeManager;
use Symfony\Component\Finder\Finder;

/**
 * @author <NAME> <<EMAIL>>
 */
class LayoutLocator
{
    /**
     * @var ThemeManager
     */
    protected $themeManager;

    /**
     * @var array
     */
    private $layouts;

    /**
     * @param ThemeManager $themeManager
     */
    public function __construct(ThemeManager $themeManager)
    {
        $this->themeManager = $themeManager;
    }

    /**
     * @param string $theme
     * @param string $directory
     *
     * @return array
     */
    public function getLayouts($theme, $directory = null)
    {
        if (null === $this->layouts) {
            $this->layouts = [];
            foreach ($this->themeManager->getThemes() as $id => $theme2) {
                if ($theme === $id
                    || in_array($id, $this->themeManager->getTheme($theme)->getFallback())
                    || $id === 'default') {
                    foreach ($theme2->getPaths() as $resource) {
                        $path = $this->themeManager->locateResource($resource).$directory;
                        if (is_dir($path)) {
                            $finder = new Finder();
                            $finder->files()->in($path)->depth(0)->name('*.html.twig');

                            /** @var \Symfony\Component\Finder\SplFileInfo $file */
                            foreach ($finder as $file) {
                                if (!in_array($file->getRelativePathname(), $this->layouts)) {
                                    $this->layouts[] = $file->getRelativePathname();
                                }
                            }
                        }
                    }
                }
{'fraction_non_alphanumeric': 0.13534031413612566, 'fraction_numerical': 0.0015706806282722514, 'mean_word_length': 1.576534052596089, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4145435', 'n_tokens_mistral': 1027, 'n_tokens_neox': 995, 'n_words': 224}