| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of Pomm's ModelManager package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * (c) 2014 - 2015 Grégoire HUBERT <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace PommProject\ModelManager\Generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use PommProject\Foundation\Inflector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use PommProject\Foundation\ParameterHolder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use PommProject\Foundation\Where; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use PommProject\ModelManager\Exception\GeneratorException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * ModelGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Generate a new model file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * If the given file already exist, it needs the force option to be set at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * 'yes'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @package   ModelManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @copyright 2014 - 2015 Grégoire HUBERT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @author    Grégoire HUBERT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @license   X11 {@link http://opensource.org/licenses/mit-license.php} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | class ModelGenerator extends BaseGenerator | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * generate | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * Generate structure file. | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @see BaseGenerator | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function generate(ParameterHolder $input, array $output = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $schema_oid = $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             ->getSession() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             ->getInspector() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             ->getSchemaOid($this->schema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if ($schema_oid === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             throw new GeneratorException(sprintf("Schema '%s' does not exist.", $this->schema)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $relations_info = $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ->getSession() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ->getInspector() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ->getSchemaRelations($schema_oid, new Where('cl.relname = $*', [$this->relation])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if ($relations_info->isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             throw new GeneratorException(sprintf("Relation '%s.%s' does not exist.", $this->schema, $this->relation)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             ->checkOverwrite($input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             ->outputFileCreation($output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ->saveFile( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 $this->filename, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $this->mergeTemplate( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                         'entity'        => Inflector::studlyCaps($this->relation), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                             'namespace'     => trim($this->namespace, '\\'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                             'trait'         => $relations_info->current()['type'] === 'table' ? 'WriteQueries' : 'ReadQueries', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                             'relation_type' => $relations_info->current()['type'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                             'relation'      => $this->relation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $output; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * getCodeTemplate | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @see BaseGenerator | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     protected function getCodeTemplate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return <<<'_' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | namespace {:namespace:}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | use PommProject\ModelManager\Model\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | use PommProject\ModelManager\Model\Projection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | use PommProject\ModelManager\Model\ModelTrait\{:trait:}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | use PommProject\Foundation\Where; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | use {:namespace:}\AutoStructure\{:entity:} as {:entity:}Structure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | use {:namespace:}\{:entity:}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  * {:entity:}Model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  * Model class for {:relation_type:} {:relation:}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  * @see Model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | class {:entity:}Model extends Model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     use {:trait:}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * Model constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->structure = new {:entity:}Structure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $this->flexible_entity_class = '\{:namespace:}\{:entity:}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | _; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |  |