@@ -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. |
@@ -527,12 +527,12 @@ discard block |
||
| 527 | 527 | { |
| 528 | 528 | "; |
| 529 | 529 | |
| 530 | - foreach ($tableList as $table) { |
|
| 531 | - $tableName = $table->getName(); |
|
| 532 | - $daoClassName = $this->getDaoNameFromTableName($tableName); |
|
| 533 | - $daoInstanceName = self::toVariableName($daoClassName); |
|
| 530 | + foreach ($tableList as $table) { |
|
| 531 | + $tableName = $table->getName(); |
|
| 532 | + $daoClassName = $this->getDaoNameFromTableName($tableName); |
|
| 533 | + $daoInstanceName = self::toVariableName($daoClassName); |
|
| 534 | 534 | |
| 535 | - $str .= ' /** |
|
| 535 | + $str .= ' /** |
|
| 536 | 536 | * @var '.$daoClassName.' |
| 537 | 537 | */ |
| 538 | 538 | private $'.$daoInstanceName.'; |
@@ -557,158 +557,158 @@ discard block |
||
| 557 | 557 | } |
| 558 | 558 | |
| 559 | 559 | '; |
| 560 | - } |
|
| 560 | + } |
|
| 561 | 561 | |
| 562 | - $str .= ' |
|
| 562 | + $str .= ' |
|
| 563 | 563 | } |
| 564 | 564 | '; |
| 565 | 565 | |
| 566 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($daoNamespace.'\\Generated\\'.$daoFactoryClassName); |
|
| 567 | - if (empty($possibleFileNames)) { |
|
| 568 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daoNamespace.'\\'.$daoFactoryClassName.'" is not autoloadable.'); |
|
| 569 | - } |
|
| 570 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 571 | - |
|
| 572 | - $this->ensureDirectoryExist($possibleFileName); |
|
| 573 | - file_put_contents($possibleFileName, $str); |
|
| 574 | - @chmod($possibleFileName, 0664); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
| 579 | - * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
| 580 | - * |
|
| 581 | - * @param $str string |
|
| 582 | - * |
|
| 583 | - * @return string |
|
| 584 | - */ |
|
| 585 | - public static function toCamelCase($str) |
|
| 586 | - { |
|
| 587 | - $str = strtoupper(substr($str, 0, 1)).substr($str, 1); |
|
| 588 | - while (true) { |
|
| 589 | - if (strpos($str, '_') === false && strpos($str, ' ') === false) { |
|
| 590 | - break; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - $pos = strpos($str, '_'); |
|
| 594 | - if ($pos === false) { |
|
| 595 | - $pos = strpos($str, ' '); |
|
| 596 | - } |
|
| 597 | - $before = substr($str, 0, $pos); |
|
| 598 | - $after = substr($str, $pos + 1); |
|
| 599 | - $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - return $str; |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - /** |
|
| 606 | - * Tries to put string to the singular form (if it is plural). |
|
| 607 | - * We assume the table names are in english. |
|
| 608 | - * |
|
| 609 | - * @param $str string |
|
| 610 | - * |
|
| 611 | - * @return string |
|
| 612 | - */ |
|
| 613 | - public static function toSingular($str) |
|
| 614 | - { |
|
| 615 | - return Inflector::singularize($str); |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - /** |
|
| 619 | - * Put the first letter of the string in lower case. |
|
| 620 | - * Very useful to transform a class name into a variable name. |
|
| 621 | - * |
|
| 622 | - * @param $str string |
|
| 623 | - * |
|
| 624 | - * @return string |
|
| 625 | - */ |
|
| 626 | - public static function toVariableName($str) |
|
| 627 | - { |
|
| 628 | - return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - /** |
|
| 632 | - * Ensures the file passed in parameter can be written in its directory. |
|
| 633 | - * |
|
| 634 | - * @param string $fileName |
|
| 635 | - * |
|
| 636 | - * @throws TDBMException |
|
| 637 | - */ |
|
| 638 | - private function ensureDirectoryExist($fileName) |
|
| 639 | - { |
|
| 640 | - $dirName = dirname($fileName); |
|
| 641 | - if (!file_exists($dirName)) { |
|
| 642 | - $old = umask(0); |
|
| 643 | - $result = mkdir($dirName, 0775, true); |
|
| 644 | - umask($old); |
|
| 645 | - if ($result === false) { |
|
| 646 | - throw new TDBMException("Unable to create directory: '".$dirName."'."); |
|
| 647 | - } |
|
| 648 | - } |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - /** |
|
| 652 | - * Absolute path to composer json file. |
|
| 653 | - * |
|
| 654 | - * @param string $composerFile |
|
| 655 | - */ |
|
| 656 | - public function setComposerFile($composerFile) |
|
| 657 | - { |
|
| 658 | - $this->rootPath = dirname($composerFile).'/'; |
|
| 659 | - $this->composerFile = basename($composerFile); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - /** |
|
| 663 | - * Transforms a DBAL type into a PHP type (for PHPDoc purpose). |
|
| 664 | - * |
|
| 665 | - * @param Type $type The DBAL type |
|
| 666 | - * |
|
| 667 | - * @return string The PHP type |
|
| 668 | - */ |
|
| 669 | - public static function dbalTypeToPhpType(Type $type) |
|
| 670 | - { |
|
| 671 | - $map = [ |
|
| 672 | - Type::TARRAY => 'array', |
|
| 673 | - Type::SIMPLE_ARRAY => 'array', |
|
| 674 | - Type::JSON_ARRAY => 'array', |
|
| 675 | - Type::BIGINT => 'string', |
|
| 676 | - Type::BOOLEAN => 'bool', |
|
| 677 | - Type::DATETIME => '\DateTimeInterface', |
|
| 678 | - Type::DATETIMETZ => '\DateTimeInterface', |
|
| 679 | - Type::DATE => '\DateTimeInterface', |
|
| 680 | - Type::TIME => '\DateTimeInterface', |
|
| 681 | - Type::DECIMAL => 'float', |
|
| 682 | - Type::INTEGER => 'int', |
|
| 683 | - Type::OBJECT => 'string', |
|
| 684 | - Type::SMALLINT => 'int', |
|
| 685 | - Type::STRING => 'string', |
|
| 686 | - Type::TEXT => 'string', |
|
| 687 | - Type::BINARY => 'string', |
|
| 688 | - Type::BLOB => 'string', |
|
| 689 | - Type::FLOAT => 'float', |
|
| 690 | - Type::GUID => 'string', |
|
| 691 | - ]; |
|
| 692 | - |
|
| 693 | - return isset($map[$type->getName()]) ? $map[$type->getName()] : $type->getName(); |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - /** |
|
| 697 | - * @param string $beanNamespace |
|
| 698 | - * |
|
| 699 | - * @return \string[] Returns a map mapping table name to beans name |
|
| 700 | - */ |
|
| 701 | - public function buildTableToBeanMap($beanNamespace) |
|
| 702 | - { |
|
| 703 | - $tableToBeanMap = []; |
|
| 704 | - |
|
| 705 | - $tables = $this->schema->getTables(); |
|
| 706 | - |
|
| 707 | - foreach ($tables as $table) { |
|
| 708 | - $tableName = $table->getName(); |
|
| 709 | - $tableToBeanMap[$tableName] = $beanNamespace.'\\'.self::getBeanNameFromTableName($tableName); |
|
| 710 | - } |
|
| 711 | - |
|
| 712 | - return $tableToBeanMap; |
|
| 713 | - } |
|
| 566 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($daoNamespace.'\\Generated\\'.$daoFactoryClassName); |
|
| 567 | + if (empty($possibleFileNames)) { |
|
| 568 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daoNamespace.'\\'.$daoFactoryClassName.'" is not autoloadable.'); |
|
| 569 | + } |
|
| 570 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 571 | + |
|
| 572 | + $this->ensureDirectoryExist($possibleFileName); |
|
| 573 | + file_put_contents($possibleFileName, $str); |
|
| 574 | + @chmod($possibleFileName, 0664); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
| 579 | + * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
| 580 | + * |
|
| 581 | + * @param $str string |
|
| 582 | + * |
|
| 583 | + * @return string |
|
| 584 | + */ |
|
| 585 | + public static function toCamelCase($str) |
|
| 586 | + { |
|
| 587 | + $str = strtoupper(substr($str, 0, 1)).substr($str, 1); |
|
| 588 | + while (true) { |
|
| 589 | + if (strpos($str, '_') === false && strpos($str, ' ') === false) { |
|
| 590 | + break; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + $pos = strpos($str, '_'); |
|
| 594 | + if ($pos === false) { |
|
| 595 | + $pos = strpos($str, ' '); |
|
| 596 | + } |
|
| 597 | + $before = substr($str, 0, $pos); |
|
| 598 | + $after = substr($str, $pos + 1); |
|
| 599 | + $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + return $str; |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + /** |
|
| 606 | + * Tries to put string to the singular form (if it is plural). |
|
| 607 | + * We assume the table names are in english. |
|
| 608 | + * |
|
| 609 | + * @param $str string |
|
| 610 | + * |
|
| 611 | + * @return string |
|
| 612 | + */ |
|
| 613 | + public static function toSingular($str) |
|
| 614 | + { |
|
| 615 | + return Inflector::singularize($str); |
|
| 616 | + } |
|
| 617 | + |
|
| 618 | + /** |
|
| 619 | + * Put the first letter of the string in lower case. |
|
| 620 | + * Very useful to transform a class name into a variable name. |
|
| 621 | + * |
|
| 622 | + * @param $str string |
|
| 623 | + * |
|
| 624 | + * @return string |
|
| 625 | + */ |
|
| 626 | + public static function toVariableName($str) |
|
| 627 | + { |
|
| 628 | + return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + /** |
|
| 632 | + * Ensures the file passed in parameter can be written in its directory. |
|
| 633 | + * |
|
| 634 | + * @param string $fileName |
|
| 635 | + * |
|
| 636 | + * @throws TDBMException |
|
| 637 | + */ |
|
| 638 | + private function ensureDirectoryExist($fileName) |
|
| 639 | + { |
|
| 640 | + $dirName = dirname($fileName); |
|
| 641 | + if (!file_exists($dirName)) { |
|
| 642 | + $old = umask(0); |
|
| 643 | + $result = mkdir($dirName, 0775, true); |
|
| 644 | + umask($old); |
|
| 645 | + if ($result === false) { |
|
| 646 | + throw new TDBMException("Unable to create directory: '".$dirName."'."); |
|
| 647 | + } |
|
| 648 | + } |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + /** |
|
| 652 | + * Absolute path to composer json file. |
|
| 653 | + * |
|
| 654 | + * @param string $composerFile |
|
| 655 | + */ |
|
| 656 | + public function setComposerFile($composerFile) |
|
| 657 | + { |
|
| 658 | + $this->rootPath = dirname($composerFile).'/'; |
|
| 659 | + $this->composerFile = basename($composerFile); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + /** |
|
| 663 | + * Transforms a DBAL type into a PHP type (for PHPDoc purpose). |
|
| 664 | + * |
|
| 665 | + * @param Type $type The DBAL type |
|
| 666 | + * |
|
| 667 | + * @return string The PHP type |
|
| 668 | + */ |
|
| 669 | + public static function dbalTypeToPhpType(Type $type) |
|
| 670 | + { |
|
| 671 | + $map = [ |
|
| 672 | + Type::TARRAY => 'array', |
|
| 673 | + Type::SIMPLE_ARRAY => 'array', |
|
| 674 | + Type::JSON_ARRAY => 'array', |
|
| 675 | + Type::BIGINT => 'string', |
|
| 676 | + Type::BOOLEAN => 'bool', |
|
| 677 | + Type::DATETIME => '\DateTimeInterface', |
|
| 678 | + Type::DATETIMETZ => '\DateTimeInterface', |
|
| 679 | + Type::DATE => '\DateTimeInterface', |
|
| 680 | + Type::TIME => '\DateTimeInterface', |
|
| 681 | + Type::DECIMAL => 'float', |
|
| 682 | + Type::INTEGER => 'int', |
|
| 683 | + Type::OBJECT => 'string', |
|
| 684 | + Type::SMALLINT => 'int', |
|
| 685 | + Type::STRING => 'string', |
|
| 686 | + Type::TEXT => 'string', |
|
| 687 | + Type::BINARY => 'string', |
|
| 688 | + Type::BLOB => 'string', |
|
| 689 | + Type::FLOAT => 'float', |
|
| 690 | + Type::GUID => 'string', |
|
| 691 | + ]; |
|
| 692 | + |
|
| 693 | + return isset($map[$type->getName()]) ? $map[$type->getName()] : $type->getName(); |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + /** |
|
| 697 | + * @param string $beanNamespace |
|
| 698 | + * |
|
| 699 | + * @return \string[] Returns a map mapping table name to beans name |
|
| 700 | + */ |
|
| 701 | + public function buildTableToBeanMap($beanNamespace) |
|
| 702 | + { |
|
| 703 | + $tableToBeanMap = []; |
|
| 704 | + |
|
| 705 | + $tables = $this->schema->getTables(); |
|
| 706 | + |
|
| 707 | + foreach ($tables as $table) { |
|
| 708 | + $tableName = $table->getName(); |
|
| 709 | + $tableToBeanMap[$tableName] = $beanNamespace.'\\'.self::getBeanNameFromTableName($tableName); |
|
| 710 | + } |
|
| 711 | + |
|
| 712 | + return $tableToBeanMap; |
|
| 713 | + } |
|
| 714 | 714 | } |
@@ -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 | */ |