| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Ae\FeatureBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Ae\FeatureBundle\Twig\Node\FeatureNode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Bundle\FrameworkBundle\Console\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Finder\Finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Twig_Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Twig_Source; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Carlo Forghieri <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class LoadFeatureCommand extends ContainerAwareCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 2 |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 2 |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 2 |  |             ->setName('features:load') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 2 |  |             ->setDescription('Persist new features found in templates') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 2 |  |             ->addArgument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 2 |  |                 'path', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 2 |  |                 InputArgument::REQUIRED | InputArgument::IS_ARRAY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 'The path or bundle where to load the features' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 2 |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 2 |  |             ->addOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  |                 'dry-run', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 2 |  |                 null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2 |  |                 InputOption::VALUE_NONE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 'Do not persist new features' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 2 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |     public function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         $container = $this->getContainer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |         $twig = $container->get('twig'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |         $files = $this->getFinderInstance($input->getArgument('path')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |         $found = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |         foreach ($files as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $tree = $twig->parse($twig->tokenize(new Twig_Source( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 file_get_contents($file->getPathname()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $file->getFilename(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 $file->getPathname() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $tags = $this->findFeatureNodes($tree); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             if (empty($tags)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $found = array_merge($found, $tags); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             foreach ($tags as $tag) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 $output->writeln(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     'Found <info>%s</info>.<info>%s</info> in <info>%s</info>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     $tag['parent'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     $tag['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     $file->getFilename() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |         if ($input->getOption('dry-run')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         $manager = $container->get('ae_feature.manager'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         foreach ($found as $tag) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $manager->findOrCreate($tag['name'], $tag['parent']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * Find feature nodes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param Twig_Node $node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 1 |  |     private function findFeatureNodes(Twig_Node $node) | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $found = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         $stack = [$node]; | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         while ($stack) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             $node = array_pop($stack); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             if ($node instanceof FeatureNode) { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |                 $arguments = $node | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 1 |  |                     ->getNode('tests') | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |                     ->getNode(0) | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                     ->getNode('arguments') | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |                     ->getKeyValuePairs(); | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                 $tag = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                 foreach ($arguments as $argument) { | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |                     $keyAttr = $argument['key']->getAttribute('value'); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |                     $valueAttr = $argument['value']->getAttribute('value'); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |                     $tag[$keyAttr] = $valueAttr; | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |                 $key = md5(serialize($tag)); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |                 $found[$key] = $tag; | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |                 foreach ($node as $child) { | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |                     if (null !== $child) { | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |                         $stack[] = $child; | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         return array_values($found); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * Gets a Finder instance with required paths. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param array $dirsOrBundles Required directories or bundles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @return Finder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 2 |  |     private function getFinderInstance(array $dirsOrBundles) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 2 |  |         $finder = new Finder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 2 |  |         $application = $this->getApplication(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 2 |  |         $kernel = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 2 |  |         $bundles = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 2 |  |         if ($application instanceof Application) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 2 |  |             $kernel = $application->getKernel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 2 |  |             $bundles = $kernel->getBundles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 2 |  |         foreach ($dirsOrBundles as $dirOrBundle) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 1 |  |             if (null !== $kernel && isset($bundles[$dirOrBundle])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 $bundle = $kernel->getBundle($dirOrBundle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 $dirOrBundle = $bundle->getPath().'/Resources/views/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 1 |  |             $finder->in($dirOrBundle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return $finder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 2 |  |             ->files() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 2 |  |             ->name('*.twig'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 159 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |  | 
            
                        
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.