Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Soon/Core/Console/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Soon/Core/Console/FrontendCommand.php
<?php
/**
 * @license http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author Hervé Guétin <[email protected]> <@herveguetin>
 * @copyright Copyright (c) 2017 Agence Soon (http://www.agence-soon.fr)
 */
declare(strict_types=1);

namespace Soon\Core\Console;

use Exception;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Filesystem\Io\File as IoFile;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Theme\Model\Config as ThemeConfig;
use Magento\Theme\Model\ResourceModel\Theme\CollectionFactory;
use Magento\Theme\Model\Theme;
use Magento\Theme\Model\Theme\Registration;
use Soon\Core\Setup\Installer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class FrontendCommand extends Command
{
    /**
     * @var ThemeConfig
     */
    private $themeConfig;
    /**
     * @var CollectionFactory
     */
    private $collectionFactory;
    /**
     * @var Registration
     */
    private $themeRegistration;
    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * @var Installer
     */
    private $installer;

    /**
     * @var IoFile
     */
    private $ioFile;

    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    /**
     * FrontendCommand constructor.
     * @param Installer $installer
     * @param IoFile $ioFile
     * @param StoreManagerInterface $storeManager
     * @param ThemeConfig $themeConfig
     * @param ScopeConfigInterface $scopeConfig
     * @param CollectionFactory $collectionFactory
     * @param Registration $themeRegistration
     * @param null $name
     */
    public function __construct(
        Installer $installer,
        IoFile $ioFile,
        StoreManagerInterface $storeManager,
        ThemeConfig $themeConfig,
        ScopeConfigInterface $scopeConfig,
        CollectionFactory $collectionFactory,
        Registration $themeRegistration,
        $name = null
    ) {
        parent::__construct($name);
        $this->installer = $installer;
        $this->ioFile = $ioFile;
        $this->storeManager = $storeManager;
        $this->themeConfig = $themeConfig;
        $this->scopeConfig = $scopeConfig;
        $this->collectionFactory = $collectionFactory;
        $this->themeRegistration = $themeRegistration;
    }

    protected function configure(): void
    {
        $this->setName('jade:frontend:setup');
        $this->setDescription('Setup Jade Frontend');
        $this->addArgument(
            'namespace',
            InputArgument::REQUIRED,
            'Type theme namespace (such as \'Cnc\' for \'Cnc_default\' theme.'
        );

        parent::configure();
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output): void
    {
        $baseUrl = $this->storeManager->getDefaultStoreView()->getBaseUrl();

        $namespaceParam = ucfirst($input->getArgument('namespace'));
        $themePath = 'app/design/frontend/' . $namespaceParam . '/default';
        $defaultThemePath = 'vendor/kaliop/theme-jade-frontend/_design/frontend/Project/default';
        $frontoolsConfigPath = 'dev/tools/frontools/config';
        $frontoolsDefaultConfigPath = 'vendor/kaliop/theme-jade-frontend/_dev/tools/frontools/config';

        try {
            if ($this->ioFile->fileExists($defaultThemePath, false)) {
                if ($this->ioFile->checkAndCreateFolder($themePath, 0755)) {
                    //@codingStandardsIgnoreLine
                    shell_exec("cp -r {$defaultThemePath}/* " . $themePath);

                    $this->searchAndReplaceInFile(
                        'Project',
                        $namespaceParam,
                        $themePath . '/registration.php'
                    );
                    $this->searchAndReplaceInFile(
                        'Project',
                        $namespaceParam,
                        $themePath . '/theme.xml'
                    );

                    $this->assignTheme($namespaceParam);
                }
            }
            if ($this->ioFile->fileExists($frontoolsDefaultConfigPath, false)) {
                if ($this->ioFile->checkAndCreateFolder($frontoolsConfigPath, 0755)) {
                    //@codingStandardsIgnoreStart
                    shell_exec("cp -r {$frontoolsDefaultConfigPath}/* " . $frontoolsConfigPath);
                    shell_exec("mkdir -p dev/tools/frontools/config");
                    //@codingStandardsIgnoreEnd

                    $this->searchAndReplaceInFile(
                        'Project',
                        $namespaceParam,
                        $frontoolsConfigPath . '/themes.json'
                    );
                    $this->searchAndReplaceInFile(
                        'project',
                        strtolower($namespaceParam),
                        $frontoolsConfigPath . '/themes.json'
                    );
                    $this->searchAndReplaceInFile(
                        'http://project-default.soon/',
                        $baseUrl,
                        $frontoolsConfigPath . '/browser-sync.json'
                    );

                    //@codingStandardsIgnoreStart
                    shell_exec("npm install --prefix ./vendor/snowdog/frontools");
                    shell_exec("cd vendor/snowdog/frontools && gulp styles");
                    //@codingStandardsIgnoreEnd
                }

                $output->writeln('<info>Theme setup complete.</info>');
            }
        } catch (Exception $e) {
            $output->writeln('<error>' . $e->getMessage() . '</error>');
        }
    }

    /**
     * @param string $search
     * @param string $replace
     * @param string $fileRelativePath
     */
    private function searchAndReplaceInFile(string $search, string $replace, string $fileRelativePath): void
    {
        $fileContent = $this->ioFile->read($fileRelativePath);
        $fileContent = str_replace($search, $replace, $fileContent);
        $this->ioFile->write($fileRelativePath, $fileContent);
    }

    /**
     * @param string $namespace
     */
    public function assignTheme(string $namespace): void
    {
        $this->themeRegistration->register();
        $themes = $this->collectionFactory->create()->loadRegisteredThemes();
        /** @var Theme $theme */
        foreach ($themes as $theme) {
            if ($theme->getCode() == $namespace . '/default') {
                $this->themeConfig->assignToStore(
                    $theme,
                    [Store::DEFAULT_STORE_ID],
                    ScopeConfigInterface::SCOPE_TYPE_DEFAULT
                );
                break;
            }
        }
    }
}

Spamworldpro Mini