@@ -11,98 +11,98 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | class ScalarBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var Column |
|
| 16 | - */ |
|
| 17 | - private $column; |
|
| 18 | - |
|
| 19 | - public function __construct(Table $table, Column $column) |
|
| 20 | - { |
|
| 21 | - parent::__construct($table); |
|
| 22 | - $this->table = $table; |
|
| 23 | - $this->column = $column; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Returns the foreign-key the column is part of, if any. null otherwise. |
|
| 28 | - * |
|
| 29 | - * @return ForeignKeyConstraint|null |
|
| 30 | - */ |
|
| 31 | - public function getForeignKey() |
|
| 32 | - { |
|
| 33 | - return false; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Returns the param annotation for this property (useful for constructor). |
|
| 38 | - * |
|
| 39 | - * @return string |
|
| 40 | - */ |
|
| 41 | - public function getParamAnnotation() |
|
| 42 | - { |
|
| 43 | - $className = $this->getClassName(); |
|
| 44 | - $paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType()); |
|
| 45 | - |
|
| 46 | - $str = ' * @param %s %s'; |
|
| 47 | - |
|
| 48 | - return sprintf($str, $paramType, $this->getVariableName()); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - public function getUpperCamelCaseName() |
|
| 52 | - { |
|
| 53 | - return TDBMDaoGenerator::toCamelCase($this->column->getName()); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
| 58 | - * |
|
| 59 | - * @return null|string |
|
| 60 | - */ |
|
| 61 | - public function getClassName() |
|
| 62 | - { |
|
| 63 | - return; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
| 68 | - * |
|
| 69 | - * @return bool |
|
| 70 | - */ |
|
| 71 | - public function isCompulsory() |
|
| 72 | - { |
|
| 73 | - return $this->column->getNotnull() && !$this->column->getAutoincrement(); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Returns true if the property is the primary key. |
|
| 78 | - * |
|
| 79 | - * @return bool |
|
| 80 | - */ |
|
| 81 | - public function isPrimaryKey() |
|
| 82 | - { |
|
| 83 | - return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns()); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Returns the PHP code for getters and setters. |
|
| 88 | - * |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public function getGetterSetterCode() |
|
| 92 | - { |
|
| 93 | - $type = $this->column->getType(); |
|
| 94 | - $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
| 95 | - |
|
| 96 | - $columnGetterName = $this->getGetterName(); |
|
| 97 | - $columnSetterName = $this->getSetterName(); |
|
| 98 | - |
|
| 99 | - if ($normalizedType == '\\DateTimeInterface') { |
|
| 100 | - $castTo = '\\DateTimeInterface '; |
|
| 101 | - } else { |
|
| 102 | - $castTo = ''; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $getterAndSetterCode = ' /** |
|
| 14 | + /** |
|
| 15 | + * @var Column |
|
| 16 | + */ |
|
| 17 | + private $column; |
|
| 18 | + |
|
| 19 | + public function __construct(Table $table, Column $column) |
|
| 20 | + { |
|
| 21 | + parent::__construct($table); |
|
| 22 | + $this->table = $table; |
|
| 23 | + $this->column = $column; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Returns the foreign-key the column is part of, if any. null otherwise. |
|
| 28 | + * |
|
| 29 | + * @return ForeignKeyConstraint|null |
|
| 30 | + */ |
|
| 31 | + public function getForeignKey() |
|
| 32 | + { |
|
| 33 | + return false; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Returns the param annotation for this property (useful for constructor). |
|
| 38 | + * |
|
| 39 | + * @return string |
|
| 40 | + */ |
|
| 41 | + public function getParamAnnotation() |
|
| 42 | + { |
|
| 43 | + $className = $this->getClassName(); |
|
| 44 | + $paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType()); |
|
| 45 | + |
|
| 46 | + $str = ' * @param %s %s'; |
|
| 47 | + |
|
| 48 | + return sprintf($str, $paramType, $this->getVariableName()); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + public function getUpperCamelCaseName() |
|
| 52 | + { |
|
| 53 | + return TDBMDaoGenerator::toCamelCase($this->column->getName()); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
| 58 | + * |
|
| 59 | + * @return null|string |
|
| 60 | + */ |
|
| 61 | + public function getClassName() |
|
| 62 | + { |
|
| 63 | + return; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
| 68 | + * |
|
| 69 | + * @return bool |
|
| 70 | + */ |
|
| 71 | + public function isCompulsory() |
|
| 72 | + { |
|
| 73 | + return $this->column->getNotnull() && !$this->column->getAutoincrement(); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Returns true if the property is the primary key. |
|
| 78 | + * |
|
| 79 | + * @return bool |
|
| 80 | + */ |
|
| 81 | + public function isPrimaryKey() |
|
| 82 | + { |
|
| 83 | + return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns()); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Returns the PHP code for getters and setters. |
|
| 88 | + * |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public function getGetterSetterCode() |
|
| 92 | + { |
|
| 93 | + $type = $this->column->getType(); |
|
| 94 | + $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
| 95 | + |
|
| 96 | + $columnGetterName = $this->getGetterName(); |
|
| 97 | + $columnSetterName = $this->getSetterName(); |
|
| 98 | + |
|
| 99 | + if ($normalizedType == '\\DateTimeInterface') { |
|
| 100 | + $castTo = '\\DateTimeInterface '; |
|
| 101 | + } else { |
|
| 102 | + $castTo = ''; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $getterAndSetterCode = ' /** |
|
| 106 | 106 | * The getter for the "%s" column. |
| 107 | 107 | * |
| 108 | 108 | * @return %s |
@@ -122,50 +122,50 @@ discard block |
||
| 122 | 122 | |
| 123 | 123 | '; |
| 124 | 124 | |
| 125 | - return sprintf($getterAndSetterCode, |
|
| 126 | - // Getter |
|
| 127 | - $this->column->getName(), |
|
| 128 | - $normalizedType, |
|
| 129 | - $columnGetterName, |
|
| 130 | - var_export($this->column->getName(), true), |
|
| 131 | - var_export($this->table->getName(), true), |
|
| 132 | - // Setter |
|
| 133 | - $this->column->getName(), |
|
| 134 | - $normalizedType, |
|
| 135 | - $this->column->getName(), |
|
| 136 | - $columnSetterName, |
|
| 137 | - $castTo, |
|
| 138 | - $this->column->getName(), |
|
| 139 | - var_export($this->column->getName(), true), |
|
| 140 | - $this->column->getName(), |
|
| 141 | - var_export($this->table->getName(), true) |
|
| 142 | - ); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Returns the part of code useful when doing json serialization. |
|
| 147 | - * |
|
| 148 | - * @return string |
|
| 149 | - */ |
|
| 150 | - public function getJsonSerializeCode() |
|
| 151 | - { |
|
| 152 | - $type = $this->column->getType(); |
|
| 153 | - $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
| 154 | - |
|
| 155 | - if ($normalizedType == '\\DateTimeInterface') { |
|
| 156 | - return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."()->format('c');\n"; |
|
| 157 | - } else { |
|
| 158 | - return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n"; |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Returns the column name. |
|
| 164 | - * |
|
| 165 | - * @return string |
|
| 166 | - */ |
|
| 167 | - public function getColumnName() |
|
| 168 | - { |
|
| 169 | - return $this->column->getName(); |
|
| 170 | - } |
|
| 125 | + return sprintf($getterAndSetterCode, |
|
| 126 | + // Getter |
|
| 127 | + $this->column->getName(), |
|
| 128 | + $normalizedType, |
|
| 129 | + $columnGetterName, |
|
| 130 | + var_export($this->column->getName(), true), |
|
| 131 | + var_export($this->table->getName(), true), |
|
| 132 | + // Setter |
|
| 133 | + $this->column->getName(), |
|
| 134 | + $normalizedType, |
|
| 135 | + $this->column->getName(), |
|
| 136 | + $columnSetterName, |
|
| 137 | + $castTo, |
|
| 138 | + $this->column->getName(), |
|
| 139 | + var_export($this->column->getName(), true), |
|
| 140 | + $this->column->getName(), |
|
| 141 | + var_export($this->table->getName(), true) |
|
| 142 | + ); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Returns the part of code useful when doing json serialization. |
|
| 147 | + * |
|
| 148 | + * @return string |
|
| 149 | + */ |
|
| 150 | + public function getJsonSerializeCode() |
|
| 151 | + { |
|
| 152 | + $type = $this->column->getType(); |
|
| 153 | + $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
| 154 | + |
|
| 155 | + if ($normalizedType == '\\DateTimeInterface') { |
|
| 156 | + return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."()->format('c');\n"; |
|
| 157 | + } else { |
|
| 158 | + return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n"; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Returns the column name. |
|
| 164 | + * |
|
| 165 | + * @return string |
|
| 166 | + */ |
|
| 167 | + public function getColumnName() |
|
| 168 | + { |
|
| 169 | + return $this->column->getName(); |
|
| 170 | + } |
|
| 171 | 171 | } |
@@ -17,197 +17,197 @@ 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 | - * @var TDBMSchemaAnalyzer |
|
| 39 | - */ |
|
| 40 | - private $tdbmSchemaAnalyzer; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Constructor. |
|
| 44 | - * |
|
| 45 | - * @param SchemaAnalyzer $schemaAnalyzer |
|
| 46 | - * @param Schema $schema |
|
| 47 | - * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
|
| 48 | - */ |
|
| 49 | - public function __construct(SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
| 50 | - { |
|
| 51 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 52 | - $this->schema = $schema; |
|
| 53 | - $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
| 54 | - $this->rootPath = __DIR__.'/../../../../../../../../'; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Generates all the daos and beans. |
|
| 59 | - * |
|
| 60 | - * @param string $daoFactoryClassName The classe name of the DAO factory |
|
| 61 | - * @param string $daonamespace The namespace for the DAOs, without trailing \ |
|
| 62 | - * @param string $beannamespace The Namespace for the beans, without trailing \ |
|
| 63 | - * @param bool $storeInUtc If the generated daos should store the date in UTC timezone instead of user's timezone. |
|
| 64 | - * |
|
| 65 | - * @return \string[] the list of tables |
|
| 66 | - * |
|
| 67 | - * @throws TDBMException |
|
| 68 | - */ |
|
| 69 | - public function generateAllDaosAndBeans($daoFactoryClassName, $daonamespace, $beannamespace, $storeInUtc) |
|
| 70 | - { |
|
| 71 | - // TODO: extract ClassNameMapper in its own package! |
|
| 72 | - $classNameMapper = ClassNameMapper::createFromComposerFile($this->rootPath.'composer.json'); |
|
| 73 | - |
|
| 74 | - // TODO: check that no class name ends with "Base". Otherwise, there will be name clash. |
|
| 75 | - |
|
| 76 | - $tableList = $this->schema->getTables(); |
|
| 77 | - |
|
| 78 | - // Remove all beans and daos from junction tables |
|
| 79 | - $junctionTables = $this->schemaAnalyzer->detectJunctionTables(); |
|
| 80 | - $junctionTableNames = array_map(function (Table $table) { |
|
| 81 | - return $table->getName(); |
|
| 82 | - }, $junctionTables); |
|
| 83 | - |
|
| 84 | - $tableList = array_filter($tableList, function (Table $table) use ($junctionTableNames) { |
|
| 85 | - return !in_array($table->getName(), $junctionTableNames); |
|
| 86 | - }); |
|
| 87 | - |
|
| 88 | - foreach ($tableList as $table) { |
|
| 89 | - $this->generateDaoAndBean($table, $daonamespace, $beannamespace, $classNameMapper, $storeInUtc); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $this->generateFactory($tableList, $daoFactoryClassName, $daonamespace, $classNameMapper); |
|
| 93 | - |
|
| 94 | - // Ok, let's return the list of all tables. |
|
| 95 | - // These will be used by the calling script to create Mouf instances. |
|
| 96 | - |
|
| 97 | - return array_map(function (Table $table) { return $table->getName(); }, $tableList); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Generates in one method call the daos and the beans for one table. |
|
| 102 | - * |
|
| 103 | - * @param Table $table |
|
| 104 | - * @param string $daonamespace |
|
| 105 | - * @param string $beannamespace |
|
| 106 | - * @param ClassNameMapper $classNameMapper |
|
| 107 | - * @param bool $storeInUtc |
|
| 108 | - * |
|
| 109 | - * @throws TDBMException |
|
| 110 | - */ |
|
| 111 | - public function generateDaoAndBean(Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc) |
|
| 112 | - { |
|
| 113 | - $tableName = $table->getName(); |
|
| 114 | - $daoName = $this->getDaoNameFromTableName($tableName); |
|
| 115 | - $beanName = $this->getBeanNameFromTableName($tableName); |
|
| 116 | - $baseBeanName = $this->getBaseBeanNameFromTableName($tableName); |
|
| 117 | - $baseDaoName = $this->getBaseDaoNameFromTableName($tableName); |
|
| 118 | - |
|
| 119 | - $beanDescriptor = new BeanDescriptor($table, $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer); |
|
| 120 | - |
|
| 121 | - $this->generateBean($beanDescriptor, $beanName, $baseBeanName, $table, $beannamespace, $classNameMapper, $storeInUtc); |
|
| 122 | - $this->generateDao($beanDescriptor, $daoName, $baseDaoName, $beanName, $table, $daonamespace, $beannamespace, $classNameMapper); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Returns the name of the bean class from the table name. |
|
| 127 | - * |
|
| 128 | - * @param $tableName |
|
| 129 | - * |
|
| 130 | - * @return string |
|
| 131 | - */ |
|
| 132 | - public static function getBeanNameFromTableName($tableName) |
|
| 133 | - { |
|
| 134 | - return self::toSingular(self::toCamelCase($tableName)).'Bean'; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Returns the name of the DAO class from the table name. |
|
| 139 | - * |
|
| 140 | - * @param $tableName |
|
| 141 | - * |
|
| 142 | - * @return string |
|
| 143 | - */ |
|
| 144 | - public static function getDaoNameFromTableName($tableName) |
|
| 145 | - { |
|
| 146 | - return self::toSingular(self::toCamelCase($tableName)).'Dao'; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Returns the name of the base bean class from the table name. |
|
| 151 | - * |
|
| 152 | - * @param $tableName |
|
| 153 | - * |
|
| 154 | - * @return string |
|
| 155 | - */ |
|
| 156 | - public static function getBaseBeanNameFromTableName($tableName) |
|
| 157 | - { |
|
| 158 | - return self::toSingular(self::toCamelCase($tableName)).'BaseBean'; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Returns the name of the base DAO class from the table name. |
|
| 163 | - * |
|
| 164 | - * @param $tableName |
|
| 165 | - * |
|
| 166 | - * @return string |
|
| 167 | - */ |
|
| 168 | - public static function getBaseDaoNameFromTableName($tableName) |
|
| 169 | - { |
|
| 170 | - return self::toSingular(self::toCamelCase($tableName)).'BaseDao'; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 175 | - * |
|
| 176 | - * @param BeanDescriptor $beanDescriptor |
|
| 177 | - * @param string $className The name of the class |
|
| 178 | - * @param string $baseClassName The name of the base class which will be extended (name only, no directory) |
|
| 179 | - * @param Table $table The table |
|
| 180 | - * @param string $beannamespace The namespace of the bean |
|
| 181 | - * @param ClassNameMapper $classNameMapper |
|
| 182 | - * |
|
| 183 | - * @throws TDBMException |
|
| 184 | - */ |
|
| 185 | - public function generateBean(BeanDescriptor $beanDescriptor, $className, $baseClassName, Table $table, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc) |
|
| 186 | - { |
|
| 187 | - $str = $beanDescriptor->generatePhpCode($beannamespace); |
|
| 188 | - |
|
| 189 | - $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\'.$baseClassName); |
|
| 190 | - if (empty($possibleBaseFileNames)) { |
|
| 191 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$baseClassName.'" is not autoloadable.'); |
|
| 192 | - } |
|
| 193 | - $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 194 | - |
|
| 195 | - $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 196 | - file_put_contents($possibleBaseFileName, $str); |
|
| 197 | - @chmod($possibleBaseFileName, 0664); |
|
| 198 | - |
|
| 199 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\'.$className); |
|
| 200 | - if (empty($possibleFileNames)) { |
|
| 201 | - // @codeCoverageIgnoreStart |
|
| 202 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$className.'" is not autoloadable.'); |
|
| 203 | - // @codeCoverageIgnoreEnd |
|
| 204 | - } |
|
| 205 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 206 | - |
|
| 207 | - if (!file_exists($possibleFileName)) { |
|
| 208 | - $tableName = $table->getName(); |
|
| 209 | - |
|
| 210 | - $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 | + * @var TDBMSchemaAnalyzer |
|
| 39 | + */ |
|
| 40 | + private $tdbmSchemaAnalyzer; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Constructor. |
|
| 44 | + * |
|
| 45 | + * @param SchemaAnalyzer $schemaAnalyzer |
|
| 46 | + * @param Schema $schema |
|
| 47 | + * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
|
| 48 | + */ |
|
| 49 | + public function __construct(SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
| 50 | + { |
|
| 51 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 52 | + $this->schema = $schema; |
|
| 53 | + $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
| 54 | + $this->rootPath = __DIR__.'/../../../../../../../../'; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Generates all the daos and beans. |
|
| 59 | + * |
|
| 60 | + * @param string $daoFactoryClassName The classe name of the DAO factory |
|
| 61 | + * @param string $daonamespace The namespace for the DAOs, without trailing \ |
|
| 62 | + * @param string $beannamespace The Namespace for the beans, without trailing \ |
|
| 63 | + * @param bool $storeInUtc If the generated daos should store the date in UTC timezone instead of user's timezone. |
|
| 64 | + * |
|
| 65 | + * @return \string[] the list of tables |
|
| 66 | + * |
|
| 67 | + * @throws TDBMException |
|
| 68 | + */ |
|
| 69 | + public function generateAllDaosAndBeans($daoFactoryClassName, $daonamespace, $beannamespace, $storeInUtc) |
|
| 70 | + { |
|
| 71 | + // TODO: extract ClassNameMapper in its own package! |
|
| 72 | + $classNameMapper = ClassNameMapper::createFromComposerFile($this->rootPath.'composer.json'); |
|
| 73 | + |
|
| 74 | + // TODO: check that no class name ends with "Base". Otherwise, there will be name clash. |
|
| 75 | + |
|
| 76 | + $tableList = $this->schema->getTables(); |
|
| 77 | + |
|
| 78 | + // Remove all beans and daos from junction tables |
|
| 79 | + $junctionTables = $this->schemaAnalyzer->detectJunctionTables(); |
|
| 80 | + $junctionTableNames = array_map(function (Table $table) { |
|
| 81 | + return $table->getName(); |
|
| 82 | + }, $junctionTables); |
|
| 83 | + |
|
| 84 | + $tableList = array_filter($tableList, function (Table $table) use ($junctionTableNames) { |
|
| 85 | + return !in_array($table->getName(), $junctionTableNames); |
|
| 86 | + }); |
|
| 87 | + |
|
| 88 | + foreach ($tableList as $table) { |
|
| 89 | + $this->generateDaoAndBean($table, $daonamespace, $beannamespace, $classNameMapper, $storeInUtc); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $this->generateFactory($tableList, $daoFactoryClassName, $daonamespace, $classNameMapper); |
|
| 93 | + |
|
| 94 | + // Ok, let's return the list of all tables. |
|
| 95 | + // These will be used by the calling script to create Mouf instances. |
|
| 96 | + |
|
| 97 | + return array_map(function (Table $table) { return $table->getName(); }, $tableList); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Generates in one method call the daos and the beans for one table. |
|
| 102 | + * |
|
| 103 | + * @param Table $table |
|
| 104 | + * @param string $daonamespace |
|
| 105 | + * @param string $beannamespace |
|
| 106 | + * @param ClassNameMapper $classNameMapper |
|
| 107 | + * @param bool $storeInUtc |
|
| 108 | + * |
|
| 109 | + * @throws TDBMException |
|
| 110 | + */ |
|
| 111 | + public function generateDaoAndBean(Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc) |
|
| 112 | + { |
|
| 113 | + $tableName = $table->getName(); |
|
| 114 | + $daoName = $this->getDaoNameFromTableName($tableName); |
|
| 115 | + $beanName = $this->getBeanNameFromTableName($tableName); |
|
| 116 | + $baseBeanName = $this->getBaseBeanNameFromTableName($tableName); |
|
| 117 | + $baseDaoName = $this->getBaseDaoNameFromTableName($tableName); |
|
| 118 | + |
|
| 119 | + $beanDescriptor = new BeanDescriptor($table, $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer); |
|
| 120 | + |
|
| 121 | + $this->generateBean($beanDescriptor, $beanName, $baseBeanName, $table, $beannamespace, $classNameMapper, $storeInUtc); |
|
| 122 | + $this->generateDao($beanDescriptor, $daoName, $baseDaoName, $beanName, $table, $daonamespace, $beannamespace, $classNameMapper); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Returns the name of the bean class from the table name. |
|
| 127 | + * |
|
| 128 | + * @param $tableName |
|
| 129 | + * |
|
| 130 | + * @return string |
|
| 131 | + */ |
|
| 132 | + public static function getBeanNameFromTableName($tableName) |
|
| 133 | + { |
|
| 134 | + return self::toSingular(self::toCamelCase($tableName)).'Bean'; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Returns the name of the DAO class from the table name. |
|
| 139 | + * |
|
| 140 | + * @param $tableName |
|
| 141 | + * |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + public static function getDaoNameFromTableName($tableName) |
|
| 145 | + { |
|
| 146 | + return self::toSingular(self::toCamelCase($tableName)).'Dao'; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Returns the name of the base bean class from the table name. |
|
| 151 | + * |
|
| 152 | + * @param $tableName |
|
| 153 | + * |
|
| 154 | + * @return string |
|
| 155 | + */ |
|
| 156 | + public static function getBaseBeanNameFromTableName($tableName) |
|
| 157 | + { |
|
| 158 | + return self::toSingular(self::toCamelCase($tableName)).'BaseBean'; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Returns the name of the base DAO class from the table name. |
|
| 163 | + * |
|
| 164 | + * @param $tableName |
|
| 165 | + * |
|
| 166 | + * @return string |
|
| 167 | + */ |
|
| 168 | + public static function getBaseDaoNameFromTableName($tableName) |
|
| 169 | + { |
|
| 170 | + return self::toSingular(self::toCamelCase($tableName)).'BaseDao'; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 175 | + * |
|
| 176 | + * @param BeanDescriptor $beanDescriptor |
|
| 177 | + * @param string $className The name of the class |
|
| 178 | + * @param string $baseClassName The name of the base class which will be extended (name only, no directory) |
|
| 179 | + * @param Table $table The table |
|
| 180 | + * @param string $beannamespace The namespace of the bean |
|
| 181 | + * @param ClassNameMapper $classNameMapper |
|
| 182 | + * |
|
| 183 | + * @throws TDBMException |
|
| 184 | + */ |
|
| 185 | + public function generateBean(BeanDescriptor $beanDescriptor, $className, $baseClassName, Table $table, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc) |
|
| 186 | + { |
|
| 187 | + $str = $beanDescriptor->generatePhpCode($beannamespace); |
|
| 188 | + |
|
| 189 | + $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\'.$baseClassName); |
|
| 190 | + if (empty($possibleBaseFileNames)) { |
|
| 191 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$baseClassName.'" is not autoloadable.'); |
|
| 192 | + } |
|
| 193 | + $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 194 | + |
|
| 195 | + $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 196 | + file_put_contents($possibleBaseFileName, $str); |
|
| 197 | + @chmod($possibleBaseFileName, 0664); |
|
| 198 | + |
|
| 199 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\'.$className); |
|
| 200 | + if (empty($possibleFileNames)) { |
|
| 201 | + // @codeCoverageIgnoreStart |
|
| 202 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$className.'" is not autoloadable.'); |
|
| 203 | + // @codeCoverageIgnoreEnd |
|
| 204 | + } |
|
| 205 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 206 | + |
|
| 207 | + if (!file_exists($possibleFileName)) { |
|
| 208 | + $tableName = $table->getName(); |
|
| 209 | + |
|
| 210 | + $str = "<?php |
|
| 211 | 211 | /* |
| 212 | 212 | * This file has been automatically generated by TDBM. |
| 213 | 213 | * You can edit this file as it will not be overwritten. |
@@ -222,67 +222,67 @@ discard block |
||
| 222 | 222 | { |
| 223 | 223 | |
| 224 | 224 | }"; |
| 225 | - $this->ensureDirectoryExist($possibleFileName); |
|
| 226 | - file_put_contents($possibleFileName, $str); |
|
| 227 | - @chmod($possibleFileName, 0664); |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Tries to find a @defaultSort annotation in one of the columns. |
|
| 233 | - * |
|
| 234 | - * @param Table $table |
|
| 235 | - * |
|
| 236 | - * @return array First item: column name, Second item: column order (asc/desc) |
|
| 237 | - */ |
|
| 238 | - private function getDefaultSortColumnFromAnnotation(Table $table) |
|
| 239 | - { |
|
| 240 | - $defaultSort = null; |
|
| 241 | - $defaultSortDirection = null; |
|
| 242 | - foreach ($table->getColumns() as $column) { |
|
| 243 | - $comments = $column->getComment(); |
|
| 244 | - $matches = array(); |
|
| 245 | - if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) { |
|
| 246 | - $defaultSort = $column->getName(); |
|
| 247 | - if (count($matches) === 3) { |
|
| 248 | - $defaultSortDirection = $matches[2]; |
|
| 249 | - } else { |
|
| 250 | - $defaultSortDirection = 'ASC'; |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - return [$defaultSort, $defaultSortDirection]; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Writes the PHP bean DAO with simple functions to create/get/save objects. |
|
| 260 | - * |
|
| 261 | - * @param BeanDescriptor $beanDescriptor |
|
| 262 | - * @param string $className The name of the class |
|
| 263 | - * @param string $baseClassName |
|
| 264 | - * @param string $beanClassName |
|
| 265 | - * @param Table $table |
|
| 266 | - * @param string $daonamespace |
|
| 267 | - * @param string $beannamespace |
|
| 268 | - * @param ClassNameMapper $classNameMapper |
|
| 269 | - * |
|
| 270 | - * @throws TDBMException |
|
| 271 | - */ |
|
| 272 | - public function generateDao(BeanDescriptor $beanDescriptor, $className, $baseClassName, $beanClassName, Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper) |
|
| 273 | - { |
|
| 274 | - $tableName = $table->getName(); |
|
| 275 | - $primaryKeyColumns = $table->getPrimaryKeyColumns(); |
|
| 276 | - |
|
| 277 | - list($defaultSort, $defaultSortDirection) = $this->getDefaultSortColumnFromAnnotation($table); |
|
| 278 | - |
|
| 279 | - // FIXME: lowercase tables with _ in the name should work! |
|
| 280 | - $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
|
| 281 | - |
|
| 282 | - $beanClassWithoutNameSpace = $beanClassName; |
|
| 283 | - $beanClassName = $beannamespace.'\\'.$beanClassName; |
|
| 284 | - |
|
| 285 | - $str = "<?php |
|
| 225 | + $this->ensureDirectoryExist($possibleFileName); |
|
| 226 | + file_put_contents($possibleFileName, $str); |
|
| 227 | + @chmod($possibleFileName, 0664); |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Tries to find a @defaultSort annotation in one of the columns. |
|
| 233 | + * |
|
| 234 | + * @param Table $table |
|
| 235 | + * |
|
| 236 | + * @return array First item: column name, Second item: column order (asc/desc) |
|
| 237 | + */ |
|
| 238 | + private function getDefaultSortColumnFromAnnotation(Table $table) |
|
| 239 | + { |
|
| 240 | + $defaultSort = null; |
|
| 241 | + $defaultSortDirection = null; |
|
| 242 | + foreach ($table->getColumns() as $column) { |
|
| 243 | + $comments = $column->getComment(); |
|
| 244 | + $matches = array(); |
|
| 245 | + if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) { |
|
| 246 | + $defaultSort = $column->getName(); |
|
| 247 | + if (count($matches) === 3) { |
|
| 248 | + $defaultSortDirection = $matches[2]; |
|
| 249 | + } else { |
|
| 250 | + $defaultSortDirection = 'ASC'; |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + return [$defaultSort, $defaultSortDirection]; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Writes the PHP bean DAO with simple functions to create/get/save objects. |
|
| 260 | + * |
|
| 261 | + * @param BeanDescriptor $beanDescriptor |
|
| 262 | + * @param string $className The name of the class |
|
| 263 | + * @param string $baseClassName |
|
| 264 | + * @param string $beanClassName |
|
| 265 | + * @param Table $table |
|
| 266 | + * @param string $daonamespace |
|
| 267 | + * @param string $beannamespace |
|
| 268 | + * @param ClassNameMapper $classNameMapper |
|
| 269 | + * |
|
| 270 | + * @throws TDBMException |
|
| 271 | + */ |
|
| 272 | + public function generateDao(BeanDescriptor $beanDescriptor, $className, $baseClassName, $beanClassName, Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper) |
|
| 273 | + { |
|
| 274 | + $tableName = $table->getName(); |
|
| 275 | + $primaryKeyColumns = $table->getPrimaryKeyColumns(); |
|
| 276 | + |
|
| 277 | + list($defaultSort, $defaultSortDirection) = $this->getDefaultSortColumnFromAnnotation($table); |
|
| 278 | + |
|
| 279 | + // FIXME: lowercase tables with _ in the name should work! |
|
| 280 | + $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
|
| 281 | + |
|
| 282 | + $beanClassWithoutNameSpace = $beanClassName; |
|
| 283 | + $beanClassName = $beannamespace.'\\'.$beanClassName; |
|
| 284 | + |
|
| 285 | + $str = "<?php |
|
| 286 | 286 | |
| 287 | 287 | /* |
| 288 | 288 | * This file has been automatically generated by TDBM. |
@@ -359,9 +359,9 @@ discard block |
||
| 359 | 359 | } |
| 360 | 360 | "; |
| 361 | 361 | |
| 362 | - if (count($primaryKeyColumns) === 1) { |
|
| 363 | - $primaryKeyColumn = $primaryKeyColumns[0]; |
|
| 364 | - $str .= " |
|
| 362 | + if (count($primaryKeyColumns) === 1) { |
|
| 363 | + $primaryKeyColumn = $primaryKeyColumns[0]; |
|
| 364 | + $str .= " |
|
| 365 | 365 | /** |
| 366 | 366 | * Get $beanClassWithoutNameSpace specified by its ID (its primary key) |
| 367 | 367 | * If the primary key does not exist, an exception is thrown. |
@@ -376,8 +376,8 @@ discard block |
||
| 376 | 376 | return \$this->tdbmService->findObjectByPk('$tableName', ['$primaryKeyColumn' => \$id], [], \$lazyLoading); |
| 377 | 377 | } |
| 378 | 378 | "; |
| 379 | - } |
|
| 380 | - $str .= " |
|
| 379 | + } |
|
| 380 | + $str .= " |
|
| 381 | 381 | /** |
| 382 | 382 | * Deletes the $beanClassWithoutNameSpace passed in parameter. |
| 383 | 383 | * |
@@ -435,34 +435,34 @@ discard block |
||
| 435 | 435 | } |
| 436 | 436 | "; |
| 437 | 437 | |
| 438 | - $str .= $beanDescriptor->generateFindByDaoCode(); |
|
| 439 | - $str .= ' |
|
| 438 | + $str .= $beanDescriptor->generateFindByDaoCode(); |
|
| 439 | + $str .= ' |
|
| 440 | 440 | } |
| 441 | 441 | '; |
| 442 | 442 | |
| 443 | - $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\'.$baseClassName); |
|
| 444 | - if (empty($possibleBaseFileNames)) { |
|
| 445 | - // @codeCoverageIgnoreStart |
|
| 446 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$baseClassName.'" is not autoloadable.'); |
|
| 447 | - // @codeCoverageIgnoreEnd |
|
| 448 | - } |
|
| 449 | - $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 450 | - |
|
| 451 | - $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 452 | - file_put_contents($possibleBaseFileName, $str); |
|
| 453 | - @chmod($possibleBaseFileName, 0664); |
|
| 454 | - |
|
| 455 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\'.$className); |
|
| 456 | - if (empty($possibleFileNames)) { |
|
| 457 | - // @codeCoverageIgnoreStart |
|
| 458 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$className.'" is not autoloadable.'); |
|
| 459 | - // @codeCoverageIgnoreEnd |
|
| 460 | - } |
|
| 461 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 462 | - |
|
| 463 | - // Now, let's generate the "editable" class |
|
| 464 | - if (!file_exists($possibleFileName)) { |
|
| 465 | - $str = "<?php |
|
| 443 | + $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\'.$baseClassName); |
|
| 444 | + if (empty($possibleBaseFileNames)) { |
|
| 445 | + // @codeCoverageIgnoreStart |
|
| 446 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$baseClassName.'" is not autoloadable.'); |
|
| 447 | + // @codeCoverageIgnoreEnd |
|
| 448 | + } |
|
| 449 | + $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 450 | + |
|
| 451 | + $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 452 | + file_put_contents($possibleBaseFileName, $str); |
|
| 453 | + @chmod($possibleBaseFileName, 0664); |
|
| 454 | + |
|
| 455 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\'.$className); |
|
| 456 | + if (empty($possibleFileNames)) { |
|
| 457 | + // @codeCoverageIgnoreStart |
|
| 458 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$className.'" is not autoloadable.'); |
|
| 459 | + // @codeCoverageIgnoreEnd |
|
| 460 | + } |
|
| 461 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 462 | + |
|
| 463 | + // Now, let's generate the "editable" class |
|
| 464 | + if (!file_exists($possibleFileName)) { |
|
| 465 | + $str = "<?php |
|
| 466 | 466 | |
| 467 | 467 | /* |
| 468 | 468 | * This file has been automatically generated by TDBM. |
@@ -479,22 +479,22 @@ discard block |
||
| 479 | 479 | |
| 480 | 480 | } |
| 481 | 481 | "; |
| 482 | - $this->ensureDirectoryExist($possibleFileName); |
|
| 483 | - file_put_contents($possibleFileName, $str); |
|
| 484 | - @chmod($possibleFileName, 0664); |
|
| 485 | - } |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * Generates the factory bean. |
|
| 490 | - * |
|
| 491 | - * @param Table[] $tableList |
|
| 492 | - */ |
|
| 493 | - private function generateFactory(array $tableList, $daoFactoryClassName, $daoNamespace, ClassNameMapper $classNameMapper) |
|
| 494 | - { |
|
| 495 | - // For each table, let's write a property. |
|
| 496 | - |
|
| 497 | - $str = "<?php |
|
| 482 | + $this->ensureDirectoryExist($possibleFileName); |
|
| 483 | + file_put_contents($possibleFileName, $str); |
|
| 484 | + @chmod($possibleFileName, 0664); |
|
| 485 | + } |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * Generates the factory bean. |
|
| 490 | + * |
|
| 491 | + * @param Table[] $tableList |
|
| 492 | + */ |
|
| 493 | + private function generateFactory(array $tableList, $daoFactoryClassName, $daoNamespace, ClassNameMapper $classNameMapper) |
|
| 494 | + { |
|
| 495 | + // For each table, let's write a property. |
|
| 496 | + |
|
| 497 | + $str = "<?php |
|
| 498 | 498 | |
| 499 | 499 | /* |
| 500 | 500 | * This file has been automatically generated by TDBM. |
@@ -511,12 +511,12 @@ discard block |
||
| 511 | 511 | { |
| 512 | 512 | "; |
| 513 | 513 | |
| 514 | - foreach ($tableList as $table) { |
|
| 515 | - $tableName = $table->getName(); |
|
| 516 | - $daoClassName = $this->getDaoNameFromTableName($tableName); |
|
| 517 | - $daoInstanceName = self::toVariableName($daoClassName); |
|
| 514 | + foreach ($tableList as $table) { |
|
| 515 | + $tableName = $table->getName(); |
|
| 516 | + $daoClassName = $this->getDaoNameFromTableName($tableName); |
|
| 517 | + $daoInstanceName = self::toVariableName($daoClassName); |
|
| 518 | 518 | |
| 519 | - $str .= ' /** |
|
| 519 | + $str .= ' /** |
|
| 520 | 520 | * @var '.$daoClassName.' |
| 521 | 521 | */ |
| 522 | 522 | private $'.$daoInstanceName.'; |
@@ -541,155 +541,155 @@ discard block |
||
| 541 | 541 | } |
| 542 | 542 | |
| 543 | 543 | '; |
| 544 | - } |
|
| 544 | + } |
|
| 545 | 545 | |
| 546 | - $str .= ' |
|
| 546 | + $str .= ' |
|
| 547 | 547 | } |
| 548 | 548 | '; |
| 549 | 549 | |
| 550 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($daoNamespace.'\\'.$daoFactoryClassName); |
|
| 551 | - if (empty($possibleFileNames)) { |
|
| 552 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daoNamespace.'\\'.$daoFactoryClassName.'" is not autoloadable.'); |
|
| 553 | - } |
|
| 554 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 555 | - |
|
| 556 | - $this->ensureDirectoryExist($possibleFileName); |
|
| 557 | - file_put_contents($possibleFileName, $str); |
|
| 558 | - @chmod($possibleFileName, 0664); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
| 563 | - * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
| 564 | - * |
|
| 565 | - * @param $str string |
|
| 566 | - * |
|
| 567 | - * @return string |
|
| 568 | - */ |
|
| 569 | - public static function toCamelCase($str) |
|
| 570 | - { |
|
| 571 | - $str = strtoupper(substr($str, 0, 1)).substr($str, 1); |
|
| 572 | - while (true) { |
|
| 573 | - if (strpos($str, '_') === false && strpos($str, ' ') === false) { |
|
| 574 | - break; |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - $pos = strpos($str, '_'); |
|
| 578 | - if ($pos === false) { |
|
| 579 | - $pos = strpos($str, ' '); |
|
| 580 | - } |
|
| 581 | - $before = substr($str, 0, $pos); |
|
| 582 | - $after = substr($str, $pos + 1); |
|
| 583 | - $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - return $str; |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - /** |
|
| 590 | - * Tries to put string to the singular form (if it is plural). |
|
| 591 | - * We assume the table names are in english. |
|
| 592 | - * |
|
| 593 | - * @param $str string |
|
| 594 | - * |
|
| 595 | - * @return string |
|
| 596 | - */ |
|
| 597 | - public static function toSingular($str) |
|
| 598 | - { |
|
| 599 | - return Inflector::singularize($str); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * Put the first letter of the string in lower case. |
|
| 604 | - * Very useful to transform a class name into a variable name. |
|
| 605 | - * |
|
| 606 | - * @param $str string |
|
| 607 | - * |
|
| 608 | - * @return string |
|
| 609 | - */ |
|
| 610 | - public static function toVariableName($str) |
|
| 611 | - { |
|
| 612 | - return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * Ensures the file passed in parameter can be written in its directory. |
|
| 617 | - * |
|
| 618 | - * @param string $fileName |
|
| 619 | - * |
|
| 620 | - * @throws TDBMException |
|
| 621 | - */ |
|
| 622 | - private function ensureDirectoryExist($fileName) |
|
| 623 | - { |
|
| 624 | - $dirName = dirname($fileName); |
|
| 625 | - if (!file_exists($dirName)) { |
|
| 626 | - $old = umask(0); |
|
| 627 | - $result = mkdir($dirName, 0775, true); |
|
| 628 | - umask($old); |
|
| 629 | - if ($result === false) { |
|
| 630 | - throw new TDBMException("Unable to create directory: '".$dirName."'."); |
|
| 631 | - } |
|
| 632 | - } |
|
| 633 | - } |
|
| 634 | - |
|
| 635 | - /** |
|
| 636 | - * @param string $rootPath |
|
| 637 | - */ |
|
| 638 | - public function setRootPath($rootPath) |
|
| 639 | - { |
|
| 640 | - $this->rootPath = $rootPath; |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - /** |
|
| 644 | - * Transforms a DBAL type into a PHP type (for PHPDoc purpose). |
|
| 645 | - * |
|
| 646 | - * @param Type $type The DBAL type |
|
| 647 | - * |
|
| 648 | - * @return string The PHP type |
|
| 649 | - */ |
|
| 650 | - public static function dbalTypeToPhpType(Type $type) |
|
| 651 | - { |
|
| 652 | - $map = [ |
|
| 653 | - Type::TARRAY => 'array', |
|
| 654 | - Type::SIMPLE_ARRAY => 'array', |
|
| 655 | - Type::JSON_ARRAY => 'array', |
|
| 656 | - Type::BIGINT => 'string', |
|
| 657 | - Type::BOOLEAN => 'bool', |
|
| 658 | - Type::DATETIME => '\DateTimeInterface', |
|
| 659 | - Type::DATETIMETZ => '\DateTimeInterface', |
|
| 660 | - Type::DATE => '\DateTimeInterface', |
|
| 661 | - Type::TIME => '\DateTimeInterface', |
|
| 662 | - Type::DECIMAL => 'float', |
|
| 663 | - Type::INTEGER => 'int', |
|
| 664 | - Type::OBJECT => 'string', |
|
| 665 | - Type::SMALLINT => 'int', |
|
| 666 | - Type::STRING => 'string', |
|
| 667 | - Type::TEXT => 'string', |
|
| 668 | - Type::BINARY => 'string', |
|
| 669 | - Type::BLOB => 'string', |
|
| 670 | - Type::FLOAT => 'float', |
|
| 671 | - Type::GUID => 'string', |
|
| 672 | - ]; |
|
| 673 | - |
|
| 674 | - return isset($map[$type->getName()]) ? $map[$type->getName()] : $type->getName(); |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - /** |
|
| 678 | - * @param string $beanNamespace |
|
| 679 | - * |
|
| 680 | - * @return \string[] Returns a map mapping table name to beans name |
|
| 681 | - */ |
|
| 682 | - public function buildTableToBeanMap($beanNamespace) |
|
| 683 | - { |
|
| 684 | - $tableToBeanMap = []; |
|
| 685 | - |
|
| 686 | - $tables = $this->schema->getTables(); |
|
| 687 | - |
|
| 688 | - foreach ($tables as $table) { |
|
| 689 | - $tableName = $table->getName(); |
|
| 690 | - $tableToBeanMap[$tableName] = $beanNamespace.'\\'.self::getBeanNameFromTableName($tableName); |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - return $tableToBeanMap; |
|
| 694 | - } |
|
| 550 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($daoNamespace.'\\'.$daoFactoryClassName); |
|
| 551 | + if (empty($possibleFileNames)) { |
|
| 552 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daoNamespace.'\\'.$daoFactoryClassName.'" is not autoloadable.'); |
|
| 553 | + } |
|
| 554 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 555 | + |
|
| 556 | + $this->ensureDirectoryExist($possibleFileName); |
|
| 557 | + file_put_contents($possibleFileName, $str); |
|
| 558 | + @chmod($possibleFileName, 0664); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
| 563 | + * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
| 564 | + * |
|
| 565 | + * @param $str string |
|
| 566 | + * |
|
| 567 | + * @return string |
|
| 568 | + */ |
|
| 569 | + public static function toCamelCase($str) |
|
| 570 | + { |
|
| 571 | + $str = strtoupper(substr($str, 0, 1)).substr($str, 1); |
|
| 572 | + while (true) { |
|
| 573 | + if (strpos($str, '_') === false && strpos($str, ' ') === false) { |
|
| 574 | + break; |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + $pos = strpos($str, '_'); |
|
| 578 | + if ($pos === false) { |
|
| 579 | + $pos = strpos($str, ' '); |
|
| 580 | + } |
|
| 581 | + $before = substr($str, 0, $pos); |
|
| 582 | + $after = substr($str, $pos + 1); |
|
| 583 | + $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + return $str; |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + /** |
|
| 590 | + * Tries to put string to the singular form (if it is plural). |
|
| 591 | + * We assume the table names are in english. |
|
| 592 | + * |
|
| 593 | + * @param $str string |
|
| 594 | + * |
|
| 595 | + * @return string |
|
| 596 | + */ |
|
| 597 | + public static function toSingular($str) |
|
| 598 | + { |
|
| 599 | + return Inflector::singularize($str); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * Put the first letter of the string in lower case. |
|
| 604 | + * Very useful to transform a class name into a variable name. |
|
| 605 | + * |
|
| 606 | + * @param $str string |
|
| 607 | + * |
|
| 608 | + * @return string |
|
| 609 | + */ |
|
| 610 | + public static function toVariableName($str) |
|
| 611 | + { |
|
| 612 | + return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * Ensures the file passed in parameter can be written in its directory. |
|
| 617 | + * |
|
| 618 | + * @param string $fileName |
|
| 619 | + * |
|
| 620 | + * @throws TDBMException |
|
| 621 | + */ |
|
| 622 | + private function ensureDirectoryExist($fileName) |
|
| 623 | + { |
|
| 624 | + $dirName = dirname($fileName); |
|
| 625 | + if (!file_exists($dirName)) { |
|
| 626 | + $old = umask(0); |
|
| 627 | + $result = mkdir($dirName, 0775, true); |
|
| 628 | + umask($old); |
|
| 629 | + if ($result === false) { |
|
| 630 | + throw new TDBMException("Unable to create directory: '".$dirName."'."); |
|
| 631 | + } |
|
| 632 | + } |
|
| 633 | + } |
|
| 634 | + |
|
| 635 | + /** |
|
| 636 | + * @param string $rootPath |
|
| 637 | + */ |
|
| 638 | + public function setRootPath($rootPath) |
|
| 639 | + { |
|
| 640 | + $this->rootPath = $rootPath; |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + /** |
|
| 644 | + * Transforms a DBAL type into a PHP type (for PHPDoc purpose). |
|
| 645 | + * |
|
| 646 | + * @param Type $type The DBAL type |
|
| 647 | + * |
|
| 648 | + * @return string The PHP type |
|
| 649 | + */ |
|
| 650 | + public static function dbalTypeToPhpType(Type $type) |
|
| 651 | + { |
|
| 652 | + $map = [ |
|
| 653 | + Type::TARRAY => 'array', |
|
| 654 | + Type::SIMPLE_ARRAY => 'array', |
|
| 655 | + Type::JSON_ARRAY => 'array', |
|
| 656 | + Type::BIGINT => 'string', |
|
| 657 | + Type::BOOLEAN => 'bool', |
|
| 658 | + Type::DATETIME => '\DateTimeInterface', |
|
| 659 | + Type::DATETIMETZ => '\DateTimeInterface', |
|
| 660 | + Type::DATE => '\DateTimeInterface', |
|
| 661 | + Type::TIME => '\DateTimeInterface', |
|
| 662 | + Type::DECIMAL => 'float', |
|
| 663 | + Type::INTEGER => 'int', |
|
| 664 | + Type::OBJECT => 'string', |
|
| 665 | + Type::SMALLINT => 'int', |
|
| 666 | + Type::STRING => 'string', |
|
| 667 | + Type::TEXT => 'string', |
|
| 668 | + Type::BINARY => 'string', |
|
| 669 | + Type::BLOB => 'string', |
|
| 670 | + Type::FLOAT => 'float', |
|
| 671 | + Type::GUID => 'string', |
|
| 672 | + ]; |
|
| 673 | + |
|
| 674 | + return isset($map[$type->getName()]) ? $map[$type->getName()] : $type->getName(); |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + /** |
|
| 678 | + * @param string $beanNamespace |
|
| 679 | + * |
|
| 680 | + * @return \string[] Returns a map mapping table name to beans name |
|
| 681 | + */ |
|
| 682 | + public function buildTableToBeanMap($beanNamespace) |
|
| 683 | + { |
|
| 684 | + $tableToBeanMap = []; |
|
| 685 | + |
|
| 686 | + $tables = $this->schema->getTables(); |
|
| 687 | + |
|
| 688 | + foreach ($tables as $table) { |
|
| 689 | + $tableName = $table->getName(); |
|
| 690 | + $tableToBeanMap[$tableName] = $beanNamespace.'\\'.self::getBeanNameFromTableName($tableName); |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + return $tableToBeanMap; |
|
| 694 | + } |
|
| 695 | 695 | } |