| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace JK\SamBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use JK\Sam\File\Locator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use JK\Sam\File\Normalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use JK\Sam\Task\Task; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use JK\Sam\Task\TaskRunner; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Style\SymfonyStyle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\DependencyInjection\ContainerAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class BuildCommand extends AbstractCommand implements ContainerAwareInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * Configure the task name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1 |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 1 |  |             ->setName('jk:assets:build') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 1 |  |             ->setDescription('Build the assets according to your assets configuration ("jk_assets")') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 1 |  |             ->addOption('config', 'c', InputOption::VALUE_OPTIONAL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 'If defined, this file will be used to load the assets configuration. It should be an yml file ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 1 |  |                 .'containing an array of tasks and filters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                     jk.assets: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                     ____tasks: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                     ________// your configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                     ________... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                     ____filters: ... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return int|null|void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @throws Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 1 |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 1 |  |         $this->io = new SymfonyStyle($input, $output); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $this | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 1 |  |             ->io | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 1 |  |             ->title('Symfony PHP Assets Manager'); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         // load configuration from container or given file path | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 1 |  |         $configuration = $this->loadConfiguration($input); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         // get debug mode | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 1 |  |         $this->debug = $configuration['debug']; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 1 |  |         if ($this->debug) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 1 |  |             $this->io->note('Debug Mode...'); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         // build tasks to run | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 1 |  |         $tasks = $this->buildTasks($configuration['tasks']); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         // build required filters | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 1 |  |         $filters = $this->buildFilters($configuration['filters']); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         // run task with configured filter | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 1 |  |         $normalizer = new Normalizer($this->container->getParameter('kernel.root_dir').'/../'); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 1 |  |         $locator = new Locator($normalizer); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         // create the runner | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 1 |  |         $runner = new TaskRunner( | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 1 |  |             $filters, | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 1 |  |             $locator, | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 1 |  |             $this->debug | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 1 |  |         $this->io->text('- Running tasks...'); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         // run tasks | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 1 |  |         foreach ($tasks as $task) { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 1 |  |             $this->runManagedTask($runner, $task); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         // display end message | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 1 |  |         $this->io->success('Assets build end'); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param TaskRunner $runner | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param Task $task | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |     protected function runManagedTask(TaskRunner $runner, Task $task) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $notificationSubscriber = $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |             ->container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |             ->get('jk.assets.notification_subscriber'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |             $this->io->text('- Running '.$task->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |             $runner->run($task); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |             foreach ($notificationSubscriber->getNotifications() as $notification) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |                 $this->io->text('  <info>x</info> '.$notification); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |             $notificationSubscriber->clearNotifications(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |             $this->io->newLine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             if ($this->debug) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 foreach ($notificationSubscriber->getNotifications() as $index => $notification) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                     if ($index == count($notificationSubscriber->getNotifications())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                         $notification = '<error>x</error> '.$notification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                         $notification = '<info>x</info>'.$notification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     $this->io->text($notification); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 $notificationSubscriber->clearNotifications(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             throw new Exception( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 'An error has been encountered during the execution of the task '.$task->getName()."\n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 .$e->getMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 $e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 135 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |  |