| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Sonata Project package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * (c) Thomas Rabaix <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Sonata\EasyExtendsBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Sonata\EasyExtendsBundle\Bundle\BundleMetadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\HttpKernel\Bundle\BundleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * Generate Application entities from bundle entities. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @author Thomas Rabaix <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | class GenerateCommand extends ContainerAwareCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     protected function configure(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         $this | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             ->setName('sonata:easy-extends:generate') | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             ->setHelp(<<<'EOT' | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | The <info>easy-extends:generate:entities</info> command generating a valid bundle structure from a Vendor Bundle. | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |   <info>ie: ./app/console sonata:easy-extends:generate SonataUserBundle</info> | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | EOT | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->setDescription('Create entities used by Sonata\'s bundles'); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $this->addArgument('bundle', InputArgument::IS_ARRAY, 'The bundle name to "easy-extends"'); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $this->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             'dest', | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             'd', | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             InputOption::VALUE_OPTIONAL, | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             'The base folder where the Application will be created', | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             false | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $this->addOption('namespace', 'ns', InputOption::VALUE_OPTIONAL, 'The namespace for the classes', false); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $this->addOption('namespace_prefix', 'nsp', InputOption::VALUE_OPTIONAL, 'The namespace prefix for the classes', false); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     protected function execute(InputInterface $input, OutputInterface $output): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $destOption = $input->getOption('dest'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if ($destOption) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $dest = realpath($destOption); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             if (false === $dest) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 throw new \RuntimeException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     'The provided destination folder \'%s\' does not exist!', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     $destOption | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $dest = $this->getContainer()->get('kernel')->getRootDir(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $namespace = $input->getOption('namespace'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         if ($namespace) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             if (!preg_match('/^(?:(?:[[:alnum:]]+|:vendor)\\\\?)+$/', $namespace)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 throw new \InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     'The provided namespace \'%s\' is not a valid namespace!', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     $namespace | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $namespace = 'Application\:vendor'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $configuration = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             'application_dir' => sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 '%s%s%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 $dest, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 DIRECTORY_SEPARATOR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 str_replace('\\', DIRECTORY_SEPARATOR, $namespace) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             'namespace' => $namespace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             'namespace_prefix' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         if ($namespacePrefix = $input->getOption('namespace_prefix')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $configuration['namespace_prefix'] = rtrim($namespacePrefix, '\\').'\\'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $bundleNames = $input->getArgument('bundle'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if (empty($bundleNames)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $output->writeln(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $output->writeln('<error>You must provide a bundle name!</error>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $output->writeln(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $output->writeln('  Bundles availables :'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             /** @var BundleInterface $bundle */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $bundleMetadata = new BundleMetadata($bundle, $configuration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 if (!$bundleMetadata->isExtendable()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 $output->writeln(sprintf('     - %s', $bundle->getName())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $output->writeln(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             foreach ($bundleNames as $bundleName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 $processed = $this->generate($bundleName, $configuration, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 if (!$processed) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                     throw new \RuntimeException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                         '<error>The bundle \'%s\' does not exist or is not registered in the kernel!</error>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                         $bundleName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                     )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $output->writeln('done!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * Generates a bundle entities from a bundle name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param string          $bundleName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @param array           $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     protected function generate(string $bundleName, array $configuration, OutputInterface $output): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $processed = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         /** @var BundleInterface $bundle */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             if ($bundle->getName() != $bundleName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             $processed = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             $bundleMetadata = new BundleMetadata($bundle, $configuration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             // generate the bundle file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             if (!$bundleMetadata->isExtendable()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                 $output->writeln(sprintf('Ignoring bundle : "<comment>%s</comment>"', $bundleMetadata->getClass())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             // generate the bundle file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             if (!$bundleMetadata->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                 $output->writeln(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                     '%s : <comment>wrong directory structure</comment>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                     $bundleMetadata->getClass() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             $output->writeln(sprintf('Processing bundle : "<info>%s</info>"', $bundleMetadata->getName())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             $this->getContainer()->get('sonata.easy_extends.generator.bundle') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |                 ->generate($output, $bundleMetadata); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             $output->writeln(sprintf('Processing Doctrine ORM : "<info>%s</info>"', $bundleMetadata->getName())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             $this->getContainer()->get('sonata.easy_extends.generator.orm') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                 ->generate($output, $bundleMetadata); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             $output->writeln(sprintf('Processing Doctrine ODM : "<info>%s</info>"', $bundleMetadata->getName())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             $this->getContainer()->get('sonata.easy_extends.generator.odm') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |                 ->generate($output, $bundleMetadata); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             $output->writeln(sprintf('Processing Doctrine PHPCR : "<info>%s</info>"', $bundleMetadata->getName())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             $this->getContainer()->get('sonata.easy_extends.generator.phpcr') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |                 ->generate($output, $bundleMetadata); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |             $output->writeln(sprintf('Processing Serializer config : "<info>%s</info>"', $bundleMetadata->getName())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |             $this->getContainer()->get('sonata.easy_extends.generator.serializer') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |                 ->generate($output, $bundleMetadata); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |             $output->writeln(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |         return $processed; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 204 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 205 |  |  |  |