<commit_before><?php
/**
 * Shopware 4
 * Copyright © shopware AG
 *
 * According to our dual licensing model, this program can be used either
 * under the terms of the GNU Affero General Public License, version 3,
 * or under a proprietary license.
 *
 * The texts of the GNU Affero General Public License with an additional
 * permission and of our proprietary license can be found at and
 * in the LICENSE file you have received along with this program.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * "Shopware" is a registered trademark of shopware AG.
 * The licensing of the program under the AGPLv3 does not imply a
 * trademark license. Therefore any rights, title and interest in
 * our trademarks remain entirely with us.
 */
namespace Shopware\Components\Theme;

use Doctrine\ORM\AbstractQuery;
use Shopware\Components\Model\ModelManager;
use Shopware\Components\Model\ModelRepository;
use Shopware\Components\Snippet\DatabaseHandler;
use Shopware\Models\Plugin\Plugin;
use Shopware\Models\Shop as Shop;
use Shopware\Components\Theme;

/**
 * The Theme\Installer class handles the theme installation.
 * It synchronize the file system themes with the already
 * installed themes which stored in the database.
 * Within the synchronization process the installer class
 * uses the Theme\Configurator class to synchronize the
 * theme configuration with the database.
 *
 * @category  Shopware
 * @package   Shopware\Components\Theme
 * @copyright Copyright (c) shopware AG (http://www.shopware.de)
 */
class Installer
{
    /**
     * @var ModelManager
     */
    private $entityManager;

    /**
     * @var PathResolver
     */
    private $pathResolver;

    /**
     * @var Configurator
     */
    private $configurator;

    /**
     * @var Util
     */
    private $util;

    /**
     * @var DatabaseHandler
     */
    private $snippetWriter;

    /**
     * @var ModelRepository
     */
    private $repository;

    /**
     * @var Service
     */
    private $service;

    /**
     * @var array The config options provided in the global config.php file
     */
    protected $snippetConfig;

    function __construct(
        ModelManager $entityManager,
        Configurator $configurator,
        PathResolver $pathResolver,
        Util $util,
        DatabaseHandler $snippetWriter,
        Service $service,
        $snippetConfig = array()
    ) {
        $this->configurator = $configurator;
        $this->entityManager = $entityManager;
        $this->pathResolver = $pathResolver;
        $this->snippetWriter = $snippetWriter;
        $this->util = $util;
        $this->repository = $entityManager->getRepository('Shopware\Models\Shop\Template');
        $this->service = $service;
        $this->snippetConfig = $snippetConfig;
    }

    /**
     * Synchronize the file system themes
     * with the already installed themes which stored in the database.
     * The function initials additionally the old shopware 3.5 templates.
     *
     * The synchronization are processed in the synchronizeThemes and
     * synchronizeTemplates function.
     */
    public function synchronize()
    {
        $this->synchronizeThemes();
        $this->synchronizeTemplates();
    }

    /**
     * Iterates all Shopware 5 themes which
     * stored in the /engine/Shopware/Themes directory.
     * Each theme are stored as new Shopware\Models\Shop\Template.
     *
     * After the themes are initialed and stored in the database,
     * the function resolves the inheritance of each theme.
     *
     * After the inheritance is build, the installer uses
     * the Theme\Configurator to synchronize the theme configurations.
     *
     */
    private function synchronizeThemes()
    {
        //creates a directory iterator for the default theme directory (engine/Shopware/Themes)
        $directories = new \DirectoryIterator(
            $this->pathResolver->getFrontendThemeDirectory()
        );

        //synchronize the default themes which are stored in the engine/Shopware/Themes directory.
        $themes = $this->synchronizeThemeDirectories($directories);

        //to prevent inconsistent data, themes that were removed from the file system have to be removed.
        $this->removeDeletedThemes();

        //before the inheritance can be built, the plugin themes have to be initialized.
        $pluginThemes = $this->synchronizePluginThemes();

        $themes = array_merge($themes, $pluginThemes);

        //builds the theme inheritance
        $this->setParents($themes);

        /**@var $theme Theme */
        foreach ($themes as $theme) {
            $this->configurator->synchronize($theme);
        }
    }

