| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Sonata Project package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Thomas Rabaix <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Sonata\EasyExtendsBundle\Generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Sonata\EasyExtendsBundle\Bundle\BundleMetadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class OrmGenerator implements GeneratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $entityTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $entityRepositoryTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->entityTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/orm/entity.mustache'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->entityRepositoryTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/orm/repository.mustache'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function generate(OutputInterface $output, BundleMetadata $bundleMetadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->generateMappingEntityFiles($output, $bundleMetadata); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->generateEntityFiles($output, $bundleMetadata); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->generateEntityRepositoryFiles($output, $bundleMetadata); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param BundleMetadata  $bundleMetadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function generateMappingEntityFiles(OutputInterface $output, BundleMetadata $bundleMetadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $output->writeln(' - Copy entity files'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $files = $bundleMetadata->getOrmMetadata()->getEntityMappingFiles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         foreach ($files as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             // copy mapping definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $fileName = substr($file->getFileName(), 0, strrpos($file->getFileName(), '.')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $dest_file = sprintf('%s/%s', $bundleMetadata->getOrmMetadata()->getExtendedMappingEntityDirectory(), $fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $src_file = sprintf('%s/%s', $bundleMetadata->getOrmMetadata()->getMappingEntityDirectory(), $file->getFileName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             if (is_file($dest_file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 $output->writeln(sprintf('   ~ <info>%s</info>', $fileName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $output->writeln(sprintf('   + <info>%s</info>', $fileName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 copy($src_file, $dest_file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param BundleMetadata  $bundleMetadata | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     public function generateEntityFiles(OutputInterface $output, BundleMetadata $bundleMetadata) | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         $output->writeln(' - Generating entity files'); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         $names = $bundleMetadata->getOrmMetadata()->getEntityNames(); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         foreach ($names as $name) { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             $extendedName = $name; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             $dest_file = sprintf('%s/%s.php', $bundleMetadata->getOrmMetadata()->getExtendedEntityDirectory(), $name); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             $src_file = sprintf('%s/%s.php', $bundleMetadata->getOrmMetadata()->getEntityDirectory(), $extendedName); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             if (!is_file($src_file)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |                 $extendedName = 'Base'.$name; | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |                 $src_file = sprintf('%s/%s.php', $bundleMetadata->getOrmMetadata()->getEntityDirectory(), $extendedName); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |                 if (!is_file($src_file)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |                     $output->writeln(sprintf('   ! <info>%s</info>', $extendedName)); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                     continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             if (is_file($dest_file)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |                 $output->writeln(sprintf('   ~ <info>%s</info>', $name)); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |                 $output->writeln(sprintf('   + <info>%s</info>', $name)); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |                 $string = Mustache::replace($this->getEntityTemplate(), array( | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                     'extended_namespace' => $bundleMetadata->getExtendedNamespace(), | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |                     'name' => $name != $extendedName ? $extendedName : $name, | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |                     'class' => $name, | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                     'extended_name' => $name == $extendedName ? 'Base'.$name : $extendedName, | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                     'namespace' => $bundleMetadata->getNamespace(), | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |                 )); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |                 file_put_contents($dest_file, $string); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param BundleMetadata  $bundleMetadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     public function generateEntityRepositoryFiles(OutputInterface $output, BundleMetadata $bundleMetadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $output->writeln(' - Generating entity repository files'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $names = $bundleMetadata->getOrmMetadata()->getEntityNames(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         foreach ($names as $name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $dest_file = sprintf('%s/%sRepository.php', $bundleMetadata->getOrmMetadata()->getExtendedEntityDirectory(), $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $src_file = sprintf('%s/Base%sRepository.php', $bundleMetadata->getOrmMetadata()->getEntityDirectory(), $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             if (!is_file($src_file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 $output->writeln(sprintf('   ! <info>%sRepository</info>', $name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             if (is_file($dest_file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 $output->writeln(sprintf('   ~ <info>%sRepository</info>', $name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 $output->writeln(sprintf('   + <info>%sRepository</info>', $name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 $string = Mustache::replace($this->getEntityRepositoryTemplate(), array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                     'extended_namespace' => $bundleMetadata->getExtendedNamespace(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                     'name' => $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                     'namespace' => $bundleMetadata->getNamespace(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                 file_put_contents($dest_file, $string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     public function getEntityTemplate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return $this->entityTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     public function getEntityRepositoryTemplate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         return $this->entityRepositoryTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 165 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |  |