| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Superdesk Web Publisher Template Engine Bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright 2016 Sourcefabric z.ú. and contributors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please see the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * AUTHORS and LICENSE files distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright 2016 Sourcefabric z.ú | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license http://www.superdesk.org/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace SWP\Bundle\CoreBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use SWP\Component\Common\Model\ThemeAwareTenantInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use SWP\Component\MultiTenancy\Exception\TenantNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Symfony\Component\Console\Question\ConfirmationQuestion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Component\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class ThemeSetupCommand extends ContainerAwareCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 85 |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 85 |  |             ->setName('swp:theme:install') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 85 |  |             ->setDescription('Installs theme.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 85 |  |             ->addArgument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 85 |  |                 'tenant', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 85 |  |                 InputArgument::REQUIRED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 85 |  |                 'Tenant code. For this tenant the theme will be installed.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 85 |  |             ->addArgument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 85 |  |                 'theme_dir', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 85 |  |                 InputArgument::REQUIRED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 85 |  |                 'Path to theme you want to install.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 85 |  |             ->addOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 85 |  |                 'force', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 85 |  |                 'f', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 85 |  |                 InputOption::VALUE_NONE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 85 |  |                 'If set, forces to execute an action without confirmation.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 85 |  |             ->setHelp( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 <<<'EOT' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 The <info>%command.name%</info> command installs your custom theme for given tenant: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |   <info>%command.full_name% <tenant> <theme_dir></info> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | You need specify the directory (<comment>theme_dir</comment>) argument to install  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | theme from any directory: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |   <info>%command.full_name% <tenant> /dir/to/theme | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |    | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | Once executed, it will create directory <comment>app/themes/<tenant></comment> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | where <comment><tenant></comment> is the tenant code you typed in the first argument. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | To force an action, you need to add an option: <info>--force</info>: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 85 |  |   <info>%command.full_name% <tenant> <theme_dir> --force</info> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | EOT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 85 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 4 |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 4 |  |         $fileSystem = new Filesystem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 4 |  |         $helper = $this->getHelper('question'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 4 |  |         $force = true === $input->getOption('force'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         /** @var ThemeAwareTenantInterface $tenant */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  |         $tenant = $this->getContainer()->get('swp.repository.tenant') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 4 |  |             ->findOneByCode($input->getArgument('tenant')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 4 |  |         $sourceDir = $input->getArgument('theme_dir'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 4 |  |         $this->assertTenantIsFound($input->getArgument('tenant'), $tenant); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  |         if (!$fileSystem->exists($sourceDir) || !is_dir($sourceDir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |             $output->writeln(sprintf('<error>Directory "%s" does not exist or it is not a directory!</error>', $sourceDir)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 2 |  |         $themesDir = $this->getContainer()->getParameter('swp.theme.configuration.default_directory'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |         $tenantThemeDir = $themesDir.\DIRECTORY_SEPARATOR.$tenant->getCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 2 |  |         $themeDir = $tenantThemeDir.\DIRECTORY_SEPARATOR.basename($sourceDir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  |             $question = new ConfirmationQuestion( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |                 '<question>This will override your current theme. Continue with this action? (yes/no)<question> <comment>[yes]</comment> ', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 2 |  |                 true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 2 |  |                 '/^(y|j)/i' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |             if (!$force) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 if (!$helper->ask($input, $output, $question)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |             $fileSystem->mirror($sourceDir, $themeDir, null, ['override' => true, 'delete' => true]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |             $output->writeln('<info>Theme has been installed successfully!</info>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |             $output->writeln('<error>Theme could not be installed!</error>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |             $output->writeln('<error>Stacktrace: '.$e->getMessage().'</error>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 4 |  |     private function assertTenantIsFound(string $tenantCode, ThemeAwareTenantInterface $tenant = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 4 |  |         if (null === $tenant) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |             throw new TenantNotFoundException($tenantCode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  |