    /**
     * Iterates all Shopware 4 templates which
     * stored in the /templates/ directory.
     * Each template are stored as new Shopware\Models\Shop\Template.
     */
    private function synchronizeTemplates()
    {
        $directories = new \DirectoryIterator(
            $this->pathResolver->getDefaultTemplateDirectory()
        );

        /**@var $directory \DirectoryIterator */
        foreach ($directories as $directory) {
            //check valid directory
            if ($directory->isDot()
                || !$directory->isDir()
                || strpos($directory->getFilename(), '_') === 0
            ) {
                continue;
            }

            if (strpos($directories->getFilename(), 'emotion') == false) {
                continue;
            }

            //draw template information over the directory
            $data = $this->getTemplateDefinition($directory);

            $template = $this->repository->findOneBy(array(
                'template' => $data['template']
            ));<commit_msg>SW-9026 - Fix theme installer. If the templates directory not exist, the theme installer skip the old templates<commit_after><?php
/**
 * Shopware 4
 * Copyright © shopware AG
 *
 * According to our dual licensing model, this program can be used either
 * under the terms of the GNU Affero General Public License, version 3,
 * or under a proprietary license.
 *
 * The texts of the GNU Affero General Public License with an additional
 * permission and of our proprietary license can be found at and
 * in the LICENSE file you have received along with this program.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * "Shopware" is a registered trademark of shopware AG.
 * The licensing of the program under the AGPLv3 does not imply a
 * trademark license. Therefore any rights, title and interest in
 * our trademarks remain entirely with us.
 */
namespace Shopware\Components\Theme;

use Doctrine\ORM\AbstractQuery;
use Shopware\Components\Model\ModelManager;
use Shopware\Components\Model\ModelRepository;
use Shopware\Components\Snippet\DatabaseHandler;
use Shopware\Models\Plugin\Plugin;
use Shopware\Models\Shop as Shop;
use Shopware\Components\Theme;

/**
 * The Theme\Installer class handles the theme installation.
 * It synchronize the file system themes with the already
 * installed themes which stored in the database.
 * Within the synchronization process the installer class
 * uses the Theme\Configurator class to synchronize the
 * theme configuration with the database.
 *
 * @category  Shopware
 * @package   Shopware\Components\Theme
 * @copyright Copyright (c) shopware AG (http://www.shopware.de)
 */
class Installer
{
    /**
     * @var ModelManager
     */
    private $entityManager;

    /**
     * @var PathResolver
     */
    private $pathResolver;

    /**
     * @var Configurator
     */
    private $configurator;

    /**
     * @var Util
     */
    private $util;

    /**
     * @var DatabaseHandler
     */
    private $snippetWriter;

    /**
     * @var ModelRepository
     */
    private $repository;

    /**
     * @var Service
     */
    private $service;

    /**
     * @var array The config options provided in the global config.php file
     */
    protected $snippetConfig;

    function __construct(
        ModelManager $entityManager,
        Configurator $configurator,
        PathResolver $pathResolver,
        Util $util,
        DatabaseHandler $snippetWriter,
        Service $service,
        $snippetConfig = array()
    ) {
        $this->configurator = $configurator;
        $this->entityManager = $entityManager;
        $this->pathResolver = $pathResolver;
        $this->snippetWriter = $snippetWriter;
        $this->util = $util;
        $this->repository = $entityManager->getRepository('Shopware\Models\Shop\Template');
        $this->service = $service;
        $this->snippetConfig = $snippetConfig;
    }

    /**
     * Synchronize the file system themes
     * with the already installed themes which stored in the database.
     * The function initials additionally the old shopware 3.5 templates.
     *
     * The synchronization are processed in the synchronizeThemes and
     * synchronizeTemplates function.
     */
    public function synchronize()
    {
        $this->synchronizeThemes();
        $this->synchronizeTemplates();
    }

    /**
     * Iterates all Shopware 5 themes which
     * stored in the /engine/Shopware/Themes directory.
     * Each theme are stored as new Shopware\Models\Shop\Template.
     *
     * After the themes are initialed and stored in the database,
     * the function resolves the inheritance of each theme.
     *
     * After the inheritance is build, the installer uses
     * the Theme\Configurator to synchronize the theme configurations.
     *
     */
    private function synchronizeThemes()
    {
        //creates a directory iterator for the default theme directory (engine/Shopware/Themes)
        $directories = new \DirectoryIterator(
            $this->pathResolver->getFrontendThemeDirectory()
        );

        //synchronize the default themes which are stored in the engine/Shopware/Themes directory.
        $themes = $this->synchronizeThemeDirectories($directories);

        //to prevent inconsistent data, themes that were removed from the file system have to be removed.
        $this->removeDeletedThemes();

        //before the inheritance can be built, the plugin themes have to be initialized.
        $pluginThemes = $this->synchronizePluginThemes();

        $themes = array_merge($themes, $pluginThemes);

        //builds the theme inheritance
        $this->setParents($themes);

        /**@var $theme Theme */
        foreach ($themes as $theme) {
            $this->configurator->synchronize($theme);
        }
    }

    /**
     * Iterates all Shopware 4 templates which
     * stored in the /templates/ directory.
     * Each template are stored as new Shopware\Models\Shop\Template.
     */
    private function synchronizeTemplates()
    {
        if (!is_dir($this->pathResolver->getDefaultTemplateDirectory())) {
            return;
        }

        $directories = new \DirectoryIterator(
            $this->pathResolver->getDefaultTemplateDirectory()
        );

        /**@var $directory \DirectoryIterator */
        foreach ($directories as $directory) {
            //check valid directory
            if ($directory->isDot()
                || !$directory->isDir()
                || strpos($directory->getFilename(), '_') === 0
            ) {
                continue;
            }

            if (strpos($directories->getFilename(), 'emotion') == false) {
                continue;
            }

            //draw template information over the directory
            $data = $this->getTemplateDefinition($directory);

            $template = $this->repository->findOneBy(array(
                'template' => $data['template']
            ));
{'fraction_non_alphanumeric': 0.09173019555850183, 'fraction_numerical': 0.0016572754391779914, 'mean_word_length': 2.53411420204978, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '405798', 'n_tokens_mistral': 3121, 'n_tokens_neox': 3016, 'n_words': 1049}