@@ -4,7 +4,6 @@ |
||
| 4 | 4 | |
| 5 | 5 | use Doctrine\Common\Inflector\Inflector; |
| 6 | 6 | use Doctrine\DBAL\Driver\Connection; |
| 7 | -use Doctrine\DBAL\Schema\Column; |
|
| 8 | 7 | use Doctrine\DBAL\Schema\Schema; |
| 9 | 8 | use Doctrine\DBAL\Schema\Table; |
| 10 | 9 | use Doctrine\DBAL\Types\Type; |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | /** |
| 148 | 148 | * Returns the name of the base bean class from the table name. |
| 149 | 149 | * |
| 150 | - * @param $tableName |
|
| 150 | + * @param string $tableName |
|
| 151 | 151 | * |
| 152 | 152 | * @return string |
| 153 | 153 | */ |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | /** |
| 160 | 160 | * Returns the name of the base DAO class from the table name. |
| 161 | 161 | * |
| 162 | - * @param $tableName |
|
| 162 | + * @param string $tableName |
|
| 163 | 163 | * |
| 164 | 164 | * @return string |
| 165 | 165 | */ |
@@ -176,6 +176,7 @@ discard block |
||
| 176 | 176 | * @param Table $table The table |
| 177 | 177 | * @param string $beannamespace The namespace of the bean |
| 178 | 178 | * @param ClassNameMapper $classNameMapper |
| 179 | + * @param boolean $storeInUtc |
|
| 179 | 180 | * |
| 180 | 181 | * @throws TDBMException |
| 181 | 182 | */ |
@@ -486,6 +487,8 @@ discard block |
||
| 486 | 487 | * Generates the factory bean. |
| 487 | 488 | * |
| 488 | 489 | * @param Table[] $tableList |
| 490 | + * @param string $daoFactoryClassName |
|
| 491 | + * @param string $daoNamespace |
|
| 489 | 492 | */ |
| 490 | 493 | private function generateFactory(array $tableList, $daoFactoryClassName, $daoNamespace, ClassNameMapper $classNameMapper) |
| 491 | 494 | { |
@@ -17,201 +17,201 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class TDBMDaoGenerator |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @var SchemaAnalyzer |
|
| 22 | - */ |
|
| 23 | - private $schemaAnalyzer; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var Schema |
|
| 27 | - */ |
|
| 28 | - private $schema; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The root directory of the project. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - private $rootPath; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Name of composer file. |
|
| 39 | - * |
|
| 40 | - * @var string |
|
| 41 | - */ |
|
| 42 | - private $composerFile; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @var TDBMSchemaAnalyzer |
|
| 46 | - */ |
|
| 47 | - private $tdbmSchemaAnalyzer; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Constructor. |
|
| 51 | - * |
|
| 52 | - * @param SchemaAnalyzer $schemaAnalyzer |
|
| 53 | - * @param Schema $schema |
|
| 54 | - * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
|
| 55 | - */ |
|
| 56 | - public function __construct(SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
| 57 | - { |
|
| 58 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 59 | - $this->schema = $schema; |
|
| 60 | - $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
| 61 | - $this->rootPath = __DIR__.'/../../../../../../../../'; |
|
| 62 | - $this->composerFile = 'composer.json'; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Generates all the daos and beans. |
|
| 67 | - * |
|
| 68 | - * @param string $daoFactoryClassName The classe name of the DAO factory |
|
| 69 | - * @param string $daonamespace The namespace for the DAOs, without trailing \ |
|
| 70 | - * @param string $beannamespace The Namespace for the beans, without trailing \ |
|
| 71 | - * @param bool $storeInUtc If the generated daos should store the date in UTC timezone instead of user's timezone. |
|
| 72 | - * |
|
| 73 | - * @return \string[] the list of tables |
|
| 74 | - * |
|
| 75 | - * @throws TDBMException |
|
| 76 | - */ |
|
| 77 | - public function generateAllDaosAndBeans($daoFactoryClassName, $daonamespace, $beannamespace, $storeInUtc) |
|
| 78 | - { |
|
| 79 | - // TODO: extract ClassNameMapper in its own package! |
|
| 80 | - $classNameMapper = ClassNameMapper::createFromComposerFile($this->rootPath.$this->composerFile); |
|
| 81 | - // TODO: check that no class name ends with "Base". Otherwise, there will be name clash. |
|
| 82 | - |
|
| 83 | - $tableList = $this->schema->getTables(); |
|
| 84 | - |
|
| 85 | - // Remove all beans and daos from junction tables |
|
| 86 | - $junctionTables = $this->schemaAnalyzer->detectJunctionTables(); |
|
| 87 | - $junctionTableNames = array_map(function (Table $table) { |
|
| 88 | - return $table->getName(); |
|
| 89 | - }, $junctionTables); |
|
| 90 | - |
|
| 91 | - $tableList = array_filter($tableList, function (Table $table) use ($junctionTableNames) { |
|
| 92 | - return !in_array($table->getName(), $junctionTableNames); |
|
| 93 | - }); |
|
| 94 | - |
|
| 95 | - foreach ($tableList as $table) { |
|
| 96 | - $this->generateDaoAndBean($table, $daonamespace, $beannamespace, $classNameMapper, $storeInUtc); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $this->generateFactory($tableList, $daoFactoryClassName, $daonamespace, $classNameMapper); |
|
| 100 | - |
|
| 101 | - // Ok, let's return the list of all tables. |
|
| 102 | - // These will be used by the calling script to create Mouf instances. |
|
| 103 | - |
|
| 104 | - return array_map(function (Table $table) { return $table->getName(); }, $tableList); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Generates in one method call the daos and the beans for one table. |
|
| 109 | - * |
|
| 110 | - * @param Table $table |
|
| 111 | - * @param string $daonamespace |
|
| 112 | - * @param string $beannamespace |
|
| 113 | - * @param ClassNameMapper $classNameMapper |
|
| 114 | - * @param bool $storeInUtc |
|
| 115 | - * |
|
| 116 | - * @throws TDBMException |
|
| 117 | - */ |
|
| 118 | - public function generateDaoAndBean(Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc) |
|
| 119 | - { |
|
| 120 | - $tableName = $table->getName(); |
|
| 121 | - $daoName = $this->getDaoNameFromTableName($tableName); |
|
| 122 | - $beanName = $this->getBeanNameFromTableName($tableName); |
|
| 123 | - $baseBeanName = $this->getBaseBeanNameFromTableName($tableName); |
|
| 124 | - $baseDaoName = $this->getBaseDaoNameFromTableName($tableName); |
|
| 125 | - |
|
| 126 | - $beanDescriptor = new BeanDescriptor($table, $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer); |
|
| 127 | - $this->generateBean($beanDescriptor, $beanName, $baseBeanName, $table, $beannamespace, $classNameMapper, $storeInUtc); |
|
| 128 | - $this->generateDao($beanDescriptor, $daoName, $baseDaoName, $beanName, $table, $daonamespace, $beannamespace, $classNameMapper); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Returns the name of the bean class from the table name. |
|
| 133 | - * |
|
| 134 | - * @param $tableName |
|
| 135 | - * |
|
| 136 | - * @return string |
|
| 137 | - */ |
|
| 138 | - public static function getBeanNameFromTableName($tableName) |
|
| 139 | - { |
|
| 140 | - return self::toSingular(self::toCamelCase($tableName)).'Bean'; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Returns the name of the DAO class from the table name. |
|
| 145 | - * |
|
| 146 | - * @param $tableName |
|
| 147 | - * |
|
| 148 | - * @return string |
|
| 149 | - */ |
|
| 150 | - public static function getDaoNameFromTableName($tableName) |
|
| 151 | - { |
|
| 152 | - return self::toSingular(self::toCamelCase($tableName)).'Dao'; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Returns the name of the base bean class from the table name. |
|
| 157 | - * |
|
| 158 | - * @param $tableName |
|
| 159 | - * |
|
| 160 | - * @return string |
|
| 161 | - */ |
|
| 162 | - public static function getBaseBeanNameFromTableName($tableName) |
|
| 163 | - { |
|
| 164 | - return self::toSingular(self::toCamelCase($tableName)).'BaseBean'; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Returns the name of the base DAO class from the table name. |
|
| 169 | - * |
|
| 170 | - * @param $tableName |
|
| 171 | - * |
|
| 172 | - * @return string |
|
| 173 | - */ |
|
| 174 | - public static function getBaseDaoNameFromTableName($tableName) |
|
| 175 | - { |
|
| 176 | - return self::toSingular(self::toCamelCase($tableName)).'BaseDao'; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 181 | - * |
|
| 182 | - * @param BeanDescriptor $beanDescriptor |
|
| 183 | - * @param string $className The name of the class |
|
| 184 | - * @param string $baseClassName The name of the base class which will be extended (name only, no directory) |
|
| 185 | - * @param Table $table The table |
|
| 186 | - * @param string $beannamespace The namespace of the bean |
|
| 187 | - * @param ClassNameMapper $classNameMapper |
|
| 188 | - * |
|
| 189 | - * @throws TDBMException |
|
| 190 | - */ |
|
| 191 | - public function generateBean(BeanDescriptor $beanDescriptor, $className, $baseClassName, Table $table, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc) |
|
| 192 | - { |
|
| 193 | - $str = $beanDescriptor->generatePhpCode($beannamespace); |
|
| 194 | - |
|
| 195 | - $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\Generated\\'.$baseClassName); |
|
| 196 | - if (empty($possibleBaseFileNames)) { |
|
| 197 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$baseClassName.'" is not autoloadable.'); |
|
| 198 | - } |
|
| 199 | - $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 200 | - |
|
| 201 | - $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 202 | - file_put_contents($possibleBaseFileName, $str); |
|
| 203 | - @chmod($possibleBaseFileName, 0664); |
|
| 204 | - |
|
| 205 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\'.$className); |
|
| 206 | - if (empty($possibleFileNames)) { |
|
| 207 | - // @codeCoverageIgnoreStart |
|
| 208 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$className.'" is not autoloadable.'); |
|
| 209 | - // @codeCoverageIgnoreEnd |
|
| 210 | - } |
|
| 211 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 212 | - if (!file_exists($possibleFileName)) { |
|
| 213 | - $tableName = $table->getName(); |
|
| 214 | - $str = "<?php |
|
| 20 | + /** |
|
| 21 | + * @var SchemaAnalyzer |
|
| 22 | + */ |
|
| 23 | + private $schemaAnalyzer; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var Schema |
|
| 27 | + */ |
|
| 28 | + private $schema; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The root directory of the project. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + private $rootPath; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Name of composer file. |
|
| 39 | + * |
|
| 40 | + * @var string |
|
| 41 | + */ |
|
| 42 | + private $composerFile; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @var TDBMSchemaAnalyzer |
|
| 46 | + */ |
|
| 47 | + private $tdbmSchemaAnalyzer; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Constructor. |
|
| 51 | + * |
|
| 52 | + * @param SchemaAnalyzer $schemaAnalyzer |
|
| 53 | + * @param Schema $schema |
|
| 54 | + * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
|
| 55 | + */ |
|
| 56 | + public function __construct(SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
| 57 | + { |
|
| 58 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 59 | + $this->schema = $schema; |
|
| 60 | + $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
| 61 | + $this->rootPath = __DIR__.'/../../../../../../../../'; |
|
| 62 | + $this->composerFile = 'composer.json'; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Generates all the daos and beans. |
|
| 67 | + * |
|
| 68 | + * @param string $daoFactoryClassName The classe name of the DAO factory |
|
| 69 | + * @param string $daonamespace The namespace for the DAOs, without trailing \ |
|
| 70 | + * @param string $beannamespace The Namespace for the beans, without trailing \ |
|
| 71 | + * @param bool $storeInUtc If the generated daos should store the date in UTC timezone instead of user's timezone. |
|
| 72 | + * |
|
| 73 | + * @return \string[] the list of tables |
|
| 74 | + * |
|
| 75 | + * @throws TDBMException |
|
| 76 | + */ |
|
| 77 | + public function generateAllDaosAndBeans($daoFactoryClassName, $daonamespace, $beannamespace, $storeInUtc) |
|
| 78 | + { |
|
| 79 | + // TODO: extract ClassNameMapper in its own package! |
|
| 80 | + $classNameMapper = ClassNameMapper::createFromComposerFile($this->rootPath.$this->composerFile); |
|
| 81 | + // TODO: check that no class name ends with "Base". Otherwise, there will be name clash. |
|
| 82 | + |
|
| 83 | + $tableList = $this->schema->getTables(); |
|
| 84 | + |
|
| 85 | + // Remove all beans and daos from junction tables |
|
| 86 | + $junctionTables = $this->schemaAnalyzer->detectJunctionTables(); |
|
| 87 | + $junctionTableNames = array_map(function (Table $table) { |
|
| 88 | + return $table->getName(); |
|
| 89 | + }, $junctionTables); |
|
| 90 | + |
|
| 91 | + $tableList = array_filter($tableList, function (Table $table) use ($junctionTableNames) { |
|
| 92 | + return !in_array($table->getName(), $junctionTableNames); |
|
| 93 | + }); |
|
| 94 | + |
|
| 95 | + foreach ($tableList as $table) { |
|
| 96 | + $this->generateDaoAndBean($table, $daonamespace, $beannamespace, $classNameMapper, $storeInUtc); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $this->generateFactory($tableList, $daoFactoryClassName, $daonamespace, $classNameMapper); |
|
| 100 | + |
|
| 101 | + // Ok, let's return the list of all tables. |
|
| 102 | + // These will be used by the calling script to create Mouf instances. |
|
| 103 | + |
|
| 104 | + return array_map(function (Table $table) { return $table->getName(); }, $tableList); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Generates in one method call the daos and the beans for one table. |
|
| 109 | + * |
|
| 110 | + * @param Table $table |
|
| 111 | + * @param string $daonamespace |
|
| 112 | + * @param string $beannamespace |
|
| 113 | + * @param ClassNameMapper $classNameMapper |
|
| 114 | + * @param bool $storeInUtc |
|
| 115 | + * |
|
| 116 | + * @throws TDBMException |
|
| 117 | + */ |
|
| 118 | + public function generateDaoAndBean(Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc) |
|
| 119 | + { |
|
| 120 | + $tableName = $table->getName(); |
|
| 121 | + $daoName = $this->getDaoNameFromTableName($tableName); |
|
| 122 | + $beanName = $this->getBeanNameFromTableName($tableName); |
|
| 123 | + $baseBeanName = $this->getBaseBeanNameFromTableName($tableName); |
|
| 124 | + $baseDaoName = $this->getBaseDaoNameFromTableName($tableName); |
|
| 125 | + |
|
| 126 | + $beanDescriptor = new BeanDescriptor($table, $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer); |
|
| 127 | + $this->generateBean($beanDescriptor, $beanName, $baseBeanName, $table, $beannamespace, $classNameMapper, $storeInUtc); |
|
| 128 | + $this->generateDao($beanDescriptor, $daoName, $baseDaoName, $beanName, $table, $daonamespace, $beannamespace, $classNameMapper); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Returns the name of the bean class from the table name. |
|
| 133 | + * |
|
| 134 | + * @param $tableName |
|
| 135 | + * |
|
| 136 | + * @return string |
|
| 137 | + */ |
|
| 138 | + public static function getBeanNameFromTableName($tableName) |
|
| 139 | + { |
|
| 140 | + return self::toSingular(self::toCamelCase($tableName)).'Bean'; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Returns the name of the DAO class from the table name. |
|
| 145 | + * |
|
| 146 | + * @param $tableName |
|
| 147 | + * |
|
| 148 | + * @return string |
|
| 149 | + */ |
|
| 150 | + public static function getDaoNameFromTableName($tableName) |
|
| 151 | + { |
|
| 152 | + return self::toSingular(self::toCamelCase($tableName)).'Dao'; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Returns the name of the base bean class from the table name. |
|
| 157 | + * |
|
| 158 | + * @param $tableName |
|
| 159 | + * |
|
| 160 | + * @return string |
|
| 161 | + */ |
|
| 162 | + public static function getBaseBeanNameFromTableName($tableName) |
|
| 163 | + { |
|
| 164 | + return self::toSingular(self::toCamelCase($tableName)).'BaseBean'; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Returns the name of the base DAO class from the table name. |
|
| 169 | + * |
|
| 170 | + * @param $tableName |
|
| 171 | + * |
|
| 172 | + * @return string |
|
| 173 | + */ |
|
| 174 | + public static function getBaseDaoNameFromTableName($tableName) |
|
| 175 | + { |
|
| 176 | + return self::toSingular(self::toCamelCase($tableName)).'BaseDao'; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 181 | + * |
|
| 182 | + * @param BeanDescriptor $beanDescriptor |
|
| 183 | + * @param string $className The name of the class |
|
| 184 | + * @param string $baseClassName The name of the base class which will be extended (name only, no directory) |
|
| 185 | + * @param Table $table The table |
|
| 186 | + * @param string $beannamespace The namespace of the bean |
|
| 187 | + * @param ClassNameMapper $classNameMapper |
|
| 188 | + * |
|
| 189 | + * @throws TDBMException |
|
| 190 | + */ |
|
| 191 | + public function generateBean(BeanDescriptor $beanDescriptor, $className, $baseClassName, Table $table, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc) |
|
| 192 | + { |
|
| 193 | + $str = $beanDescriptor->generatePhpCode($beannamespace); |
|
| 194 | + |
|
| 195 | + $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\Generated\\'.$baseClassName); |
|
| 196 | + if (empty($possibleBaseFileNames)) { |
|
| 197 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$baseClassName.'" is not autoloadable.'); |
|
| 198 | + } |
|
| 199 | + $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 200 | + |
|
| 201 | + $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 202 | + file_put_contents($possibleBaseFileName, $str); |
|
| 203 | + @chmod($possibleBaseFileName, 0664); |
|
| 204 | + |
|
| 205 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\'.$className); |
|
| 206 | + if (empty($possibleFileNames)) { |
|
| 207 | + // @codeCoverageIgnoreStart |
|
| 208 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$className.'" is not autoloadable.'); |
|
| 209 | + // @codeCoverageIgnoreEnd |
|
| 210 | + } |
|
| 211 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 212 | + if (!file_exists($possibleFileName)) { |
|
| 213 | + $tableName = $table->getName(); |
|
| 214 | + $str = "<?php |
|
| 215 | 215 | /* |
| 216 | 216 | * This file has been automatically generated by TDBM. |
| 217 | 217 | * You can edit this file as it will not be overwritten. |
@@ -228,76 +228,76 @@ discard block |
||
| 228 | 228 | { |
| 229 | 229 | |
| 230 | 230 | }"; |
| 231 | - $this->ensureDirectoryExist($possibleFileName); |
|
| 232 | - file_put_contents($possibleFileName, $str); |
|
| 233 | - @chmod($possibleFileName, 0664); |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Tries to find a @defaultSort annotation in one of the columns. |
|
| 239 | - * |
|
| 240 | - * @param Table $table |
|
| 241 | - * |
|
| 242 | - * @return array First item: column name, Second item: column order (asc/desc) |
|
| 243 | - */ |
|
| 244 | - private function getDefaultSortColumnFromAnnotation(Table $table) |
|
| 245 | - { |
|
| 246 | - $defaultSort = null; |
|
| 247 | - $defaultSortDirection = null; |
|
| 248 | - foreach ($table->getColumns() as $column) { |
|
| 249 | - $comments = $column->getComment(); |
|
| 250 | - $matches = []; |
|
| 251 | - if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) { |
|
| 252 | - $defaultSort = $column->getName(); |
|
| 253 | - if (count($matches) === 3) { |
|
| 254 | - $defaultSortDirection = $matches[2]; |
|
| 255 | - } else { |
|
| 256 | - $defaultSortDirection = 'ASC'; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - return [$defaultSort, $defaultSortDirection]; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Writes the PHP bean DAO with simple functions to create/get/save objects. |
|
| 266 | - * |
|
| 267 | - * @param BeanDescriptor $beanDescriptor |
|
| 268 | - * @param string $className The name of the class |
|
| 269 | - * @param string $baseClassName |
|
| 270 | - * @param string $beanClassName |
|
| 271 | - * @param Table $table |
|
| 272 | - * @param string $daonamespace |
|
| 273 | - * @param string $beannamespace |
|
| 274 | - * @param ClassNameMapper $classNameMapper |
|
| 275 | - * |
|
| 276 | - * @throws TDBMException |
|
| 277 | - */ |
|
| 278 | - public function generateDao(BeanDescriptor $beanDescriptor, $className, $baseClassName, $beanClassName, Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper) |
|
| 279 | - { |
|
| 280 | - $tableName = $table->getName(); |
|
| 281 | - $primaryKeyColumns = $table->getPrimaryKeyColumns(); |
|
| 282 | - |
|
| 283 | - list($defaultSort, $defaultSortDirection) = $this->getDefaultSortColumnFromAnnotation($table); |
|
| 284 | - |
|
| 285 | - // FIXME: lowercase tables with _ in the name should work! |
|
| 286 | - $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
|
| 287 | - |
|
| 288 | - $beanClassWithoutNameSpace = $beanClassName; |
|
| 289 | - $beanClassName = $beannamespace.'\\'.$beanClassName; |
|
| 290 | - |
|
| 291 | - list($usedBeans, $findByDaoCode) = $beanDescriptor->generateFindByDaoCode($beannamespace, $beanClassWithoutNameSpace); |
|
| 292 | - |
|
| 293 | - $usedBeans[] = $beanClassName; |
|
| 294 | - // Let's suppress duplicates in used beans (if any) |
|
| 295 | - $usedBeans = array_flip(array_flip($usedBeans)); |
|
| 296 | - $useStatements = array_map(function ($usedBean) { |
|
| 297 | - return "use $usedBean;\n"; |
|
| 298 | - }, $usedBeans); |
|
| 299 | - |
|
| 300 | - $str = "<?php |
|
| 231 | + $this->ensureDirectoryExist($possibleFileName); |
|
| 232 | + file_put_contents($possibleFileName, $str); |
|
| 233 | + @chmod($possibleFileName, 0664); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Tries to find a @defaultSort annotation in one of the columns. |
|
| 239 | + * |
|
| 240 | + * @param Table $table |
|
| 241 | + * |
|
| 242 | + * @return array First item: column name, Second item: column order (asc/desc) |
|
| 243 | + */ |
|
| 244 | + private function getDefaultSortColumnFromAnnotation(Table $table) |
|
| 245 | + { |
|
| 246 | + $defaultSort = null; |
|
| 247 | + $defaultSortDirection = null; |
|
| 248 | + foreach ($table->getColumns() as $column) { |
|
| 249 | + $comments = $column->getComment(); |
|
| 250 | + $matches = []; |
|
| 251 | + if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) { |
|
| 252 | + $defaultSort = $column->getName(); |
|
| 253 | + if (count($matches) === 3) { |
|
| 254 | + $defaultSortDirection = $matches[2]; |
|
| 255 | + } else { |
|
| 256 | + $defaultSortDirection = 'ASC'; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + return [$defaultSort, $defaultSortDirection]; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Writes the PHP bean DAO with simple functions to create/get/save objects. |
|
| 266 | + * |
|
| 267 | + * @param BeanDescriptor $beanDescriptor |
|
| 268 | + * @param string $className The name of the class |
|
| 269 | + * @param string $baseClassName |
|
| 270 | + * @param string $beanClassName |
|
| 271 | + * @param Table $table |
|
| 272 | + * @param string $daonamespace |
|
| 273 | + * @param string $beannamespace |
|
| 274 | + * @param ClassNameMapper $classNameMapper |
|
| 275 | + * |
|
| 276 | + * @throws TDBMException |
|
| 277 | + */ |
|
| 278 | + public function generateDao(BeanDescriptor $beanDescriptor, $className, $baseClassName, $beanClassName, Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper) |
|
| 279 | + { |
|
| 280 | + $tableName = $table->getName(); |
|
| 281 | + $primaryKeyColumns = $table->getPrimaryKeyColumns(); |
|
| 282 | + |
|
| 283 | + list($defaultSort, $defaultSortDirection) = $this->getDefaultSortColumnFromAnnotation($table); |
|
| 284 | + |
|
| 285 | + // FIXME: lowercase tables with _ in the name should work! |
|
| 286 | + $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
|
| 287 | + |
|
| 288 | + $beanClassWithoutNameSpace = $beanClassName; |
|
| 289 | + $beanClassName = $beannamespace.'\\'.$beanClassName; |
|
| 290 | + |
|
| 291 | + list($usedBeans, $findByDaoCode) = $beanDescriptor->generateFindByDaoCode($beannamespace, $beanClassWithoutNameSpace); |
|
| 292 | + |
|
| 293 | + $usedBeans[] = $beanClassName; |
|
| 294 | + // Let's suppress duplicates in used beans (if any) |
|
| 295 | + $usedBeans = array_flip(array_flip($usedBeans)); |
|
| 296 | + $useStatements = array_map(function ($usedBean) { |
|
| 297 | + return "use $usedBean;\n"; |
|
| 298 | + }, $usedBeans); |
|
| 299 | + |
|
| 300 | + $str = "<?php |
|
| 301 | 301 | |
| 302 | 302 | /* |
| 303 | 303 | * This file has been automatically generated by TDBM. |
@@ -374,9 +374,9 @@ discard block |
||
| 374 | 374 | } |
| 375 | 375 | "; |
| 376 | 376 | |
| 377 | - if (count($primaryKeyColumns) === 1) { |
|
| 378 | - $primaryKeyColumn = $primaryKeyColumns[0]; |
|
| 379 | - $str .= " |
|
| 377 | + if (count($primaryKeyColumns) === 1) { |
|
| 378 | + $primaryKeyColumn = $primaryKeyColumns[0]; |
|
| 379 | + $str .= " |
|
| 380 | 380 | /** |
| 381 | 381 | * Get $beanClassWithoutNameSpace specified by its ID (its primary key) |
| 382 | 382 | * If the primary key does not exist, an exception is thrown. |
@@ -391,8 +391,8 @@ discard block |
||
| 391 | 391 | return \$this->tdbmService->findObjectByPk('$tableName', ['$primaryKeyColumn' => \$id], [], \$lazyLoading); |
| 392 | 392 | } |
| 393 | 393 | "; |
| 394 | - } |
|
| 395 | - $str .= " |
|
| 394 | + } |
|
| 395 | + $str .= " |
|
| 396 | 396 | /** |
| 397 | 397 | * Deletes the $beanClassWithoutNameSpace passed in parameter. |
| 398 | 398 | * |
@@ -450,33 +450,33 @@ discard block |
||
| 450 | 450 | } |
| 451 | 451 | "; |
| 452 | 452 | |
| 453 | - $str .= $findByDaoCode; |
|
| 454 | - $str .= '} |
|
| 453 | + $str .= $findByDaoCode; |
|
| 454 | + $str .= '} |
|
| 455 | 455 | '; |
| 456 | 456 | |
| 457 | - $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\Generated\\'.$baseClassName); |
|
| 458 | - if (empty($possibleBaseFileNames)) { |
|
| 459 | - // @codeCoverageIgnoreStart |
|
| 460 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$baseClassName.'" is not autoloadable.'); |
|
| 461 | - // @codeCoverageIgnoreEnd |
|
| 462 | - } |
|
| 463 | - $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 464 | - |
|
| 465 | - $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 466 | - file_put_contents($possibleBaseFileName, $str); |
|
| 467 | - @chmod($possibleBaseFileName, 0664); |
|
| 468 | - |
|
| 469 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\'.$className); |
|
| 470 | - if (empty($possibleFileNames)) { |
|
| 471 | - // @codeCoverageIgnoreStart |
|
| 472 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$className.'" is not autoloadable.'); |
|
| 473 | - // @codeCoverageIgnoreEnd |
|
| 474 | - } |
|
| 475 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 476 | - |
|
| 477 | - // Now, let's generate the "editable" class |
|
| 478 | - if (!file_exists($possibleFileName)) { |
|
| 479 | - $str = "<?php |
|
| 457 | + $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\Generated\\'.$baseClassName); |
|
| 458 | + if (empty($possibleBaseFileNames)) { |
|
| 459 | + // @codeCoverageIgnoreStart |
|
| 460 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$baseClassName.'" is not autoloadable.'); |
|
| 461 | + // @codeCoverageIgnoreEnd |
|
| 462 | + } |
|
| 463 | + $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 464 | + |
|
| 465 | + $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 466 | + file_put_contents($possibleBaseFileName, $str); |
|
| 467 | + @chmod($possibleBaseFileName, 0664); |
|
| 468 | + |
|
| 469 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\'.$className); |
|
| 470 | + if (empty($possibleFileNames)) { |
|
| 471 | + // @codeCoverageIgnoreStart |
|
| 472 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$className.'" is not autoloadable.'); |
|
| 473 | + // @codeCoverageIgnoreEnd |
|
| 474 | + } |
|
| 475 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 476 | + |
|
| 477 | + // Now, let's generate the "editable" class |
|
| 478 | + if (!file_exists($possibleFileName)) { |
|
| 479 | + $str = "<?php |
|
| 480 | 480 | |
| 481 | 481 | /* |
| 482 | 482 | * This file has been automatically generated by TDBM. |
@@ -495,22 +495,22 @@ discard block |
||
| 495 | 495 | |
| 496 | 496 | } |
| 497 | 497 | "; |
| 498 | - $this->ensureDirectoryExist($possibleFileName); |
|
| 499 | - file_put_contents($possibleFileName, $str); |
|
| 500 | - @chmod($possibleFileName, 0664); |
|
| 501 | - } |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Generates the factory bean. |
|
| 506 | - * |
|
| 507 | - * @param Table[] $tableList |
|
| 508 | - */ |
|
| 509 | - private function generateFactory(array $tableList, $daoFactoryClassName, $daoNamespace, ClassNameMapper $classNameMapper) |
|
| 510 | - { |
|
| 511 | - // For each table, let's write a property. |
|
| 512 | - |
|
| 513 | - $str = "<?php |
|
| 498 | + $this->ensureDirectoryExist($possibleFileName); |
|
| 499 | + file_put_contents($possibleFileName, $str); |
|
| 500 | + @chmod($possibleFileName, 0664); |
|
| 501 | + } |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Generates the factory bean. |
|
| 506 | + * |
|
| 507 | + * @param Table[] $tableList |
|
| 508 | + */ |
|
| 509 | + private function generateFactory(array $tableList, $daoFactoryClassName, $daoNamespace, ClassNameMapper $classNameMapper) |
|
| 510 | + { |
|
| 511 | + // For each table, let's write a property. |
|
| 512 | + |
|
| 513 | + $str = "<?php |
|
| 514 | 514 | |
| 515 | 515 | /* |
| 516 | 516 | * This file has been automatically generated by TDBM. |
@@ -519,13 +519,13 @@ discard block |
||
| 519 | 519 | |
| 520 | 520 | namespace {$daoNamespace}\\Generated; |
| 521 | 521 | "; |
| 522 | - foreach ($tableList as $table) { |
|
| 523 | - $tableName = $table->getName(); |
|
| 524 | - $daoClassName = $this->getDaoNameFromTableName($tableName); |
|
| 525 | - $str .= "use {$daoNamespace}\\".$daoClassName.";\n"; |
|
| 526 | - } |
|
| 522 | + foreach ($tableList as $table) { |
|
| 523 | + $tableName = $table->getName(); |
|
| 524 | + $daoClassName = $this->getDaoNameFromTableName($tableName); |
|
| 525 | + $str .= "use {$daoNamespace}\\".$daoClassName.";\n"; |
|
| 526 | + } |
|
| 527 | 527 | |
| 528 | - $str .= " |
|
| 528 | + $str .= " |
|
| 529 | 529 | /** |
| 530 | 530 | * The $daoFactoryClassName provides an easy access to all DAOs generated by TDBM. |
| 531 | 531 | * |
@@ -534,12 +534,12 @@ discard block |
||
| 534 | 534 | { |
| 535 | 535 | "; |
| 536 | 536 | |
| 537 | - foreach ($tableList as $table) { |
|
| 538 | - $tableName = $table->getName(); |
|
| 539 | - $daoClassName = $this->getDaoNameFromTableName($tableName); |
|
| 540 | - $daoInstanceName = self::toVariableName($daoClassName); |
|
| 537 | + foreach ($tableList as $table) { |
|
| 538 | + $tableName = $table->getName(); |
|
| 539 | + $daoClassName = $this->getDaoNameFromTableName($tableName); |
|
| 540 | + $daoInstanceName = self::toVariableName($daoClassName); |
|
| 541 | 541 | |
| 542 | - $str .= ' /** |
|
| 542 | + $str .= ' /** |
|
| 543 | 543 | * @var '.$daoClassName.' |
| 544 | 544 | */ |
| 545 | 545 | private $'.$daoInstanceName.'; |
@@ -564,158 +564,158 @@ discard block |
||
| 564 | 564 | } |
| 565 | 565 | |
| 566 | 566 | '; |
| 567 | - } |
|
| 567 | + } |
|
| 568 | 568 | |
| 569 | - $str .= ' |
|
| 569 | + $str .= ' |
|
| 570 | 570 | } |
| 571 | 571 | '; |
| 572 | 572 | |
| 573 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($daoNamespace.'\\Generated\\'.$daoFactoryClassName); |
|
| 574 | - if (empty($possibleFileNames)) { |
|
| 575 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daoNamespace.'\\'.$daoFactoryClassName.'" is not autoloadable.'); |
|
| 576 | - } |
|
| 577 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 578 | - |
|
| 579 | - $this->ensureDirectoryExist($possibleFileName); |
|
| 580 | - file_put_contents($possibleFileName, $str); |
|
| 581 | - @chmod($possibleFileName, 0664); |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - /** |
|
| 585 | - * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
| 586 | - * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
| 587 | - * |
|
| 588 | - * @param $str string |
|
| 589 | - * |
|
| 590 | - * @return string |
|
| 591 | - */ |
|
| 592 | - public static function toCamelCase($str) |
|
| 593 | - { |
|
| 594 | - $str = strtoupper(substr($str, 0, 1)).substr($str, 1); |
|
| 595 | - while (true) { |
|
| 596 | - if (strpos($str, '_') === false && strpos($str, ' ') === false) { |
|
| 597 | - break; |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - $pos = strpos($str, '_'); |
|
| 601 | - if ($pos === false) { |
|
| 602 | - $pos = strpos($str, ' '); |
|
| 603 | - } |
|
| 604 | - $before = substr($str, 0, $pos); |
|
| 605 | - $after = substr($str, $pos + 1); |
|
| 606 | - $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - return $str; |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - /** |
|
| 613 | - * Tries to put string to the singular form (if it is plural). |
|
| 614 | - * We assume the table names are in english. |
|
| 615 | - * |
|
| 616 | - * @param $str string |
|
| 617 | - * |
|
| 618 | - * @return string |
|
| 619 | - */ |
|
| 620 | - public static function toSingular($str) |
|
| 621 | - { |
|
| 622 | - return Inflector::singularize($str); |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - /** |
|
| 626 | - * Put the first letter of the string in lower case. |
|
| 627 | - * Very useful to transform a class name into a variable name. |
|
| 628 | - * |
|
| 629 | - * @param $str string |
|
| 630 | - * |
|
| 631 | - * @return string |
|
| 632 | - */ |
|
| 633 | - public static function toVariableName($str) |
|
| 634 | - { |
|
| 635 | - return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * Ensures the file passed in parameter can be written in its directory. |
|
| 640 | - * |
|
| 641 | - * @param string $fileName |
|
| 642 | - * |
|
| 643 | - * @throws TDBMException |
|
| 644 | - */ |
|
| 645 | - private function ensureDirectoryExist($fileName) |
|
| 646 | - { |
|
| 647 | - $dirName = dirname($fileName); |
|
| 648 | - if (!file_exists($dirName)) { |
|
| 649 | - $old = umask(0); |
|
| 650 | - $result = mkdir($dirName, 0775, true); |
|
| 651 | - umask($old); |
|
| 652 | - if ($result === false) { |
|
| 653 | - throw new TDBMException("Unable to create directory: '".$dirName."'."); |
|
| 654 | - } |
|
| 655 | - } |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * Absolute path to composer json file. |
|
| 660 | - * |
|
| 661 | - * @param string $composerFile |
|
| 662 | - */ |
|
| 663 | - public function setComposerFile($composerFile) |
|
| 664 | - { |
|
| 665 | - $this->rootPath = dirname($composerFile).'/'; |
|
| 666 | - $this->composerFile = basename($composerFile); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - /** |
|
| 670 | - * Transforms a DBAL type into a PHP type (for PHPDoc purpose). |
|
| 671 | - * |
|
| 672 | - * @param Type $type The DBAL type |
|
| 673 | - * |
|
| 674 | - * @return string The PHP type |
|
| 675 | - */ |
|
| 676 | - public static function dbalTypeToPhpType(Type $type) |
|
| 677 | - { |
|
| 678 | - $map = [ |
|
| 679 | - Type::TARRAY => 'array', |
|
| 680 | - Type::SIMPLE_ARRAY => 'array', |
|
| 681 | - Type::JSON_ARRAY => 'array', |
|
| 682 | - Type::BIGINT => 'string', |
|
| 683 | - Type::BOOLEAN => 'bool', |
|
| 684 | - Type::DATETIME => '\DateTimeInterface', |
|
| 685 | - Type::DATETIMETZ => '\DateTimeInterface', |
|
| 686 | - Type::DATE => '\DateTimeInterface', |
|
| 687 | - Type::TIME => '\DateTimeInterface', |
|
| 688 | - Type::DECIMAL => 'float', |
|
| 689 | - Type::INTEGER => 'int', |
|
| 690 | - Type::OBJECT => 'string', |
|
| 691 | - Type::SMALLINT => 'int', |
|
| 692 | - Type::STRING => 'string', |
|
| 693 | - Type::TEXT => 'string', |
|
| 694 | - Type::BINARY => 'string', |
|
| 695 | - Type::BLOB => 'string', |
|
| 696 | - Type::FLOAT => 'float', |
|
| 697 | - Type::GUID => 'string', |
|
| 698 | - ]; |
|
| 699 | - |
|
| 700 | - return isset($map[$type->getName()]) ? $map[$type->getName()] : $type->getName(); |
|
| 701 | - } |
|
| 702 | - |
|
| 703 | - /** |
|
| 704 | - * @param string $beanNamespace |
|
| 705 | - * |
|
| 706 | - * @return \string[] Returns a map mapping table name to beans name |
|
| 707 | - */ |
|
| 708 | - public function buildTableToBeanMap($beanNamespace) |
|
| 709 | - { |
|
| 710 | - $tableToBeanMap = []; |
|
| 711 | - |
|
| 712 | - $tables = $this->schema->getTables(); |
|
| 713 | - |
|
| 714 | - foreach ($tables as $table) { |
|
| 715 | - $tableName = $table->getName(); |
|
| 716 | - $tableToBeanMap[$tableName] = $beanNamespace.'\\'.self::getBeanNameFromTableName($tableName); |
|
| 717 | - } |
|
| 718 | - |
|
| 719 | - return $tableToBeanMap; |
|
| 720 | - } |
|
| 573 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($daoNamespace.'\\Generated\\'.$daoFactoryClassName); |
|
| 574 | + if (empty($possibleFileNames)) { |
|
| 575 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daoNamespace.'\\'.$daoFactoryClassName.'" is not autoloadable.'); |
|
| 576 | + } |
|
| 577 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 578 | + |
|
| 579 | + $this->ensureDirectoryExist($possibleFileName); |
|
| 580 | + file_put_contents($possibleFileName, $str); |
|
| 581 | + @chmod($possibleFileName, 0664); |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + /** |
|
| 585 | + * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
| 586 | + * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
| 587 | + * |
|
| 588 | + * @param $str string |
|
| 589 | + * |
|
| 590 | + * @return string |
|
| 591 | + */ |
|
| 592 | + public static function toCamelCase($str) |
|
| 593 | + { |
|
| 594 | + $str = strtoupper(substr($str, 0, 1)).substr($str, 1); |
|
| 595 | + while (true) { |
|
| 596 | + if (strpos($str, '_') === false && strpos($str, ' ') === false) { |
|
| 597 | + break; |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + $pos = strpos($str, '_'); |
|
| 601 | + if ($pos === false) { |
|
| 602 | + $pos = strpos($str, ' '); |
|
| 603 | + } |
|
| 604 | + $before = substr($str, 0, $pos); |
|
| 605 | + $after = substr($str, $pos + 1); |
|
| 606 | + $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + return $str; |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + /** |
|
| 613 | + * Tries to put string to the singular form (if it is plural). |
|
| 614 | + * We assume the table names are in english. |
|
| 615 | + * |
|
| 616 | + * @param $str string |
|
| 617 | + * |
|
| 618 | + * @return string |
|
| 619 | + */ |
|
| 620 | + public static function toSingular($str) |
|
| 621 | + { |
|
| 622 | + return Inflector::singularize($str); |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + /** |
|
| 626 | + * Put the first letter of the string in lower case. |
|
| 627 | + * Very useful to transform a class name into a variable name. |
|
| 628 | + * |
|
| 629 | + * @param $str string |
|
| 630 | + * |
|
| 631 | + * @return string |
|
| 632 | + */ |
|
| 633 | + public static function toVariableName($str) |
|
| 634 | + { |
|
| 635 | + return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * Ensures the file passed in parameter can be written in its directory. |
|
| 640 | + * |
|
| 641 | + * @param string $fileName |
|
| 642 | + * |
|
| 643 | + * @throws TDBMException |
|
| 644 | + */ |
|
| 645 | + private function ensureDirectoryExist($fileName) |
|
| 646 | + { |
|
| 647 | + $dirName = dirname($fileName); |
|
| 648 | + if (!file_exists($dirName)) { |
|
| 649 | + $old = umask(0); |
|
| 650 | + $result = mkdir($dirName, 0775, true); |
|
| 651 | + umask($old); |
|
| 652 | + if ($result === false) { |
|
| 653 | + throw new TDBMException("Unable to create directory: '".$dirName."'."); |
|
| 654 | + } |
|
| 655 | + } |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + /** |
|
| 659 | + * Absolute path to composer json file. |
|
| 660 | + * |
|
| 661 | + * @param string $composerFile |
|
| 662 | + */ |
|
| 663 | + public function setComposerFile($composerFile) |
|
| 664 | + { |
|
| 665 | + $this->rootPath = dirname($composerFile).'/'; |
|
| 666 | + $this->composerFile = basename($composerFile); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + /** |
|
| 670 | + * Transforms a DBAL type into a PHP type (for PHPDoc purpose). |
|
| 671 | + * |
|
| 672 | + * @param Type $type The DBAL type |
|
| 673 | + * |
|
| 674 | + * @return string The PHP type |
|
| 675 | + */ |
|
| 676 | + public static function dbalTypeToPhpType(Type $type) |
|
| 677 | + { |
|
| 678 | + $map = [ |
|
| 679 | + Type::TARRAY => 'array', |
|
| 680 | + Type::SIMPLE_ARRAY => 'array', |
|
| 681 | + Type::JSON_ARRAY => 'array', |
|
| 682 | + Type::BIGINT => 'string', |
|
| 683 | + Type::BOOLEAN => 'bool', |
|
| 684 | + Type::DATETIME => '\DateTimeInterface', |
|
| 685 | + Type::DATETIMETZ => '\DateTimeInterface', |
|
| 686 | + Type::DATE => '\DateTimeInterface', |
|
| 687 | + Type::TIME => '\DateTimeInterface', |
|
| 688 | + Type::DECIMAL => 'float', |
|
| 689 | + Type::INTEGER => 'int', |
|
| 690 | + Type::OBJECT => 'string', |
|
| 691 | + Type::SMALLINT => 'int', |
|
| 692 | + Type::STRING => 'string', |
|
| 693 | + Type::TEXT => 'string', |
|
| 694 | + Type::BINARY => 'string', |
|
| 695 | + Type::BLOB => 'string', |
|
| 696 | + Type::FLOAT => 'float', |
|
| 697 | + Type::GUID => 'string', |
|
| 698 | + ]; |
|
| 699 | + |
|
| 700 | + return isset($map[$type->getName()]) ? $map[$type->getName()] : $type->getName(); |
|
| 701 | + } |
|
| 702 | + |
|
| 703 | + /** |
|
| 704 | + * @param string $beanNamespace |
|
| 705 | + * |
|
| 706 | + * @return \string[] Returns a map mapping table name to beans name |
|
| 707 | + */ |
|
| 708 | + public function buildTableToBeanMap($beanNamespace) |
|
| 709 | + { |
|
| 710 | + $tableToBeanMap = []; |
|
| 711 | + |
|
| 712 | + $tables = $this->schema->getTables(); |
|
| 713 | + |
|
| 714 | + foreach ($tables as $table) { |
|
| 715 | + $tableName = $table->getName(); |
|
| 716 | + $tableToBeanMap[$tableName] = $beanNamespace.'\\'.self::getBeanNameFromTableName($tableName); |
|
| 717 | + } |
|
| 718 | + |
|
| 719 | + return $tableToBeanMap; |
|
| 720 | + } |
|
| 721 | 721 | } |
@@ -181,6 +181,9 @@ |
||
| 181 | 181 | |
| 182 | 182 | protected $errorMsg; |
| 183 | 183 | |
| 184 | + /** |
|
| 185 | + * @param string $msg |
|
| 186 | + */ |
|
| 184 | 187 | private function displayErrorMsg($msg) |
| 185 | 188 | { |
| 186 | 189 | $this->errorMsg = $msg; |
@@ -15,172 +15,172 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class TdbmInstallController extends Controller |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * @var HtmlBlock |
|
| 20 | - */ |
|
| 21 | - public $content; |
|
| 22 | - |
|
| 23 | - public $selfedit; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * The active MoufManager to be edited/viewed. |
|
| 27 | - * |
|
| 28 | - * @var MoufManager |
|
| 29 | - */ |
|
| 30 | - public $moufManager; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * The template used by the main page for mouf. |
|
| 34 | - * |
|
| 35 | - * @Property |
|
| 36 | - * @Compulsory |
|
| 37 | - * |
|
| 38 | - * @var TemplateInterface |
|
| 39 | - */ |
|
| 40 | - public $template; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Displays the first install screen. |
|
| 44 | - * |
|
| 45 | - * @Action |
|
| 46 | - * @Logged |
|
| 47 | - * |
|
| 48 | - * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only) |
|
| 49 | - */ |
|
| 50 | - public function defaultAction($selfedit = 'false') |
|
| 51 | - { |
|
| 52 | - $this->selfedit = $selfedit; |
|
| 53 | - |
|
| 54 | - if ($selfedit == 'true') { |
|
| 55 | - $this->moufManager = MoufManager::getMoufManager(); |
|
| 56 | - } else { |
|
| 57 | - $this->moufManager = MoufManager::getMoufManagerHiddenInstance(); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - $this->content->addFile(dirname(__FILE__).'/../../../../views/installStep1.php', $this); |
|
| 61 | - $this->template->toHtml(); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Skips the install process. |
|
| 66 | - * |
|
| 67 | - * @Action |
|
| 68 | - * @Logged |
|
| 69 | - * |
|
| 70 | - * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only) |
|
| 71 | - */ |
|
| 72 | - public function skip($selfedit = 'false') |
|
| 73 | - { |
|
| 74 | - InstallUtils::continueInstall($selfedit == 'true'); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - protected $daoNamespace; |
|
| 78 | - protected $beanNamespace; |
|
| 79 | - protected $autoloadDetected; |
|
| 80 | - protected $storeInUtc; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Displays the second install screen. |
|
| 84 | - * |
|
| 85 | - * @Action |
|
| 86 | - * @Logged |
|
| 87 | - * |
|
| 88 | - * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only) |
|
| 89 | - */ |
|
| 90 | - public function configure($selfedit = 'false') |
|
| 91 | - { |
|
| 92 | - $this->selfedit = $selfedit; |
|
| 93 | - |
|
| 94 | - if ($selfedit == 'true') { |
|
| 95 | - $this->moufManager = MoufManager::getMoufManager(); |
|
| 96 | - } else { |
|
| 97 | - $this->moufManager = MoufManager::getMoufManagerHiddenInstance(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - // Let's start by performing basic checks about the instances we assume to exist. |
|
| 101 | - if (!$this->moufManager->instanceExists('dbalConnection')) { |
|
| 102 | - $this->displayErrorMsg("The TDBM install process assumes your database connection instance is already created, and that the name of this instance is 'dbalConnection'. Could not find the 'dbalConnection' instance."); |
|
| 103 | - |
|
| 104 | - return; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace_tdbmService'); |
|
| 108 | - $this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace_tdbmService'); |
|
| 109 | - |
|
| 110 | - if ($this->daoNamespace == null && $this->beanNamespace == null) { |
|
| 111 | - $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json'); |
|
| 112 | - |
|
| 113 | - $autoloadNamespaces = $classNameMapper->getManagedNamespaces(); |
|
| 114 | - if ($autoloadNamespaces) { |
|
| 115 | - $this->autoloadDetected = true; |
|
| 116 | - $rootNamespace = $autoloadNamespaces[0]; |
|
| 117 | - $this->daoNamespace = $rootNamespace.'Model\\Dao'; |
|
| 118 | - $this->beanNamespace = $rootNamespace.'Model\\Bean'; |
|
| 119 | - } else { |
|
| 120 | - $this->autoloadDetected = false; |
|
| 121 | - $this->daoNamespace = 'YourApplication\\Model\\Dao'; |
|
| 122 | - $this->beanNamespace = 'YourApplication\\Model\\Bean'; |
|
| 123 | - } |
|
| 124 | - } else { |
|
| 125 | - $this->autoloadDetected = true; |
|
| 126 | - } |
|
| 127 | - $this->defaultPath = true; |
|
| 128 | - $this->storePath = ''; |
|
| 129 | - |
|
| 130 | - $this->castDatesToDateTime = true; |
|
| 131 | - |
|
| 132 | - $this->content->addFile(dirname(__FILE__).'/../../../../views/installStep2.php', $this); |
|
| 133 | - $this->template->toHtml(); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * This action generates the TDBM instance, then the DAOs and Beans. |
|
| 138 | - * |
|
| 139 | - * @Action |
|
| 140 | - * |
|
| 141 | - * @param string $daonamespace |
|
| 142 | - * @param string $beannamespace |
|
| 143 | - * @param int $storeInUtc |
|
| 144 | - * @param string $selfedit |
|
| 145 | - * |
|
| 146 | - * @throws \Mouf\MoufException |
|
| 147 | - */ |
|
| 148 | - public function generate($daonamespace, $beannamespace, $storeInUtc = 0, $selfedit = 'false', $defaultPath = false, $storePath = '') |
|
| 149 | - { |
|
| 150 | - $this->selfedit = $selfedit; |
|
| 151 | - |
|
| 152 | - if ($selfedit == 'true') { |
|
| 153 | - $this->moufManager = MoufManager::getMoufManager(); |
|
| 154 | - } else { |
|
| 155 | - $this->moufManager = MoufManager::getMoufManagerHiddenInstance(); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - if (!$this->moufManager->instanceExists('doctrineApcCache')) { |
|
| 159 | - $doctrineApcCache = $this->moufManager->createInstance('Doctrine\\Common\\Cache\\ApcCache')->setName('doctrineApcCache'); |
|
| 160 | - // TODO: set namespace |
|
| 161 | - } else { |
|
| 162 | - $doctrineApcCache = $this->moufManager->getInstanceDescriptor('doctrineApcCache'); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - if (!$this->moufManager->instanceExists('tdbmService')) { |
|
| 166 | - $tdbmService = $this->moufManager->createInstance('Mouf\\Database\\TDBM\\TDBMService')->setName('tdbmService'); |
|
| 167 | - $tdbmService->getConstructorArgumentProperty('connection')->setValue($this->moufManager->getInstanceDescriptor('dbalConnection')); |
|
| 168 | - $tdbmService->getConstructorArgumentProperty('cache')->setValue($doctrineApcCache); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - $this->moufManager->rewriteMouf(); |
|
| 172 | - |
|
| 173 | - TdbmController::generateDaos($this->moufManager, 'tdbmService', $daonamespace, $beannamespace, 'DaoFactory', 'daoFactory', $selfedit, $storeInUtc, $defaultPath, $storePath); |
|
| 174 | - |
|
| 175 | - InstallUtils::continueInstall($selfedit == 'true'); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - protected $errorMsg; |
|
| 179 | - |
|
| 180 | - private function displayErrorMsg($msg) |
|
| 181 | - { |
|
| 182 | - $this->errorMsg = $msg; |
|
| 183 | - $this->content->addFile(dirname(__FILE__).'/../../../../views/installError.php', $this); |
|
| 184 | - $this->template->toHtml(); |
|
| 185 | - } |
|
| 18 | + /** |
|
| 19 | + * @var HtmlBlock |
|
| 20 | + */ |
|
| 21 | + public $content; |
|
| 22 | + |
|
| 23 | + public $selfedit; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * The active MoufManager to be edited/viewed. |
|
| 27 | + * |
|
| 28 | + * @var MoufManager |
|
| 29 | + */ |
|
| 30 | + public $moufManager; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * The template used by the main page for mouf. |
|
| 34 | + * |
|
| 35 | + * @Property |
|
| 36 | + * @Compulsory |
|
| 37 | + * |
|
| 38 | + * @var TemplateInterface |
|
| 39 | + */ |
|
| 40 | + public $template; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Displays the first install screen. |
|
| 44 | + * |
|
| 45 | + * @Action |
|
| 46 | + * @Logged |
|
| 47 | + * |
|
| 48 | + * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only) |
|
| 49 | + */ |
|
| 50 | + public function defaultAction($selfedit = 'false') |
|
| 51 | + { |
|
| 52 | + $this->selfedit = $selfedit; |
|
| 53 | + |
|
| 54 | + if ($selfedit == 'true') { |
|
| 55 | + $this->moufManager = MoufManager::getMoufManager(); |
|
| 56 | + } else { |
|
| 57 | + $this->moufManager = MoufManager::getMoufManagerHiddenInstance(); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + $this->content->addFile(dirname(__FILE__).'/../../../../views/installStep1.php', $this); |
|
| 61 | + $this->template->toHtml(); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Skips the install process. |
|
| 66 | + * |
|
| 67 | + * @Action |
|
| 68 | + * @Logged |
|
| 69 | + * |
|
| 70 | + * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only) |
|
| 71 | + */ |
|
| 72 | + public function skip($selfedit = 'false') |
|
| 73 | + { |
|
| 74 | + InstallUtils::continueInstall($selfedit == 'true'); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + protected $daoNamespace; |
|
| 78 | + protected $beanNamespace; |
|
| 79 | + protected $autoloadDetected; |
|
| 80 | + protected $storeInUtc; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Displays the second install screen. |
|
| 84 | + * |
|
| 85 | + * @Action |
|
| 86 | + * @Logged |
|
| 87 | + * |
|
| 88 | + * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only) |
|
| 89 | + */ |
|
| 90 | + public function configure($selfedit = 'false') |
|
| 91 | + { |
|
| 92 | + $this->selfedit = $selfedit; |
|
| 93 | + |
|
| 94 | + if ($selfedit == 'true') { |
|
| 95 | + $this->moufManager = MoufManager::getMoufManager(); |
|
| 96 | + } else { |
|
| 97 | + $this->moufManager = MoufManager::getMoufManagerHiddenInstance(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + // Let's start by performing basic checks about the instances we assume to exist. |
|
| 101 | + if (!$this->moufManager->instanceExists('dbalConnection')) { |
|
| 102 | + $this->displayErrorMsg("The TDBM install process assumes your database connection instance is already created, and that the name of this instance is 'dbalConnection'. Could not find the 'dbalConnection' instance."); |
|
| 103 | + |
|
| 104 | + return; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace_tdbmService'); |
|
| 108 | + $this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace_tdbmService'); |
|
| 109 | + |
|
| 110 | + if ($this->daoNamespace == null && $this->beanNamespace == null) { |
|
| 111 | + $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json'); |
|
| 112 | + |
|
| 113 | + $autoloadNamespaces = $classNameMapper->getManagedNamespaces(); |
|
| 114 | + if ($autoloadNamespaces) { |
|
| 115 | + $this->autoloadDetected = true; |
|
| 116 | + $rootNamespace = $autoloadNamespaces[0]; |
|
| 117 | + $this->daoNamespace = $rootNamespace.'Model\\Dao'; |
|
| 118 | + $this->beanNamespace = $rootNamespace.'Model\\Bean'; |
|
| 119 | + } else { |
|
| 120 | + $this->autoloadDetected = false; |
|
| 121 | + $this->daoNamespace = 'YourApplication\\Model\\Dao'; |
|
| 122 | + $this->beanNamespace = 'YourApplication\\Model\\Bean'; |
|
| 123 | + } |
|
| 124 | + } else { |
|
| 125 | + $this->autoloadDetected = true; |
|
| 126 | + } |
|
| 127 | + $this->defaultPath = true; |
|
| 128 | + $this->storePath = ''; |
|
| 129 | + |
|
| 130 | + $this->castDatesToDateTime = true; |
|
| 131 | + |
|
| 132 | + $this->content->addFile(dirname(__FILE__).'/../../../../views/installStep2.php', $this); |
|
| 133 | + $this->template->toHtml(); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * This action generates the TDBM instance, then the DAOs and Beans. |
|
| 138 | + * |
|
| 139 | + * @Action |
|
| 140 | + * |
|
| 141 | + * @param string $daonamespace |
|
| 142 | + * @param string $beannamespace |
|
| 143 | + * @param int $storeInUtc |
|
| 144 | + * @param string $selfedit |
|
| 145 | + * |
|
| 146 | + * @throws \Mouf\MoufException |
|
| 147 | + */ |
|
| 148 | + public function generate($daonamespace, $beannamespace, $storeInUtc = 0, $selfedit = 'false', $defaultPath = false, $storePath = '') |
|
| 149 | + { |
|
| 150 | + $this->selfedit = $selfedit; |
|
| 151 | + |
|
| 152 | + if ($selfedit == 'true') { |
|
| 153 | + $this->moufManager = MoufManager::getMoufManager(); |
|
| 154 | + } else { |
|
| 155 | + $this->moufManager = MoufManager::getMoufManagerHiddenInstance(); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + if (!$this->moufManager->instanceExists('doctrineApcCache')) { |
|
| 159 | + $doctrineApcCache = $this->moufManager->createInstance('Doctrine\\Common\\Cache\\ApcCache')->setName('doctrineApcCache'); |
|
| 160 | + // TODO: set namespace |
|
| 161 | + } else { |
|
| 162 | + $doctrineApcCache = $this->moufManager->getInstanceDescriptor('doctrineApcCache'); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + if (!$this->moufManager->instanceExists('tdbmService')) { |
|
| 166 | + $tdbmService = $this->moufManager->createInstance('Mouf\\Database\\TDBM\\TDBMService')->setName('tdbmService'); |
|
| 167 | + $tdbmService->getConstructorArgumentProperty('connection')->setValue($this->moufManager->getInstanceDescriptor('dbalConnection')); |
|
| 168 | + $tdbmService->getConstructorArgumentProperty('cache')->setValue($doctrineApcCache); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + $this->moufManager->rewriteMouf(); |
|
| 172 | + |
|
| 173 | + TdbmController::generateDaos($this->moufManager, 'tdbmService', $daonamespace, $beannamespace, 'DaoFactory', 'daoFactory', $selfedit, $storeInUtc, $defaultPath, $storePath); |
|
| 174 | + |
|
| 175 | + InstallUtils::continueInstall($selfedit == 'true'); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + protected $errorMsg; |
|
| 179 | + |
|
| 180 | + private function displayErrorMsg($msg) |
|
| 181 | + { |
|
| 182 | + $this->errorMsg = $msg; |
|
| 183 | + $this->content->addFile(dirname(__FILE__).'/../../../../views/installError.php', $this); |
|
| 184 | + $this->template->toHtml(); |
|
| 185 | + } |
|
| 186 | 186 | } |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | * @Action |
| 87 | 87 | * |
| 88 | 88 | * @param string $name |
| 89 | - * @param bool $selfedit |
|
| 89 | + * @param string|boolean $selfedit |
|
| 90 | 90 | */ |
| 91 | 91 | public function generate($name, $daonamespace, $beannamespace, $daofactoryclassname, $daofactoryinstancename, $storeInUtc = 0, $selfedit = 'false', $useCustomComposer = false, $composerFile = '') |
| 92 | 92 | { |
@@ -109,6 +109,8 @@ discard block |
||
| 109 | 109 | * @param string $daofactoryinstancename |
| 110 | 110 | * @param string $selfedit |
| 111 | 111 | * @param bool $storeInUtc |
| 112 | + * @param boolean $useCustomComposer |
|
| 113 | + * @param string $composerFile |
|
| 112 | 114 | * |
| 113 | 115 | * @throws \Mouf\MoufException |
| 114 | 116 | */ |
@@ -16,213 +16,213 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class BeanDescriptor |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * @var Table |
|
| 21 | - */ |
|
| 22 | - private $table; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var SchemaAnalyzer |
|
| 26 | - */ |
|
| 27 | - private $schemaAnalyzer; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var Schema |
|
| 31 | - */ |
|
| 32 | - private $schema; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var AbstractBeanPropertyDescriptor[] |
|
| 36 | - */ |
|
| 37 | - private $beanPropertyDescriptors = []; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @var TDBMSchemaAnalyzer |
|
| 41 | - */ |
|
| 42 | - private $tdbmSchemaAnalyzer; |
|
| 43 | - |
|
| 44 | - public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
| 45 | - { |
|
| 46 | - $this->table = $table; |
|
| 47 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 48 | - $this->schema = $schema; |
|
| 49 | - $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
| 50 | - $this->initBeanPropertyDescriptors(); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - private function initBeanPropertyDescriptors() |
|
| 54 | - { |
|
| 55 | - $this->beanPropertyDescriptors = $this->getProperties($this->table); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Returns the foreign-key the column is part of, if any. null otherwise. |
|
| 60 | - * |
|
| 61 | - * @param Table $table |
|
| 62 | - * @param Column $column |
|
| 63 | - * |
|
| 64 | - * @return ForeignKeyConstraint|null |
|
| 65 | - */ |
|
| 66 | - private function isPartOfForeignKey(Table $table, Column $column) |
|
| 67 | - { |
|
| 68 | - $localColumnName = $column->getName(); |
|
| 69 | - foreach ($table->getForeignKeys() as $foreignKey) { |
|
| 70 | - foreach ($foreignKey->getColumns() as $columnName) { |
|
| 71 | - if ($columnName === $localColumnName) { |
|
| 72 | - return $foreignKey; |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - return; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @return AbstractBeanPropertyDescriptor[] |
|
| 82 | - */ |
|
| 83 | - public function getBeanPropertyDescriptors() |
|
| 84 | - { |
|
| 85 | - return $this->beanPropertyDescriptors; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent. |
|
| 90 | - * |
|
| 91 | - * @return AbstractBeanPropertyDescriptor[] |
|
| 92 | - */ |
|
| 93 | - public function getConstructorProperties() |
|
| 94 | - { |
|
| 95 | - $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
| 96 | - return $property->isCompulsory(); |
|
| 97 | - }); |
|
| 98 | - |
|
| 99 | - return $constructorProperties; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Returns the list of properties exposed as getters and setters in this class. |
|
| 104 | - * |
|
| 105 | - * @return AbstractBeanPropertyDescriptor[] |
|
| 106 | - */ |
|
| 107 | - public function getExposedProperties() |
|
| 108 | - { |
|
| 109 | - $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
| 110 | - return $property->getTable()->getName() == $this->table->getName(); |
|
| 111 | - }); |
|
| 112 | - |
|
| 113 | - return $exposedProperties; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Returns the list of properties for this table (including parent tables). |
|
| 118 | - * |
|
| 119 | - * @param Table $table |
|
| 120 | - * |
|
| 121 | - * @return AbstractBeanPropertyDescriptor[] |
|
| 122 | - */ |
|
| 123 | - private function getProperties(Table $table) |
|
| 124 | - { |
|
| 125 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
| 126 | - if ($parentRelationship) { |
|
| 127 | - $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); |
|
| 128 | - $properties = $this->getProperties($parentTable); |
|
| 129 | - // we merge properties by overriding property names. |
|
| 130 | - $localProperties = $this->getPropertiesForTable($table); |
|
| 131 | - foreach ($localProperties as $name => $property) { |
|
| 132 | - // We do not override properties if this is a primary key! |
|
| 133 | - if ($property->isPrimaryKey()) { |
|
| 134 | - continue; |
|
| 135 | - } |
|
| 136 | - $properties[$name] = $property; |
|
| 137 | - } |
|
| 138 | - } else { |
|
| 139 | - $properties = $this->getPropertiesForTable($table); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $properties; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Returns the list of properties for this table (ignoring parent tables). |
|
| 147 | - * |
|
| 148 | - * @param Table $table |
|
| 149 | - * |
|
| 150 | - * @return AbstractBeanPropertyDescriptor[] |
|
| 151 | - */ |
|
| 152 | - private function getPropertiesForTable(Table $table) |
|
| 153 | - { |
|
| 154 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
| 155 | - if ($parentRelationship) { |
|
| 156 | - $ignoreColumns = $parentRelationship->getLocalColumns(); |
|
| 157 | - } else { |
|
| 158 | - $ignoreColumns = []; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - $beanPropertyDescriptors = []; |
|
| 162 | - |
|
| 163 | - foreach ($table->getColumns() as $column) { |
|
| 164 | - if (array_search($column->getName(), $ignoreColumns) !== false) { |
|
| 165 | - continue; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - $fk = $this->isPartOfForeignKey($table, $column); |
|
| 169 | - if ($fk !== null) { |
|
| 170 | - // Check that previously added descriptors are not added on same FK (can happen with multi key FK). |
|
| 171 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 172 | - if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) { |
|
| 173 | - continue 2; |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - // Check that this property is not an inheritance relationship |
|
| 177 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
| 178 | - if ($parentRelationship === $fk) { |
|
| 179 | - continue; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer); |
|
| 183 | - } else { |
|
| 184 | - $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column); |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - // Now, let's get the name of all properties and let's check there is no duplicate. |
|
| 189 | - /** @var $names AbstractBeanPropertyDescriptor[] */ |
|
| 190 | - $names = []; |
|
| 191 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 192 | - $name = $beanDescriptor->getUpperCamelCaseName(); |
|
| 193 | - if (isset($names[$name])) { |
|
| 194 | - $names[$name]->useAlternativeName(); |
|
| 195 | - $beanDescriptor->useAlternativeName(); |
|
| 196 | - } else { |
|
| 197 | - $names[$name] = $beanDescriptor; |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - // Final check (throw exceptions if problem arises) |
|
| 202 | - $names = []; |
|
| 203 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 204 | - $name = $beanDescriptor->getUpperCamelCaseName(); |
|
| 205 | - if (isset($names[$name])) { |
|
| 206 | - throw new TDBMException('Unsolvable name conflict while generating method name'); |
|
| 207 | - } else { |
|
| 208 | - $names[$name] = $beanDescriptor; |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - // Last step, let's rebuild the list with a map: |
|
| 213 | - $beanPropertyDescriptorsMap = []; |
|
| 214 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 215 | - $beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - return $beanPropertyDescriptorsMap; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - public function generateBeanConstructor() |
|
| 222 | - { |
|
| 223 | - $constructorProperties = $this->getConstructorProperties(); |
|
| 224 | - |
|
| 225 | - $constructorCode = ' /** |
|
| 19 | + /** |
|
| 20 | + * @var Table |
|
| 21 | + */ |
|
| 22 | + private $table; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var SchemaAnalyzer |
|
| 26 | + */ |
|
| 27 | + private $schemaAnalyzer; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var Schema |
|
| 31 | + */ |
|
| 32 | + private $schema; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var AbstractBeanPropertyDescriptor[] |
|
| 36 | + */ |
|
| 37 | + private $beanPropertyDescriptors = []; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @var TDBMSchemaAnalyzer |
|
| 41 | + */ |
|
| 42 | + private $tdbmSchemaAnalyzer; |
|
| 43 | + |
|
| 44 | + public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
| 45 | + { |
|
| 46 | + $this->table = $table; |
|
| 47 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 48 | + $this->schema = $schema; |
|
| 49 | + $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
| 50 | + $this->initBeanPropertyDescriptors(); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + private function initBeanPropertyDescriptors() |
|
| 54 | + { |
|
| 55 | + $this->beanPropertyDescriptors = $this->getProperties($this->table); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Returns the foreign-key the column is part of, if any. null otherwise. |
|
| 60 | + * |
|
| 61 | + * @param Table $table |
|
| 62 | + * @param Column $column |
|
| 63 | + * |
|
| 64 | + * @return ForeignKeyConstraint|null |
|
| 65 | + */ |
|
| 66 | + private function isPartOfForeignKey(Table $table, Column $column) |
|
| 67 | + { |
|
| 68 | + $localColumnName = $column->getName(); |
|
| 69 | + foreach ($table->getForeignKeys() as $foreignKey) { |
|
| 70 | + foreach ($foreignKey->getColumns() as $columnName) { |
|
| 71 | + if ($columnName === $localColumnName) { |
|
| 72 | + return $foreignKey; |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @return AbstractBeanPropertyDescriptor[] |
|
| 82 | + */ |
|
| 83 | + public function getBeanPropertyDescriptors() |
|
| 84 | + { |
|
| 85 | + return $this->beanPropertyDescriptors; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent. |
|
| 90 | + * |
|
| 91 | + * @return AbstractBeanPropertyDescriptor[] |
|
| 92 | + */ |
|
| 93 | + public function getConstructorProperties() |
|
| 94 | + { |
|
| 95 | + $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
| 96 | + return $property->isCompulsory(); |
|
| 97 | + }); |
|
| 98 | + |
|
| 99 | + return $constructorProperties; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Returns the list of properties exposed as getters and setters in this class. |
|
| 104 | + * |
|
| 105 | + * @return AbstractBeanPropertyDescriptor[] |
|
| 106 | + */ |
|
| 107 | + public function getExposedProperties() |
|
| 108 | + { |
|
| 109 | + $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
| 110 | + return $property->getTable()->getName() == $this->table->getName(); |
|
| 111 | + }); |
|
| 112 | + |
|
| 113 | + return $exposedProperties; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Returns the list of properties for this table (including parent tables). |
|
| 118 | + * |
|
| 119 | + * @param Table $table |
|
| 120 | + * |
|
| 121 | + * @return AbstractBeanPropertyDescriptor[] |
|
| 122 | + */ |
|
| 123 | + private function getProperties(Table $table) |
|
| 124 | + { |
|
| 125 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
| 126 | + if ($parentRelationship) { |
|
| 127 | + $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); |
|
| 128 | + $properties = $this->getProperties($parentTable); |
|
| 129 | + // we merge properties by overriding property names. |
|
| 130 | + $localProperties = $this->getPropertiesForTable($table); |
|
| 131 | + foreach ($localProperties as $name => $property) { |
|
| 132 | + // We do not override properties if this is a primary key! |
|
| 133 | + if ($property->isPrimaryKey()) { |
|
| 134 | + continue; |
|
| 135 | + } |
|
| 136 | + $properties[$name] = $property; |
|
| 137 | + } |
|
| 138 | + } else { |
|
| 139 | + $properties = $this->getPropertiesForTable($table); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return $properties; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Returns the list of properties for this table (ignoring parent tables). |
|
| 147 | + * |
|
| 148 | + * @param Table $table |
|
| 149 | + * |
|
| 150 | + * @return AbstractBeanPropertyDescriptor[] |
|
| 151 | + */ |
|
| 152 | + private function getPropertiesForTable(Table $table) |
|
| 153 | + { |
|
| 154 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
| 155 | + if ($parentRelationship) { |
|
| 156 | + $ignoreColumns = $parentRelationship->getLocalColumns(); |
|
| 157 | + } else { |
|
| 158 | + $ignoreColumns = []; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + $beanPropertyDescriptors = []; |
|
| 162 | + |
|
| 163 | + foreach ($table->getColumns() as $column) { |
|
| 164 | + if (array_search($column->getName(), $ignoreColumns) !== false) { |
|
| 165 | + continue; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + $fk = $this->isPartOfForeignKey($table, $column); |
|
| 169 | + if ($fk !== null) { |
|
| 170 | + // Check that previously added descriptors are not added on same FK (can happen with multi key FK). |
|
| 171 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 172 | + if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) { |
|
| 173 | + continue 2; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + // Check that this property is not an inheritance relationship |
|
| 177 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
| 178 | + if ($parentRelationship === $fk) { |
|
| 179 | + continue; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer); |
|
| 183 | + } else { |
|
| 184 | + $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + // Now, let's get the name of all properties and let's check there is no duplicate. |
|
| 189 | + /** @var $names AbstractBeanPropertyDescriptor[] */ |
|
| 190 | + $names = []; |
|
| 191 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 192 | + $name = $beanDescriptor->getUpperCamelCaseName(); |
|
| 193 | + if (isset($names[$name])) { |
|
| 194 | + $names[$name]->useAlternativeName(); |
|
| 195 | + $beanDescriptor->useAlternativeName(); |
|
| 196 | + } else { |
|
| 197 | + $names[$name] = $beanDescriptor; |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + // Final check (throw exceptions if problem arises) |
|
| 202 | + $names = []; |
|
| 203 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 204 | + $name = $beanDescriptor->getUpperCamelCaseName(); |
|
| 205 | + if (isset($names[$name])) { |
|
| 206 | + throw new TDBMException('Unsolvable name conflict while generating method name'); |
|
| 207 | + } else { |
|
| 208 | + $names[$name] = $beanDescriptor; |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + // Last step, let's rebuild the list with a map: |
|
| 213 | + $beanPropertyDescriptorsMap = []; |
|
| 214 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 215 | + $beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + return $beanPropertyDescriptorsMap; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + public function generateBeanConstructor() |
|
| 222 | + { |
|
| 223 | + $constructorProperties = $this->getConstructorProperties(); |
|
| 224 | + |
|
| 225 | + $constructorCode = ' /** |
|
| 226 | 226 | * The constructor takes all compulsory arguments. |
| 227 | 227 | * |
| 228 | 228 | %s |
@@ -232,65 +232,65 @@ discard block |
||
| 232 | 232 | } |
| 233 | 233 | '; |
| 234 | 234 | |
| 235 | - $paramAnnotations = []; |
|
| 236 | - $arguments = []; |
|
| 237 | - $assigns = []; |
|
| 238 | - $parentConstructorArguments = []; |
|
| 239 | - |
|
| 240 | - foreach ($constructorProperties as $property) { |
|
| 241 | - $className = $property->getClassName(); |
|
| 242 | - if ($className) { |
|
| 243 | - $arguments[] = $className.' '.$property->getVariableName(); |
|
| 244 | - } else { |
|
| 245 | - $arguments[] = $property->getVariableName(); |
|
| 246 | - } |
|
| 247 | - $paramAnnotations[] = $property->getParamAnnotation(); |
|
| 248 | - if ($property->getTable()->getName() === $this->table->getName()) { |
|
| 249 | - $assigns[] = $property->getConstructorAssignCode(); |
|
| 250 | - } else { |
|
| 251 | - $parentConstructorArguments[] = $property->getVariableName(); |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - $parentConstrutorCode = sprintf(" parent::__construct(%s);\n", implode(', ', $parentConstructorArguments)); |
|
| 256 | - |
|
| 257 | - return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstrutorCode, implode("\n", $assigns)); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - public function generateDirectForeignKeysCode() |
|
| 261 | - { |
|
| 262 | - $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
| 263 | - |
|
| 264 | - $fksByTable = []; |
|
| 265 | - |
|
| 266 | - foreach ($fks as $fk) { |
|
| 267 | - $fksByTable[$fk->getLocalTableName()][] = $fk; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /* @var $fksByMethodName ForeignKeyConstraint[] */ |
|
| 271 | - $fksByMethodName = []; |
|
| 272 | - |
|
| 273 | - foreach ($fksByTable as $tableName => $fksForTable) { |
|
| 274 | - if (count($fksForTable) > 1) { |
|
| 275 | - foreach ($fksForTable as $fk) { |
|
| 276 | - $methodName = 'get'.TDBMDaoGenerator::toCamelCase($fk->getLocalTableName()).'By'; |
|
| 277 | - |
|
| 278 | - $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $fk->getLocalColumns()); |
|
| 279 | - |
|
| 280 | - $methodName .= implode('And', $camelizedColumns); |
|
| 281 | - |
|
| 282 | - $fksByMethodName[$methodName] = $fk; |
|
| 283 | - } |
|
| 284 | - } else { |
|
| 285 | - $methodName = 'get'.TDBMDaoGenerator::toCamelCase($fksForTable[0]->getLocalTableName()); |
|
| 286 | - $fksByMethodName[$methodName] = $fksForTable[0]; |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - $code = ''; |
|
| 291 | - |
|
| 292 | - foreach ($fksByMethodName as $methodName => $fk) { |
|
| 293 | - $getterCode = ' /** |
|
| 235 | + $paramAnnotations = []; |
|
| 236 | + $arguments = []; |
|
| 237 | + $assigns = []; |
|
| 238 | + $parentConstructorArguments = []; |
|
| 239 | + |
|
| 240 | + foreach ($constructorProperties as $property) { |
|
| 241 | + $className = $property->getClassName(); |
|
| 242 | + if ($className) { |
|
| 243 | + $arguments[] = $className.' '.$property->getVariableName(); |
|
| 244 | + } else { |
|
| 245 | + $arguments[] = $property->getVariableName(); |
|
| 246 | + } |
|
| 247 | + $paramAnnotations[] = $property->getParamAnnotation(); |
|
| 248 | + if ($property->getTable()->getName() === $this->table->getName()) { |
|
| 249 | + $assigns[] = $property->getConstructorAssignCode(); |
|
| 250 | + } else { |
|
| 251 | + $parentConstructorArguments[] = $property->getVariableName(); |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + $parentConstrutorCode = sprintf(" parent::__construct(%s);\n", implode(', ', $parentConstructorArguments)); |
|
| 256 | + |
|
| 257 | + return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstrutorCode, implode("\n", $assigns)); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + public function generateDirectForeignKeysCode() |
|
| 261 | + { |
|
| 262 | + $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
| 263 | + |
|
| 264 | + $fksByTable = []; |
|
| 265 | + |
|
| 266 | + foreach ($fks as $fk) { |
|
| 267 | + $fksByTable[$fk->getLocalTableName()][] = $fk; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /* @var $fksByMethodName ForeignKeyConstraint[] */ |
|
| 271 | + $fksByMethodName = []; |
|
| 272 | + |
|
| 273 | + foreach ($fksByTable as $tableName => $fksForTable) { |
|
| 274 | + if (count($fksForTable) > 1) { |
|
| 275 | + foreach ($fksForTable as $fk) { |
|
| 276 | + $methodName = 'get'.TDBMDaoGenerator::toCamelCase($fk->getLocalTableName()).'By'; |
|
| 277 | + |
|
| 278 | + $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $fk->getLocalColumns()); |
|
| 279 | + |
|
| 280 | + $methodName .= implode('And', $camelizedColumns); |
|
| 281 | + |
|
| 282 | + $fksByMethodName[$methodName] = $fk; |
|
| 283 | + } |
|
| 284 | + } else { |
|
| 285 | + $methodName = 'get'.TDBMDaoGenerator::toCamelCase($fksForTable[0]->getLocalTableName()); |
|
| 286 | + $fksByMethodName[$methodName] = $fksForTable[0]; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + $code = ''; |
|
| 291 | + |
|
| 292 | + foreach ($fksByMethodName as $methodName => $fk) { |
|
| 293 | + $getterCode = ' /** |
|
| 294 | 294 | * Returns the list of %s pointing to this bean via the %s column. |
| 295 | 295 | * |
| 296 | 296 | * @return %s[]|ResultIterator |
@@ -302,111 +302,111 @@ discard block |
||
| 302 | 302 | |
| 303 | 303 | '; |
| 304 | 304 | |
| 305 | - list($sql, $parametersCode) = $this->getFilters($fk); |
|
| 306 | - |
|
| 307 | - $beanClass = TDBMDaoGenerator::getBeanNameFromTableName($fk->getLocalTableName()); |
|
| 308 | - $code .= sprintf($getterCode, |
|
| 309 | - $beanClass, |
|
| 310 | - implode(', ', $fk->getColumns()), |
|
| 311 | - $beanClass, |
|
| 312 | - $methodName, |
|
| 313 | - var_export($fk->getLocalTableName(), true), |
|
| 314 | - $sql, |
|
| 315 | - $parametersCode |
|
| 316 | - ); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - return $code; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - private function getFilters(ForeignKeyConstraint $fk) |
|
| 323 | - { |
|
| 324 | - $sqlParts = []; |
|
| 325 | - $counter = 0; |
|
| 326 | - $parameters = []; |
|
| 327 | - |
|
| 328 | - $pkColumns = $this->table->getPrimaryKeyColumns(); |
|
| 329 | - |
|
| 330 | - foreach ($fk->getLocalColumns() as $columnName) { |
|
| 331 | - $paramName = 'tdbmparam'.$counter; |
|
| 332 | - $sqlParts[] = $fk->getLocalTableName().'.'.$columnName.' = :'.$paramName; |
|
| 333 | - |
|
| 334 | - $pkColumn = $pkColumns[$counter]; |
|
| 335 | - $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($paramName, true), var_export($pkColumn, true), var_export($this->table->getName(), true)); |
|
| 336 | - ++$counter; |
|
| 337 | - } |
|
| 338 | - $sql = "'".implode(' AND ', $sqlParts)."'"; |
|
| 339 | - $parametersCode = '[ '.implode(', ', $parameters).' ]'; |
|
| 340 | - |
|
| 341 | - return [$sql, $parametersCode]; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Generate code section about pivot tables. |
|
| 346 | - * |
|
| 347 | - * @return string |
|
| 348 | - */ |
|
| 349 | - public function generatePivotTableCode() |
|
| 350 | - { |
|
| 351 | - $finalDescs = $this->getPivotTableDescriptors(); |
|
| 352 | - |
|
| 353 | - $code = ''; |
|
| 354 | - |
|
| 355 | - foreach ($finalDescs as $desc) { |
|
| 356 | - $code .= $this->getPivotTableCode($desc['name'], $desc['table'], $desc['localFK'], $desc['remoteFK']); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - return $code; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - private function getPivotTableDescriptors() |
|
| 363 | - { |
|
| 364 | - $descs = []; |
|
| 365 | - foreach ($this->schemaAnalyzer->detectJunctionTables() as $table) { |
|
| 366 | - // There are exactly 2 FKs since this is a pivot table. |
|
| 367 | - $fks = array_values($table->getForeignKeys()); |
|
| 368 | - |
|
| 369 | - if ($fks[0]->getForeignTableName() === $this->table->getName()) { |
|
| 370 | - $localFK = $fks[0]; |
|
| 371 | - $remoteFK = $fks[1]; |
|
| 372 | - } elseif ($fks[1]->getForeignTableName() === $this->table->getName()) { |
|
| 373 | - $localFK = $fks[1]; |
|
| 374 | - $remoteFK = $fks[0]; |
|
| 375 | - } else { |
|
| 376 | - continue; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - $descs[$remoteFK->getForeignTableName()][] = [ |
|
| 380 | - 'table' => $table, |
|
| 381 | - 'localFK' => $localFK, |
|
| 382 | - 'remoteFK' => $remoteFK, |
|
| 383 | - ]; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - $finalDescs = []; |
|
| 387 | - foreach ($descs as $descArray) { |
|
| 388 | - if (count($descArray) > 1) { |
|
| 389 | - foreach ($descArray as $desc) { |
|
| 390 | - $desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($desc['table']->getName()); |
|
| 391 | - $finalDescs[] = $desc; |
|
| 392 | - } |
|
| 393 | - } else { |
|
| 394 | - $desc = $descArray[0]; |
|
| 395 | - $desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()); |
|
| 396 | - $finalDescs[] = $desc; |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - return $finalDescs; |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - public function getPivotTableCode($name, Table $table, ForeignKeyConstraint $localFK, ForeignKeyConstraint $remoteFK) |
|
| 404 | - { |
|
| 405 | - $singularName = TDBMDaoGenerator::toSingular($name); |
|
| 406 | - $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
|
| 407 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 408 | - |
|
| 409 | - $str = ' /** |
|
| 305 | + list($sql, $parametersCode) = $this->getFilters($fk); |
|
| 306 | + |
|
| 307 | + $beanClass = TDBMDaoGenerator::getBeanNameFromTableName($fk->getLocalTableName()); |
|
| 308 | + $code .= sprintf($getterCode, |
|
| 309 | + $beanClass, |
|
| 310 | + implode(', ', $fk->getColumns()), |
|
| 311 | + $beanClass, |
|
| 312 | + $methodName, |
|
| 313 | + var_export($fk->getLocalTableName(), true), |
|
| 314 | + $sql, |
|
| 315 | + $parametersCode |
|
| 316 | + ); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + return $code; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + private function getFilters(ForeignKeyConstraint $fk) |
|
| 323 | + { |
|
| 324 | + $sqlParts = []; |
|
| 325 | + $counter = 0; |
|
| 326 | + $parameters = []; |
|
| 327 | + |
|
| 328 | + $pkColumns = $this->table->getPrimaryKeyColumns(); |
|
| 329 | + |
|
| 330 | + foreach ($fk->getLocalColumns() as $columnName) { |
|
| 331 | + $paramName = 'tdbmparam'.$counter; |
|
| 332 | + $sqlParts[] = $fk->getLocalTableName().'.'.$columnName.' = :'.$paramName; |
|
| 333 | + |
|
| 334 | + $pkColumn = $pkColumns[$counter]; |
|
| 335 | + $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($paramName, true), var_export($pkColumn, true), var_export($this->table->getName(), true)); |
|
| 336 | + ++$counter; |
|
| 337 | + } |
|
| 338 | + $sql = "'".implode(' AND ', $sqlParts)."'"; |
|
| 339 | + $parametersCode = '[ '.implode(', ', $parameters).' ]'; |
|
| 340 | + |
|
| 341 | + return [$sql, $parametersCode]; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Generate code section about pivot tables. |
|
| 346 | + * |
|
| 347 | + * @return string |
|
| 348 | + */ |
|
| 349 | + public function generatePivotTableCode() |
|
| 350 | + { |
|
| 351 | + $finalDescs = $this->getPivotTableDescriptors(); |
|
| 352 | + |
|
| 353 | + $code = ''; |
|
| 354 | + |
|
| 355 | + foreach ($finalDescs as $desc) { |
|
| 356 | + $code .= $this->getPivotTableCode($desc['name'], $desc['table'], $desc['localFK'], $desc['remoteFK']); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + return $code; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + private function getPivotTableDescriptors() |
|
| 363 | + { |
|
| 364 | + $descs = []; |
|
| 365 | + foreach ($this->schemaAnalyzer->detectJunctionTables() as $table) { |
|
| 366 | + // There are exactly 2 FKs since this is a pivot table. |
|
| 367 | + $fks = array_values($table->getForeignKeys()); |
|
| 368 | + |
|
| 369 | + if ($fks[0]->getForeignTableName() === $this->table->getName()) { |
|
| 370 | + $localFK = $fks[0]; |
|
| 371 | + $remoteFK = $fks[1]; |
|
| 372 | + } elseif ($fks[1]->getForeignTableName() === $this->table->getName()) { |
|
| 373 | + $localFK = $fks[1]; |
|
| 374 | + $remoteFK = $fks[0]; |
|
| 375 | + } else { |
|
| 376 | + continue; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + $descs[$remoteFK->getForeignTableName()][] = [ |
|
| 380 | + 'table' => $table, |
|
| 381 | + 'localFK' => $localFK, |
|
| 382 | + 'remoteFK' => $remoteFK, |
|
| 383 | + ]; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + $finalDescs = []; |
|
| 387 | + foreach ($descs as $descArray) { |
|
| 388 | + if (count($descArray) > 1) { |
|
| 389 | + foreach ($descArray as $desc) { |
|
| 390 | + $desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($desc['table']->getName()); |
|
| 391 | + $finalDescs[] = $desc; |
|
| 392 | + } |
|
| 393 | + } else { |
|
| 394 | + $desc = $descArray[0]; |
|
| 395 | + $desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()); |
|
| 396 | + $finalDescs[] = $desc; |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + return $finalDescs; |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + public function getPivotTableCode($name, Table $table, ForeignKeyConstraint $localFK, ForeignKeyConstraint $remoteFK) |
|
| 404 | + { |
|
| 405 | + $singularName = TDBMDaoGenerator::toSingular($name); |
|
| 406 | + $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
|
| 407 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 408 | + |
|
| 409 | + $str = ' /** |
|
| 410 | 410 | * Returns the list of %s associated to this bean via the %s pivot table. |
| 411 | 411 | * |
| 412 | 412 | * @return %s[] |
@@ -416,9 +416,9 @@ discard block |
||
| 416 | 416 | } |
| 417 | 417 | '; |
| 418 | 418 | |
| 419 | - $getterCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $name, var_export($remoteFK->getLocalTableName(), true)); |
|
| 419 | + $getterCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $name, var_export($remoteFK->getLocalTableName(), true)); |
|
| 420 | 420 | |
| 421 | - $str = ' /** |
|
| 421 | + $str = ' /** |
|
| 422 | 422 | * Adds a relationship with %s associated to this bean via the %s pivot table. |
| 423 | 423 | * |
| 424 | 424 | * @param %s %s |
@@ -428,9 +428,9 @@ discard block |
||
| 428 | 428 | } |
| 429 | 429 | '; |
| 430 | 430 | |
| 431 | - $adderCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
| 431 | + $adderCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
| 432 | 432 | |
| 433 | - $str = ' /** |
|
| 433 | + $str = ' /** |
|
| 434 | 434 | * Deletes the relationship with %s associated to this bean via the %s pivot table. |
| 435 | 435 | * |
| 436 | 436 | * @param %s %s |
@@ -440,9 +440,9 @@ discard block |
||
| 440 | 440 | } |
| 441 | 441 | '; |
| 442 | 442 | |
| 443 | - $removerCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
| 443 | + $removerCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
| 444 | 444 | |
| 445 | - $str = ' /** |
|
| 445 | + $str = ' /** |
|
| 446 | 446 | * Returns whether this bean is associated with %s via the %s pivot table. |
| 447 | 447 | * |
| 448 | 448 | * @param %s %s |
@@ -453,24 +453,24 @@ discard block |
||
| 453 | 453 | } |
| 454 | 454 | '; |
| 455 | 455 | |
| 456 | - $hasCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
| 456 | + $hasCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
| 457 | 457 | |
| 458 | - $code = $getterCode.$adderCode.$removerCode.$hasCode; |
|
| 458 | + $code = $getterCode.$adderCode.$removerCode.$hasCode; |
|
| 459 | 459 | |
| 460 | - return $code; |
|
| 461 | - } |
|
| 460 | + return $code; |
|
| 461 | + } |
|
| 462 | 462 | |
| 463 | - public function generateJsonSerialize() |
|
| 464 | - { |
|
| 465 | - $tableName = $this->table->getName(); |
|
| 466 | - $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
| 467 | - if ($parentFk !== null) { |
|
| 468 | - $initializer = '$array = parent::jsonSerialize();'; |
|
| 469 | - } else { |
|
| 470 | - $initializer = '$array = [];'; |
|
| 471 | - } |
|
| 463 | + public function generateJsonSerialize() |
|
| 464 | + { |
|
| 465 | + $tableName = $this->table->getName(); |
|
| 466 | + $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
| 467 | + if ($parentFk !== null) { |
|
| 468 | + $initializer = '$array = parent::jsonSerialize();'; |
|
| 469 | + } else { |
|
| 470 | + $initializer = '$array = [];'; |
|
| 471 | + } |
|
| 472 | 472 | |
| 473 | - $str = ' |
|
| 473 | + $str = ' |
|
| 474 | 474 | /** |
| 475 | 475 | * Serializes the object for JSON encoding |
| 476 | 476 | * |
@@ -486,89 +486,89 @@ discard block |
||
| 486 | 486 | } |
| 487 | 487 | '; |
| 488 | 488 | |
| 489 | - $propertiesCode = ''; |
|
| 490 | - foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) { |
|
| 491 | - $propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode(); |
|
| 492 | - } |
|
| 489 | + $propertiesCode = ''; |
|
| 490 | + foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) { |
|
| 491 | + $propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode(); |
|
| 492 | + } |
|
| 493 | 493 | |
| 494 | - // Many to many relationships: |
|
| 494 | + // Many to many relationships: |
|
| 495 | 495 | |
| 496 | - $descs = $this->getPivotTableDescriptors(); |
|
| 496 | + $descs = $this->getPivotTableDescriptors(); |
|
| 497 | 497 | |
| 498 | - $many2manyCode = ''; |
|
| 498 | + $many2manyCode = ''; |
|
| 499 | 499 | |
| 500 | - foreach ($descs as $desc) { |
|
| 501 | - $remoteFK = $desc['remoteFK']; |
|
| 502 | - $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
|
| 503 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 500 | + foreach ($descs as $desc) { |
|
| 501 | + $remoteFK = $desc['remoteFK']; |
|
| 502 | + $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
|
| 503 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 504 | 504 | |
| 505 | - $many2manyCode .= ' if (!$stopRecursion) { |
|
| 505 | + $many2manyCode .= ' if (!$stopRecursion) { |
|
| 506 | 506 | $array[\''.lcfirst($desc['name']).'\'] = array_map(function('.$remoteBeanName.' '.$variableName.') { |
| 507 | 507 | return '.$variableName.'->jsonSerialize(true); |
| 508 | 508 | }, $this->get'.$desc['name'].'()); |
| 509 | 509 | } |
| 510 | 510 | '; |
| 511 | - } |
|
| 512 | - |
|
| 513 | - return sprintf($str, $initializer, $propertiesCode, $many2manyCode); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * Returns as an array the class we need to extend from and the list of use statements. |
|
| 518 | - * |
|
| 519 | - * @return array |
|
| 520 | - */ |
|
| 521 | - private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null) |
|
| 522 | - { |
|
| 523 | - $classes = []; |
|
| 524 | - if ($parentFk !== null) { |
|
| 525 | - $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName()); |
|
| 526 | - $classes[] = $extends; |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) { |
|
| 530 | - $className = $beanPropertyDescriptor->getClassName(); |
|
| 531 | - if (null !== $className) { |
|
| 532 | - $classes[] = $beanPropertyDescriptor->getClassName(); |
|
| 533 | - } |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - foreach ($this->getPivotTableDescriptors() as $descriptor) { |
|
| 537 | - /* @var $fk ForeignKeyConstraint */ |
|
| 538 | - $fk = $descriptor['remoteFK']; |
|
| 539 | - $classes[] = TDBMDaoGenerator::getBeanNameFromTableName($fk->getForeignTableName()); |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - $classes = array_flip(array_flip($classes)); |
|
| 543 | - |
|
| 544 | - return $classes; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - /** |
|
| 548 | - * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 549 | - * |
|
| 550 | - * @param string $beannamespace The namespace of the bean |
|
| 551 | - */ |
|
| 552 | - public function generatePhpCode($beannamespace) |
|
| 553 | - { |
|
| 554 | - $tableName = $this->table->getName(); |
|
| 555 | - $baseClassName = TDBMDaoGenerator::getBaseBeanNameFromTableName($tableName); |
|
| 556 | - $className = TDBMDaoGenerator::getBeanNameFromTableName($tableName); |
|
| 557 | - $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
| 558 | - |
|
| 559 | - $classes = $this->generateExtendsAndUseStatements($parentFk); |
|
| 560 | - |
|
| 561 | - $uses = array_map(function ($className) use ($beannamespace) { return 'use '.$beannamespace.'\\'.$className.";\n"; }, $classes); |
|
| 562 | - $use = implode('', $uses); |
|
| 563 | - |
|
| 564 | - if ($parentFk !== null) { |
|
| 565 | - $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName()); |
|
| 566 | - } else { |
|
| 567 | - $extends = 'AbstractTDBMObject'; |
|
| 568 | - $use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n"; |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - $str = "<?php |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + return sprintf($str, $initializer, $propertiesCode, $many2manyCode); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * Returns as an array the class we need to extend from and the list of use statements. |
|
| 518 | + * |
|
| 519 | + * @return array |
|
| 520 | + */ |
|
| 521 | + private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null) |
|
| 522 | + { |
|
| 523 | + $classes = []; |
|
| 524 | + if ($parentFk !== null) { |
|
| 525 | + $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName()); |
|
| 526 | + $classes[] = $extends; |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) { |
|
| 530 | + $className = $beanPropertyDescriptor->getClassName(); |
|
| 531 | + if (null !== $className) { |
|
| 532 | + $classes[] = $beanPropertyDescriptor->getClassName(); |
|
| 533 | + } |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + foreach ($this->getPivotTableDescriptors() as $descriptor) { |
|
| 537 | + /* @var $fk ForeignKeyConstraint */ |
|
| 538 | + $fk = $descriptor['remoteFK']; |
|
| 539 | + $classes[] = TDBMDaoGenerator::getBeanNameFromTableName($fk->getForeignTableName()); |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + $classes = array_flip(array_flip($classes)); |
|
| 543 | + |
|
| 544 | + return $classes; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + /** |
|
| 548 | + * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 549 | + * |
|
| 550 | + * @param string $beannamespace The namespace of the bean |
|
| 551 | + */ |
|
| 552 | + public function generatePhpCode($beannamespace) |
|
| 553 | + { |
|
| 554 | + $tableName = $this->table->getName(); |
|
| 555 | + $baseClassName = TDBMDaoGenerator::getBaseBeanNameFromTableName($tableName); |
|
| 556 | + $className = TDBMDaoGenerator::getBeanNameFromTableName($tableName); |
|
| 557 | + $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
| 558 | + |
|
| 559 | + $classes = $this->generateExtendsAndUseStatements($parentFk); |
|
| 560 | + |
|
| 561 | + $uses = array_map(function ($className) use ($beannamespace) { return 'use '.$beannamespace.'\\'.$className.";\n"; }, $classes); |
|
| 562 | + $use = implode('', $uses); |
|
| 563 | + |
|
| 564 | + if ($parentFk !== null) { |
|
| 565 | + $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName()); |
|
| 566 | + } else { |
|
| 567 | + $extends = 'AbstractTDBMObject'; |
|
| 568 | + $use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n"; |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + $str = "<?php |
|
| 572 | 572 | namespace {$beannamespace}\\Generated; |
| 573 | 573 | |
| 574 | 574 | use Mouf\\Database\\TDBM\\ResultIterator; |
@@ -586,122 +586,122 @@ discard block |
||
| 586 | 586 | { |
| 587 | 587 | "; |
| 588 | 588 | |
| 589 | - $str .= $this->generateBeanConstructor(); |
|
| 589 | + $str .= $this->generateBeanConstructor(); |
|
| 590 | 590 | |
| 591 | - foreach ($this->getExposedProperties() as $property) { |
|
| 592 | - $str .= $property->getGetterSetterCode(); |
|
| 593 | - } |
|
| 591 | + foreach ($this->getExposedProperties() as $property) { |
|
| 592 | + $str .= $property->getGetterSetterCode(); |
|
| 593 | + } |
|
| 594 | 594 | |
| 595 | - $str .= $this->generateDirectForeignKeysCode(); |
|
| 596 | - $str .= $this->generatePivotTableCode(); |
|
| 597 | - $str .= $this->generateJsonSerialize(); |
|
| 595 | + $str .= $this->generateDirectForeignKeysCode(); |
|
| 596 | + $str .= $this->generatePivotTableCode(); |
|
| 597 | + $str .= $this->generateJsonSerialize(); |
|
| 598 | 598 | |
| 599 | - $str .= '} |
|
| 599 | + $str .= '} |
|
| 600 | 600 | '; |
| 601 | 601 | |
| 602 | - return $str; |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - /** |
|
| 606 | - * @param string $beanNamespace |
|
| 607 | - * @param string $beanClassName |
|
| 608 | - * |
|
| 609 | - * @return array first element: list of used beans, second item: PHP code as a string |
|
| 610 | - */ |
|
| 611 | - public function generateFindByDaoCode($beanNamespace, $beanClassName) |
|
| 612 | - { |
|
| 613 | - $code = ''; |
|
| 614 | - $usedBeans = []; |
|
| 615 | - foreach ($this->table->getIndexes() as $index) { |
|
| 616 | - if (!$index->isPrimary()) { |
|
| 617 | - list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName); |
|
| 618 | - $code .= $codeForIndex; |
|
| 619 | - $usedBeans = array_merge($usedBeans, $usedBeansForIndex); |
|
| 620 | - } |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - return [$usedBeans, $code]; |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * @param Index $index |
|
| 628 | - * @param string $beanNamespace |
|
| 629 | - * @param string $beanClassName |
|
| 630 | - * |
|
| 631 | - * @return array first element: list of used beans, second item: PHP code as a string |
|
| 632 | - */ |
|
| 633 | - private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName) |
|
| 634 | - { |
|
| 635 | - $columns = $index->getColumns(); |
|
| 636 | - $usedBeans = []; |
|
| 637 | - |
|
| 638 | - /* |
|
| 602 | + return $str; |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + /** |
|
| 606 | + * @param string $beanNamespace |
|
| 607 | + * @param string $beanClassName |
|
| 608 | + * |
|
| 609 | + * @return array first element: list of used beans, second item: PHP code as a string |
|
| 610 | + */ |
|
| 611 | + public function generateFindByDaoCode($beanNamespace, $beanClassName) |
|
| 612 | + { |
|
| 613 | + $code = ''; |
|
| 614 | + $usedBeans = []; |
|
| 615 | + foreach ($this->table->getIndexes() as $index) { |
|
| 616 | + if (!$index->isPrimary()) { |
|
| 617 | + list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName); |
|
| 618 | + $code .= $codeForIndex; |
|
| 619 | + $usedBeans = array_merge($usedBeans, $usedBeansForIndex); |
|
| 620 | + } |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + return [$usedBeans, $code]; |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * @param Index $index |
|
| 628 | + * @param string $beanNamespace |
|
| 629 | + * @param string $beanClassName |
|
| 630 | + * |
|
| 631 | + * @return array first element: list of used beans, second item: PHP code as a string |
|
| 632 | + */ |
|
| 633 | + private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName) |
|
| 634 | + { |
|
| 635 | + $columns = $index->getColumns(); |
|
| 636 | + $usedBeans = []; |
|
| 637 | + |
|
| 638 | + /* |
|
| 639 | 639 | * The list of elements building this index (expressed as columns or foreign keys) |
| 640 | 640 | * @var AbstractBeanPropertyDescriptor[] |
| 641 | 641 | */ |
| 642 | - $elements = []; |
|
| 643 | - |
|
| 644 | - foreach ($columns as $column) { |
|
| 645 | - $fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column)); |
|
| 646 | - if ($fk !== null) { |
|
| 647 | - if (!in_array($fk, $elements)) { |
|
| 648 | - $elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer); |
|
| 649 | - } |
|
| 650 | - } else { |
|
| 651 | - $elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column)); |
|
| 652 | - } |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - // If the index is actually only a foreign key, let's bypass it entirely. |
|
| 656 | - if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) { |
|
| 657 | - return [[], '']; |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - $methodNameComponent = []; |
|
| 661 | - $functionParameters = []; |
|
| 662 | - $first = true; |
|
| 663 | - foreach ($elements as $element) { |
|
| 664 | - $methodNameComponent[] = $element->getUpperCamelCaseName(); |
|
| 665 | - $functionParameter = $element->getClassName(); |
|
| 666 | - if ($functionParameter) { |
|
| 667 | - $usedBeans[] = $beanNamespace.'\\'.$functionParameter; |
|
| 668 | - $functionParameter .= ' '; |
|
| 669 | - } |
|
| 670 | - $functionParameter .= $element->getVariableName(); |
|
| 671 | - if ($first) { |
|
| 672 | - $first = false; |
|
| 673 | - } else { |
|
| 674 | - $functionParameter .= ' = null'; |
|
| 675 | - } |
|
| 676 | - $functionParameters[] = $functionParameter; |
|
| 677 | - } |
|
| 678 | - if ($index->isUnique()) { |
|
| 679 | - $methodName = 'findOneBy'.implode('And', $methodNameComponent); |
|
| 680 | - $calledMethod = 'findOne'; |
|
| 681 | - } else { |
|
| 682 | - $methodName = 'findBy'.implode('And', $methodNameComponent); |
|
| 683 | - $calledMethod = 'find'; |
|
| 684 | - } |
|
| 685 | - $functionParametersString = implode(', ', $functionParameters); |
|
| 686 | - |
|
| 687 | - $count = 0; |
|
| 688 | - |
|
| 689 | - $params = []; |
|
| 690 | - $filterArrayCode = ''; |
|
| 691 | - $commentArguments = []; |
|
| 692 | - foreach ($elements as $element) { |
|
| 693 | - $params[] = $element->getParamAnnotation(); |
|
| 694 | - if ($element instanceof ScalarBeanPropertyDescriptor) { |
|
| 695 | - $filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n"; |
|
| 696 | - } else { |
|
| 697 | - ++$count; |
|
| 698 | - $filterArrayCode .= ' '.$count.' => '.$element->getVariableName().",\n"; |
|
| 699 | - } |
|
| 700 | - $commentArguments[] = substr($element->getVariableName(), 1); |
|
| 701 | - } |
|
| 702 | - $paramsString = implode("\n", $params); |
|
| 703 | - |
|
| 704 | - $code = " |
|
| 642 | + $elements = []; |
|
| 643 | + |
|
| 644 | + foreach ($columns as $column) { |
|
| 645 | + $fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column)); |
|
| 646 | + if ($fk !== null) { |
|
| 647 | + if (!in_array($fk, $elements)) { |
|
| 648 | + $elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer); |
|
| 649 | + } |
|
| 650 | + } else { |
|
| 651 | + $elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column)); |
|
| 652 | + } |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + // If the index is actually only a foreign key, let's bypass it entirely. |
|
| 656 | + if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) { |
|
| 657 | + return [[], '']; |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + $methodNameComponent = []; |
|
| 661 | + $functionParameters = []; |
|
| 662 | + $first = true; |
|
| 663 | + foreach ($elements as $element) { |
|
| 664 | + $methodNameComponent[] = $element->getUpperCamelCaseName(); |
|
| 665 | + $functionParameter = $element->getClassName(); |
|
| 666 | + if ($functionParameter) { |
|
| 667 | + $usedBeans[] = $beanNamespace.'\\'.$functionParameter; |
|
| 668 | + $functionParameter .= ' '; |
|
| 669 | + } |
|
| 670 | + $functionParameter .= $element->getVariableName(); |
|
| 671 | + if ($first) { |
|
| 672 | + $first = false; |
|
| 673 | + } else { |
|
| 674 | + $functionParameter .= ' = null'; |
|
| 675 | + } |
|
| 676 | + $functionParameters[] = $functionParameter; |
|
| 677 | + } |
|
| 678 | + if ($index->isUnique()) { |
|
| 679 | + $methodName = 'findOneBy'.implode('And', $methodNameComponent); |
|
| 680 | + $calledMethod = 'findOne'; |
|
| 681 | + } else { |
|
| 682 | + $methodName = 'findBy'.implode('And', $methodNameComponent); |
|
| 683 | + $calledMethod = 'find'; |
|
| 684 | + } |
|
| 685 | + $functionParametersString = implode(', ', $functionParameters); |
|
| 686 | + |
|
| 687 | + $count = 0; |
|
| 688 | + |
|
| 689 | + $params = []; |
|
| 690 | + $filterArrayCode = ''; |
|
| 691 | + $commentArguments = []; |
|
| 692 | + foreach ($elements as $element) { |
|
| 693 | + $params[] = $element->getParamAnnotation(); |
|
| 694 | + if ($element instanceof ScalarBeanPropertyDescriptor) { |
|
| 695 | + $filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n"; |
|
| 696 | + } else { |
|
| 697 | + ++$count; |
|
| 698 | + $filterArrayCode .= ' '.$count.' => '.$element->getVariableName().",\n"; |
|
| 699 | + } |
|
| 700 | + $commentArguments[] = substr($element->getVariableName(), 1); |
|
| 701 | + } |
|
| 702 | + $paramsString = implode("\n", $params); |
|
| 703 | + |
|
| 704 | + $code = " |
|
| 705 | 705 | /** |
| 706 | 706 | * Get a list of $beanClassName filtered by ".implode(', ', $commentArguments).". |
| 707 | 707 | * |
@@ -719,6 +719,6 @@ discard block |
||
| 719 | 719 | } |
| 720 | 720 | "; |
| 721 | 721 | |
| 722 | - return [$usedBeans, $code]; |
|
| 723 | - } |
|
| 722 | + return [$usedBeans, $code]; |
|
| 723 | + } |
|
| 724 | 724 | } |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | */ |
| 93 | 93 | public function getConstructorProperties() |
| 94 | 94 | { |
| 95 | - $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
| 95 | + $constructorProperties = array_filter($this->beanPropertyDescriptors, function(AbstractBeanPropertyDescriptor $property) { |
|
| 96 | 96 | return $property->isCompulsory(); |
| 97 | 97 | }); |
| 98 | 98 | |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | */ |
| 107 | 107 | public function getExposedProperties() |
| 108 | 108 | { |
| 109 | - $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
| 109 | + $exposedProperties = array_filter($this->beanPropertyDescriptors, function(AbstractBeanPropertyDescriptor $property) { |
|
| 110 | 110 | return $property->getTable()->getName() == $this->table->getName(); |
| 111 | 111 | }); |
| 112 | 112 | |
@@ -271,7 +271,7 @@ discard block |
||
| 271 | 271 | $fksByMethodName = []; |
| 272 | 272 | |
| 273 | 273 | foreach ($fksByTable as $tableName => $fksForTable) { |
| 274 | - if (count($fksForTable) > 1) { |
|
| 274 | + if (count($fksForTable)>1) { |
|
| 275 | 275 | foreach ($fksForTable as $fk) { |
| 276 | 276 | $methodName = 'get'.TDBMDaoGenerator::toCamelCase($fk->getLocalTableName()).'By'; |
| 277 | 277 | |
@@ -385,7 +385,7 @@ discard block |
||
| 385 | 385 | |
| 386 | 386 | $finalDescs = []; |
| 387 | 387 | foreach ($descs as $descArray) { |
| 388 | - if (count($descArray) > 1) { |
|
| 388 | + if (count($descArray)>1) { |
|
| 389 | 389 | foreach ($descArray as $desc) { |
| 390 | 390 | $desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($desc['table']->getName()); |
| 391 | 391 | $finalDescs[] = $desc; |
@@ -558,7 +558,7 @@ discard block |
||
| 558 | 558 | |
| 559 | 559 | $classes = $this->generateExtendsAndUseStatements($parentFk); |
| 560 | 560 | |
| 561 | - $uses = array_map(function ($className) use ($beannamespace) { return 'use '.$beannamespace.'\\'.$className.";\n"; }, $classes); |
|
| 561 | + $uses = array_map(function($className) use ($beannamespace) { return 'use '.$beannamespace.'\\'.$className.";\n"; }, $classes); |
|
| 562 | 562 | $use = implode('', $uses); |
| 563 | 563 | |
| 564 | 564 | if ($parentFk !== null) { |