@@ -46,7 +46,6 @@ discard block |
||
| 46 | 46 | /** |
| 47 | 47 | * Constructor. |
| 48 | 48 | * |
| 49 | - * @param Connection $dbConnection The connection to the database. |
|
| 50 | 49 | */ |
| 51 | 50 | public function __construct(SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) { |
| 52 | 51 | $this->schemaAnalyzer = $schemaAnalyzer; |
@@ -99,6 +98,9 @@ discard block |
||
| 99 | 98 | * Generates in one method call the daos and the beans for one table. |
| 100 | 99 | * |
| 101 | 100 | * @param $tableName |
| 101 | + * @param string $daonamespace |
|
| 102 | + * @param string $beannamespace |
|
| 103 | + * @param boolean $storeInUtc |
|
| 102 | 104 | */ |
| 103 | 105 | public function generateDaoAndBean(Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc) { |
| 104 | 106 | $tableName = $table->getName(); |
@@ -134,7 +136,7 @@ discard block |
||
| 134 | 136 | /** |
| 135 | 137 | * Returns the name of the base bean class from the table name. |
| 136 | 138 | * |
| 137 | - * @param $tableName |
|
| 139 | + * @param string $tableName |
|
| 138 | 140 | * @return string |
| 139 | 141 | */ |
| 140 | 142 | public static function getBaseBeanNameFromTableName($tableName) { |
@@ -144,7 +146,7 @@ discard block |
||
| 144 | 146 | /** |
| 145 | 147 | * Returns the name of the base DAO class from the table name. |
| 146 | 148 | * |
| 147 | - * @param $tableName |
|
| 149 | + * @param string $tableName |
|
| 148 | 150 | * @return string |
| 149 | 151 | */ |
| 150 | 152 | public static function getBaseDaoNameFromTableName($tableName) { |
@@ -156,7 +158,7 @@ discard block |
||
| 156 | 158 | * |
| 157 | 159 | * @param string $className The name of the class |
| 158 | 160 | * @param string $baseClassName The name of the base class which will be extended (name only, no directory) |
| 159 | - * @param string $tableName The name of the table |
|
| 161 | + * @param string $table The name of the table |
|
| 160 | 162 | * @param string $beannamespace The namespace of the bean |
| 161 | 163 | * @param ClassNameMapper $classNameMapper |
| 162 | 164 | * @throws TDBMException |
@@ -214,9 +216,10 @@ discard block |
||
| 214 | 216 | /** |
| 215 | 217 | * Writes the PHP bean DAO with simple functions to create/get/save objects. |
| 216 | 218 | * |
| 217 | - * @param string $fileName The file that will be written (without the directory) |
|
| 218 | 219 | * @param string $className The name of the class |
| 219 | - * @param string $tableName The name of the table |
|
| 220 | + * @param string $table The name of the table |
|
| 221 | + * @param string $baseClassName |
|
| 222 | + * @param string $beanClassName |
|
| 220 | 223 | */ |
| 221 | 224 | public function generateDao($className, $baseClassName, $beanClassName, Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper) { |
| 222 | 225 | $tableName = $table->getName(); |
@@ -458,6 +461,8 @@ discard block |
||
| 458 | 461 | * Generates the factory bean. |
| 459 | 462 | * |
| 460 | 463 | * @param Table[] $tableList |
| 464 | + * @param string $daoFactoryClassName |
|
| 465 | + * @param string $daoNamespace |
|
| 461 | 466 | */ |
| 462 | 467 | private function generateFactory(array $tableList, $daoFactoryClassName, $daoNamespace, ClassNameMapper $classNameMapper) { |
| 463 | 468 | // For each table, let's write a property. |
@@ -2,8 +2,6 @@ |
||
| 2 | 2 | namespace Mouf\Database\TDBM\Utils; |
| 3 | 3 | |
| 4 | 4 | use Doctrine\DBAL\Driver\Connection; |
| 5 | -use Doctrine\DBAL\Schema\Column; |
|
| 6 | -use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
|
| 7 | 5 | use Doctrine\DBAL\Schema\Schema; |
| 8 | 6 | use Doctrine\DBAL\Schema\Table; |
| 9 | 7 | use Doctrine\DBAL\Types\Type; |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | |
| 207 | 207 | }"; |
| 208 | 208 | $this->ensureDirectoryExist($possibleFileName); |
| 209 | - file_put_contents($possibleFileName ,$str); |
|
| 209 | + file_put_contents($possibleFileName, $str); |
|
| 210 | 210 | @chmod($possibleFileName, 0664); |
| 211 | 211 | } |
| 212 | 212 | } |
@@ -226,11 +226,11 @@ discard block |
||
| 226 | 226 | foreach ($table->getColumns() as $column) { |
| 227 | 227 | $comments = $column->getComment(); |
| 228 | 228 | $matches = array(); |
| 229 | - if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0){ |
|
| 229 | + if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) { |
|
| 230 | 230 | $defaultSort = $data['column_name']; |
| 231 | - if (count($matches == 3)){ |
|
| 231 | + if (count($matches == 3)) { |
|
| 232 | 232 | $defaultSortDirection = $matches[2]; |
| 233 | - }else{ |
|
| 233 | + } else { |
|
| 234 | 234 | $defaultSortDirection = 'ASC'; |
| 235 | 235 | } |
| 236 | 236 | } |
@@ -416,7 +416,7 @@ discard block |
||
| 416 | 416 | $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
| 417 | 417 | |
| 418 | 418 | $this->ensureDirectoryExist($possibleBaseFileName); |
| 419 | - file_put_contents($possibleBaseFileName ,$str); |
|
| 419 | + file_put_contents($possibleBaseFileName, $str); |
|
| 420 | 420 | @chmod($possibleBaseFileName, 0664); |
| 421 | 421 | |
| 422 | 422 | $possibleFileNames = $classNameMapper->getPossibleFileNames($daonamespace."\\".$className); |
@@ -447,7 +447,7 @@ discard block |
||
| 447 | 447 | } |
| 448 | 448 | "; |
| 449 | 449 | $this->ensureDirectoryExist($possibleFileName); |
| 450 | - file_put_contents($possibleFileName ,$str); |
|
| 450 | + file_put_contents($possibleFileName, $str); |
|
| 451 | 451 | @chmod($possibleFileName, 0664); |
| 452 | 452 | } |
| 453 | 453 | } |
@@ -523,7 +523,7 @@ discard block |
||
| 523 | 523 | $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
| 524 | 524 | |
| 525 | 525 | $this->ensureDirectoryExist($possibleFileName); |
| 526 | - file_put_contents($possibleFileName ,$str); |
|
| 526 | + file_put_contents($possibleFileName, $str); |
|
| 527 | 527 | } |
| 528 | 528 | |
| 529 | 529 | /** |
@@ -534,7 +534,7 @@ discard block |
||
| 534 | 534 | * @return string |
| 535 | 535 | */ |
| 536 | 536 | public static function toCamelCase($str) { |
| 537 | - $str = strtoupper(substr($str,0,1)).substr($str,1); |
|
| 537 | + $str = strtoupper(substr($str, 0, 1)).substr($str, 1); |
|
| 538 | 538 | while (true) { |
| 539 | 539 | if (strpos($str, "_") === false && strpos($str, " ") === false) { |
| 540 | 540 | break; |
@@ -544,9 +544,9 @@ discard block |
||
| 544 | 544 | if ($pos === false) { |
| 545 | 545 | $pos = strpos($str, " "); |
| 546 | 546 | } |
| 547 | - $before = substr($str,0,$pos); |
|
| 548 | - $after = substr($str,$pos+1); |
|
| 549 | - $str = $before.strtoupper(substr($after,0,1)).substr($after,1); |
|
| 547 | + $before = substr($str, 0, $pos); |
|
| 548 | + $after = substr($str, $pos+1); |
|
| 549 | + $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
|
| 550 | 550 | } |
| 551 | 551 | return $str; |
| 552 | 552 | } |
@@ -627,7 +627,7 @@ discard block |
||
| 627 | 627 | Type::GUID => 'string' |
| 628 | 628 | ]; |
| 629 | 629 | |
| 630 | - return isset($map[$type->getName()])?$map[$type->getName()]:$type->getName(); |
|
| 630 | + return isset($map[$type->getName()]) ? $map[$type->getName()] : $type->getName(); |
|
| 631 | 631 | } |
| 632 | 632 | |
| 633 | 633 | /** |
@@ -642,7 +642,7 @@ discard block |
||
| 642 | 642 | |
| 643 | 643 | foreach ($tables as $table) { |
| 644 | 644 | $tableName = $table->getName(); |
| 645 | - $tableToBeanMap[$tableName] = $beanNamespace . "\\" . self::getBeanNameFromTableName($tableName); |
|
| 645 | + $tableToBeanMap[$tableName] = $beanNamespace."\\".self::getBeanNameFromTableName($tableName); |
|
| 646 | 646 | } |
| 647 | 647 | return $tableToBeanMap; |
| 648 | 648 | } |
@@ -230,7 +230,7 @@ |
||
| 230 | 230 | $defaultSort = $data['column_name']; |
| 231 | 231 | if (count($matches == 3)){ |
| 232 | 232 | $defaultSortDirection = $matches[2]; |
| 233 | - }else{ |
|
| 233 | + } else{ |
|
| 234 | 234 | $defaultSortDirection = 'ASC'; |
| 235 | 235 | } |
| 236 | 236 | } |
@@ -19,178 +19,178 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | class TDBMDaoGenerator { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * |
|
| 24 | - * @var SchemaAnalyzer |
|
| 25 | - */ |
|
| 26 | - private $schemaAnalyzer; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - * @var Schema |
|
| 31 | - */ |
|
| 32 | - private $schema; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The root directory of the project. |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - private $rootPath; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var TDBMSchemaAnalyzer |
|
| 43 | - */ |
|
| 44 | - private $tdbmSchemaAnalyzer; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Constructor. |
|
| 48 | - * |
|
| 49 | - * @param Connection $dbConnection The connection to the database. |
|
| 50 | - */ |
|
| 51 | - public function __construct(SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) { |
|
| 52 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 53 | - $this->schema = $schema; |
|
| 54 | - $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
| 55 | - $this->rootPath = __DIR__."/../../../../../../../../"; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Generates all the daos and beans. |
|
| 60 | - * |
|
| 61 | - * @param string $daoFactoryClassName The classe name of the DAO factory |
|
| 62 | - * @param string $daonamespace The namespace for the DAOs, without trailing \ |
|
| 63 | - * @param string $beannamespace The Namespace for the beans, without trailing \ |
|
| 64 | - * @param bool $storeInUtc If the generated daos should store the date in UTC timezone instead of user's timezone. |
|
| 65 | - * @return \string[] the list of tables |
|
| 66 | - * @throws TDBMException |
|
| 67 | - */ |
|
| 68 | - public function generateAllDaosAndBeans($daoFactoryClassName, $daonamespace, $beannamespace, $storeInUtc) { |
|
| 69 | - // TODO: extract ClassNameMapper in its own package! |
|
| 70 | - $classNameMapper = ClassNameMapper::createFromComposerFile($this->rootPath.'composer.json'); |
|
| 71 | - |
|
| 72 | - // TODO: check that no class name ends with "Base". Otherwise, there will be name clash. |
|
| 73 | - |
|
| 74 | - $tableList = $this->schema->getTables(); |
|
| 75 | - |
|
| 76 | - // Remove all beans and daos from junction tables |
|
| 77 | - $junctionTables = $this->schemaAnalyzer->detectJunctionTables(); |
|
| 78 | - $junctionTableNames = array_map(function(Table $table) { |
|
| 79 | - return $table->getName(); |
|
| 80 | - }, $junctionTables); |
|
| 81 | - |
|
| 82 | - $tableList = array_filter($tableList, function(Table $table) use ($junctionTableNames) { |
|
| 83 | - return !in_array($table->getName(), $junctionTableNames); |
|
| 84 | - }); |
|
| 85 | - |
|
| 86 | - foreach ($tableList as $table) { |
|
| 87 | - $this->generateDaoAndBean($table, $daonamespace, $beannamespace, $classNameMapper, $storeInUtc); |
|
| 88 | - } |
|
| 22 | + /** |
|
| 23 | + * |
|
| 24 | + * @var SchemaAnalyzer |
|
| 25 | + */ |
|
| 26 | + private $schemaAnalyzer; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + * @var Schema |
|
| 31 | + */ |
|
| 32 | + private $schema; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The root directory of the project. |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + private $rootPath; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var TDBMSchemaAnalyzer |
|
| 43 | + */ |
|
| 44 | + private $tdbmSchemaAnalyzer; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Constructor. |
|
| 48 | + * |
|
| 49 | + * @param Connection $dbConnection The connection to the database. |
|
| 50 | + */ |
|
| 51 | + public function __construct(SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) { |
|
| 52 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 53 | + $this->schema = $schema; |
|
| 54 | + $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
| 55 | + $this->rootPath = __DIR__."/../../../../../../../../"; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Generates all the daos and beans. |
|
| 60 | + * |
|
| 61 | + * @param string $daoFactoryClassName The classe name of the DAO factory |
|
| 62 | + * @param string $daonamespace The namespace for the DAOs, without trailing \ |
|
| 63 | + * @param string $beannamespace The Namespace for the beans, without trailing \ |
|
| 64 | + * @param bool $storeInUtc If the generated daos should store the date in UTC timezone instead of user's timezone. |
|
| 65 | + * @return \string[] the list of tables |
|
| 66 | + * @throws TDBMException |
|
| 67 | + */ |
|
| 68 | + public function generateAllDaosAndBeans($daoFactoryClassName, $daonamespace, $beannamespace, $storeInUtc) { |
|
| 69 | + // TODO: extract ClassNameMapper in its own package! |
|
| 70 | + $classNameMapper = ClassNameMapper::createFromComposerFile($this->rootPath.'composer.json'); |
|
| 71 | + |
|
| 72 | + // TODO: check that no class name ends with "Base". Otherwise, there will be name clash. |
|
| 73 | + |
|
| 74 | + $tableList = $this->schema->getTables(); |
|
| 75 | + |
|
| 76 | + // Remove all beans and daos from junction tables |
|
| 77 | + $junctionTables = $this->schemaAnalyzer->detectJunctionTables(); |
|
| 78 | + $junctionTableNames = array_map(function(Table $table) { |
|
| 79 | + return $table->getName(); |
|
| 80 | + }, $junctionTables); |
|
| 81 | + |
|
| 82 | + $tableList = array_filter($tableList, function(Table $table) use ($junctionTableNames) { |
|
| 83 | + return !in_array($table->getName(), $junctionTableNames); |
|
| 84 | + }); |
|
| 85 | + |
|
| 86 | + foreach ($tableList as $table) { |
|
| 87 | + $this->generateDaoAndBean($table, $daonamespace, $beannamespace, $classNameMapper, $storeInUtc); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - $this->generateFactory($tableList, $daoFactoryClassName, $daonamespace, $classNameMapper); |
|
| 90 | + $this->generateFactory($tableList, $daoFactoryClassName, $daonamespace, $classNameMapper); |
|
| 91 | 91 | |
| 92 | - // Ok, let's return the list of all tables. |
|
| 93 | - // These will be used by the calling script to create Mouf instances. |
|
| 92 | + // Ok, let's return the list of all tables. |
|
| 93 | + // These will be used by the calling script to create Mouf instances. |
|
| 94 | 94 | |
| 95 | - return array_map(function(Table $table) { return $table->getName(); },$tableList); |
|
| 96 | - } |
|
| 95 | + return array_map(function(Table $table) { return $table->getName(); },$tableList); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Generates in one method call the daos and the beans for one table. |
|
| 100 | - * |
|
| 101 | - * @param $tableName |
|
| 102 | - */ |
|
| 103 | - public function generateDaoAndBean(Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc) { |
|
| 98 | + /** |
|
| 99 | + * Generates in one method call the daos and the beans for one table. |
|
| 100 | + * |
|
| 101 | + * @param $tableName |
|
| 102 | + */ |
|
| 103 | + public function generateDaoAndBean(Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc) { |
|
| 104 | 104 | $tableName = $table->getName(); |
| 105 | - $daoName = $this->getDaoNameFromTableName($tableName); |
|
| 106 | - $beanName = $this->getBeanNameFromTableName($tableName); |
|
| 107 | - $baseBeanName = $this->getBaseBeanNameFromTableName($tableName); |
|
| 108 | - $baseDaoName = $this->getBaseDaoNameFromTableName($tableName); |
|
| 109 | - |
|
| 110 | - $this->generateBean($beanName, $baseBeanName, $table, $beannamespace, $classNameMapper, $storeInUtc); |
|
| 111 | - $this->generateDao($daoName, $baseDaoName, $beanName, $table, $daonamespace, $beannamespace, $classNameMapper); |
|
| 112 | - } |
|
| 105 | + $daoName = $this->getDaoNameFromTableName($tableName); |
|
| 106 | + $beanName = $this->getBeanNameFromTableName($tableName); |
|
| 107 | + $baseBeanName = $this->getBaseBeanNameFromTableName($tableName); |
|
| 108 | + $baseDaoName = $this->getBaseDaoNameFromTableName($tableName); |
|
| 109 | + |
|
| 110 | + $this->generateBean($beanName, $baseBeanName, $table, $beannamespace, $classNameMapper, $storeInUtc); |
|
| 111 | + $this->generateDao($daoName, $baseDaoName, $beanName, $table, $daonamespace, $beannamespace, $classNameMapper); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Returns the name of the bean class from the table name. |
|
| 116 | - * |
|
| 117 | - * @param $tableName |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - public static function getBeanNameFromTableName($tableName) { |
|
| 121 | - return TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($tableName))."Bean"; |
|
| 122 | - } |
|
| 114 | + /** |
|
| 115 | + * Returns the name of the bean class from the table name. |
|
| 116 | + * |
|
| 117 | + * @param $tableName |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + public static function getBeanNameFromTableName($tableName) { |
|
| 121 | + return TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($tableName))."Bean"; |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * Returns the name of the DAO class from the table name. |
|
| 126 | - * |
|
| 127 | - * @param $tableName |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 130 | - public static function getDaoNameFromTableName($tableName) { |
|
| 131 | - return TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($tableName))."Dao"; |
|
| 132 | - } |
|
| 124 | + /** |
|
| 125 | + * Returns the name of the DAO class from the table name. |
|
| 126 | + * |
|
| 127 | + * @param $tableName |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | + public static function getDaoNameFromTableName($tableName) { |
|
| 131 | + return TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($tableName))."Dao"; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Returns the name of the base bean class from the table name. |
|
| 136 | - * |
|
| 137 | - * @param $tableName |
|
| 138 | - * @return string |
|
| 139 | - */ |
|
| 140 | - public static function getBaseBeanNameFromTableName($tableName) { |
|
| 141 | - return TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($tableName))."BaseBean"; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Returns the name of the base DAO class from the table name. |
|
| 146 | - * |
|
| 147 | - * @param $tableName |
|
| 148 | - * @return string |
|
| 149 | - */ |
|
| 150 | - public static function getBaseDaoNameFromTableName($tableName) { |
|
| 151 | - return TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($tableName))."BaseDao"; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 156 | - * |
|
| 157 | - * @param string $className The name of the class |
|
| 158 | - * @param string $baseClassName The name of the base class which will be extended (name only, no directory) |
|
| 159 | - * @param string $tableName The name of the table |
|
| 160 | - * @param string $beannamespace The namespace of the bean |
|
| 161 | - * @param ClassNameMapper $classNameMapper |
|
| 162 | - * @throws TDBMException |
|
| 163 | - */ |
|
| 164 | - public function generateBean($className, $baseClassName, Table $table, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc) { |
|
| 165 | - |
|
| 166 | - $beanDescriptor = new BeanDescriptor($table, $this->schemaAnalyzer, $this->schema); |
|
| 167 | - |
|
| 168 | - $str = $beanDescriptor->generatePhpCode($beannamespace); |
|
| 169 | - |
|
| 170 | - $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($beannamespace."\\".$baseClassName); |
|
| 171 | - if (!$possibleBaseFileNames) { |
|
| 172 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace."\\".$baseClassName.'" is not autoloadable.'); |
|
| 173 | - } |
|
| 174 | - $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 175 | - |
|
| 176 | - $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 177 | - file_put_contents($possibleBaseFileName, $str); |
|
| 178 | - @chmod($possibleBaseFileName, 0664); |
|
| 179 | - |
|
| 180 | - |
|
| 181 | - |
|
| 182 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($beannamespace."\\".$className); |
|
| 183 | - if (!$possibleFileNames) { |
|
| 184 | - // @codeCoverageIgnoreStart |
|
| 185 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace."\\".$className.'" is not autoloadable.'); |
|
| 186 | - // @codeCoverageIgnoreEnd |
|
| 187 | - } |
|
| 188 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 189 | - |
|
| 190 | - if (!file_exists($possibleFileName)) { |
|
| 191 | - $tableName = $table->getName(); |
|
| 192 | - |
|
| 193 | - $str = "<?php |
|
| 134 | + /** |
|
| 135 | + * Returns the name of the base bean class from the table name. |
|
| 136 | + * |
|
| 137 | + * @param $tableName |
|
| 138 | + * @return string |
|
| 139 | + */ |
|
| 140 | + public static function getBaseBeanNameFromTableName($tableName) { |
|
| 141 | + return TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($tableName))."BaseBean"; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Returns the name of the base DAO class from the table name. |
|
| 146 | + * |
|
| 147 | + * @param $tableName |
|
| 148 | + * @return string |
|
| 149 | + */ |
|
| 150 | + public static function getBaseDaoNameFromTableName($tableName) { |
|
| 151 | + return TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($tableName))."BaseDao"; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 156 | + * |
|
| 157 | + * @param string $className The name of the class |
|
| 158 | + * @param string $baseClassName The name of the base class which will be extended (name only, no directory) |
|
| 159 | + * @param string $tableName The name of the table |
|
| 160 | + * @param string $beannamespace The namespace of the bean |
|
| 161 | + * @param ClassNameMapper $classNameMapper |
|
| 162 | + * @throws TDBMException |
|
| 163 | + */ |
|
| 164 | + public function generateBean($className, $baseClassName, Table $table, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc) { |
|
| 165 | + |
|
| 166 | + $beanDescriptor = new BeanDescriptor($table, $this->schemaAnalyzer, $this->schema); |
|
| 167 | + |
|
| 168 | + $str = $beanDescriptor->generatePhpCode($beannamespace); |
|
| 169 | + |
|
| 170 | + $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($beannamespace."\\".$baseClassName); |
|
| 171 | + if (!$possibleBaseFileNames) { |
|
| 172 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace."\\".$baseClassName.'" is not autoloadable.'); |
|
| 173 | + } |
|
| 174 | + $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 175 | + |
|
| 176 | + $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 177 | + file_put_contents($possibleBaseFileName, $str); |
|
| 178 | + @chmod($possibleBaseFileName, 0664); |
|
| 179 | + |
|
| 180 | + |
|
| 181 | + |
|
| 182 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($beannamespace."\\".$className); |
|
| 183 | + if (!$possibleFileNames) { |
|
| 184 | + // @codeCoverageIgnoreStart |
|
| 185 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace."\\".$className.'" is not autoloadable.'); |
|
| 186 | + // @codeCoverageIgnoreEnd |
|
| 187 | + } |
|
| 188 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 189 | + |
|
| 190 | + if (!file_exists($possibleFileName)) { |
|
| 191 | + $tableName = $table->getName(); |
|
| 192 | + |
|
| 193 | + $str = "<?php |
|
| 194 | 194 | /* |
| 195 | 195 | * This file has been automatically generated by TDBM. |
| 196 | 196 | * You can edit this file as it will not be overwritten. |
@@ -205,44 +205,44 @@ discard block |
||
| 205 | 205 | { |
| 206 | 206 | |
| 207 | 207 | }"; |
| 208 | - $this->ensureDirectoryExist($possibleFileName); |
|
| 209 | - file_put_contents($possibleFileName ,$str); |
|
| 210 | - @chmod($possibleFileName, 0664); |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Writes the PHP bean DAO with simple functions to create/get/save objects. |
|
| 216 | - * |
|
| 217 | - * @param string $fileName The file that will be written (without the directory) |
|
| 218 | - * @param string $className The name of the class |
|
| 219 | - * @param string $tableName The name of the table |
|
| 220 | - */ |
|
| 221 | - public function generateDao($className, $baseClassName, $beanClassName, Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper) { |
|
| 222 | - $tableName = $table->getName(); |
|
| 223 | - $primaryKeyColumns = $table->getPrimaryKeyColumns(); |
|
| 224 | - |
|
| 225 | - $defaultSort = null; |
|
| 226 | - foreach ($table->getColumns() as $column) { |
|
| 227 | - $comments = $column->getComment(); |
|
| 228 | - $matches = array(); |
|
| 229 | - if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0){ |
|
| 230 | - $defaultSort = $data['column_name']; |
|
| 231 | - if (count($matches == 3)){ |
|
| 232 | - $defaultSortDirection = $matches[2]; |
|
| 233 | - }else{ |
|
| 234 | - $defaultSortDirection = 'ASC'; |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - } |
|
| 208 | + $this->ensureDirectoryExist($possibleFileName); |
|
| 209 | + file_put_contents($possibleFileName ,$str); |
|
| 210 | + @chmod($possibleFileName, 0664); |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Writes the PHP bean DAO with simple functions to create/get/save objects. |
|
| 216 | + * |
|
| 217 | + * @param string $fileName The file that will be written (without the directory) |
|
| 218 | + * @param string $className The name of the class |
|
| 219 | + * @param string $tableName The name of the table |
|
| 220 | + */ |
|
| 221 | + public function generateDao($className, $baseClassName, $beanClassName, Table $table, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper) { |
|
| 222 | + $tableName = $table->getName(); |
|
| 223 | + $primaryKeyColumns = $table->getPrimaryKeyColumns(); |
|
| 224 | + |
|
| 225 | + $defaultSort = null; |
|
| 226 | + foreach ($table->getColumns() as $column) { |
|
| 227 | + $comments = $column->getComment(); |
|
| 228 | + $matches = array(); |
|
| 229 | + if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0){ |
|
| 230 | + $defaultSort = $data['column_name']; |
|
| 231 | + if (count($matches == 3)){ |
|
| 232 | + $defaultSortDirection = $matches[2]; |
|
| 233 | + }else{ |
|
| 234 | + $defaultSortDirection = 'ASC'; |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - // FIXME: lowercase tables with _ in the name should work! |
|
| 240 | - $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
|
| 239 | + // FIXME: lowercase tables with _ in the name should work! |
|
| 240 | + $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
|
| 241 | 241 | |
| 242 | - $beanClassWithoutNameSpace = $beanClassName; |
|
| 243 | - $beanClassName = $beannamespace."\\".$beanClassName; |
|
| 242 | + $beanClassWithoutNameSpace = $beanClassName; |
|
| 243 | + $beanClassName = $beannamespace."\\".$beanClassName; |
|
| 244 | 244 | |
| 245 | - $str = "<?php |
|
| 245 | + $str = "<?php |
|
| 246 | 246 | |
| 247 | 247 | /* |
| 248 | 248 | * This file has been automatically generated by TDBM. |
@@ -329,9 +329,9 @@ discard block |
||
| 329 | 329 | } |
| 330 | 330 | "; |
| 331 | 331 | |
| 332 | - if (count($primaryKeyColumns) === 1) { |
|
| 333 | - $primaryKeyColumn = $primaryKeyColumns[0]; |
|
| 334 | - $str .= " |
|
| 332 | + if (count($primaryKeyColumns) === 1) { |
|
| 333 | + $primaryKeyColumn = $primaryKeyColumns[0]; |
|
| 334 | + $str .= " |
|
| 335 | 335 | /** |
| 336 | 336 | * Get $beanClassWithoutNameSpace specified by its ID (its primary key) |
| 337 | 337 | * If the primary key does not exist, an exception is thrown. |
@@ -346,8 +346,8 @@ discard block |
||
| 346 | 346 | return \$this->tdbmService->findObjectByPk('$tableName', ['$primaryKeyColumn' => \$id], [], \$lazyLoading); |
| 347 | 347 | } |
| 348 | 348 | "; |
| 349 | - } |
|
| 350 | - $str .= " |
|
| 349 | + } |
|
| 350 | + $str .= " |
|
| 351 | 351 | /** |
| 352 | 352 | * Deletes the $beanClassWithoutNameSpace passed in parameter. |
| 353 | 353 | * |
@@ -407,29 +407,29 @@ discard block |
||
| 407 | 407 | } |
| 408 | 408 | "; |
| 409 | 409 | |
| 410 | - $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($daonamespace."\\".$baseClassName); |
|
| 411 | - if (!$possibleBaseFileNames) { |
|
| 412 | - // @codeCoverageIgnoreStart |
|
| 413 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$baseClassName.'" is not autoloadable.'); |
|
| 414 | - // @codeCoverageIgnoreEnd |
|
| 415 | - } |
|
| 416 | - $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 417 | - |
|
| 418 | - $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 419 | - file_put_contents($possibleBaseFileName ,$str); |
|
| 420 | - @chmod($possibleBaseFileName, 0664); |
|
| 421 | - |
|
| 422 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($daonamespace."\\".$className); |
|
| 423 | - if (!$possibleFileNames) { |
|
| 424 | - // @codeCoverageIgnoreStart |
|
| 425 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$className.'" is not autoloadable.'); |
|
| 426 | - // @codeCoverageIgnoreEnd |
|
| 427 | - } |
|
| 428 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 410 | + $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($daonamespace."\\".$baseClassName); |
|
| 411 | + if (!$possibleBaseFileNames) { |
|
| 412 | + // @codeCoverageIgnoreStart |
|
| 413 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$baseClassName.'" is not autoloadable.'); |
|
| 414 | + // @codeCoverageIgnoreEnd |
|
| 415 | + } |
|
| 416 | + $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 417 | + |
|
| 418 | + $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 419 | + file_put_contents($possibleBaseFileName ,$str); |
|
| 420 | + @chmod($possibleBaseFileName, 0664); |
|
| 421 | + |
|
| 422 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($daonamespace."\\".$className); |
|
| 423 | + if (!$possibleFileNames) { |
|
| 424 | + // @codeCoverageIgnoreStart |
|
| 425 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$className.'" is not autoloadable.'); |
|
| 426 | + // @codeCoverageIgnoreEnd |
|
| 427 | + } |
|
| 428 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 429 | 429 | |
| 430 | - // Now, let's generate the "editable" class |
|
| 431 | - if (!file_exists($possibleFileName)) { |
|
| 432 | - $str = "<?php |
|
| 430 | + // Now, let's generate the "editable" class |
|
| 431 | + if (!file_exists($possibleFileName)) { |
|
| 432 | + $str = "<?php |
|
| 433 | 433 | |
| 434 | 434 | /* |
| 435 | 435 | * This file has been automatically generated by TDBM. |
@@ -446,23 +446,23 @@ discard block |
||
| 446 | 446 | |
| 447 | 447 | } |
| 448 | 448 | "; |
| 449 | - $this->ensureDirectoryExist($possibleFileName); |
|
| 450 | - file_put_contents($possibleFileName ,$str); |
|
| 451 | - @chmod($possibleFileName, 0664); |
|
| 452 | - } |
|
| 453 | - } |
|
| 449 | + $this->ensureDirectoryExist($possibleFileName); |
|
| 450 | + file_put_contents($possibleFileName ,$str); |
|
| 451 | + @chmod($possibleFileName, 0664); |
|
| 452 | + } |
|
| 453 | + } |
|
| 454 | 454 | |
| 455 | 455 | |
| 456 | 456 | |
| 457 | - /** |
|
| 458 | - * Generates the factory bean. |
|
| 459 | - * |
|
| 460 | - * @param Table[] $tableList |
|
| 461 | - */ |
|
| 462 | - private function generateFactory(array $tableList, $daoFactoryClassName, $daoNamespace, ClassNameMapper $classNameMapper) { |
|
| 463 | - // For each table, let's write a property. |
|
| 457 | + /** |
|
| 458 | + * Generates the factory bean. |
|
| 459 | + * |
|
| 460 | + * @param Table[] $tableList |
|
| 461 | + */ |
|
| 462 | + private function generateFactory(array $tableList, $daoFactoryClassName, $daoNamespace, ClassNameMapper $classNameMapper) { |
|
| 463 | + // For each table, let's write a property. |
|
| 464 | 464 | |
| 465 | - $str = "<?php |
|
| 465 | + $str = "<?php |
|
| 466 | 466 | |
| 467 | 467 | /* |
| 468 | 468 | * This file has been automatically generated by TDBM. |
@@ -479,12 +479,12 @@ discard block |
||
| 479 | 479 | { |
| 480 | 480 | "; |
| 481 | 481 | |
| 482 | - foreach ($tableList as $table) { |
|
| 483 | - $tableName = $table->getName(); |
|
| 484 | - $daoClassName = $this->getDaoNameFromTableName($tableName); |
|
| 485 | - $daoInstanceName = self::toVariableName($daoClassName); |
|
| 482 | + foreach ($tableList as $table) { |
|
| 483 | + $tableName = $table->getName(); |
|
| 484 | + $daoClassName = $this->getDaoNameFromTableName($tableName); |
|
| 485 | + $daoInstanceName = self::toVariableName($daoClassName); |
|
| 486 | 486 | |
| 487 | - $str .= ' /** |
|
| 487 | + $str .= ' /** |
|
| 488 | 488 | * @var '.$daoClassName.' |
| 489 | 489 | */ |
| 490 | 490 | private $'.$daoInstanceName.'; |
@@ -509,143 +509,143 @@ discard block |
||
| 509 | 509 | } |
| 510 | 510 | |
| 511 | 511 | '; |
| 512 | - } |
|
| 512 | + } |
|
| 513 | 513 | |
| 514 | 514 | |
| 515 | - $str .= ' |
|
| 515 | + $str .= ' |
|
| 516 | 516 | } |
| 517 | 517 | '; |
| 518 | 518 | |
| 519 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($daoNamespace."\\".$daoFactoryClassName); |
|
| 520 | - if (!$possibleFileNames) { |
|
| 521 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daoNamespace."\\".$daoFactoryClassName.'" is not autoloadable.'); |
|
| 522 | - } |
|
| 523 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 524 | - |
|
| 525 | - $this->ensureDirectoryExist($possibleFileName); |
|
| 526 | - file_put_contents($possibleFileName ,$str); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
| 531 | - * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
| 532 | - * |
|
| 533 | - * @param $str string |
|
| 534 | - * @return string |
|
| 535 | - */ |
|
| 536 | - public static function toCamelCase($str) { |
|
| 537 | - $str = strtoupper(substr($str,0,1)).substr($str,1); |
|
| 538 | - while (true) { |
|
| 539 | - if (strpos($str, "_") === false && strpos($str, " ") === false) { |
|
| 540 | - break; |
|
| 519 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($daoNamespace."\\".$daoFactoryClassName); |
|
| 520 | + if (!$possibleFileNames) { |
|
| 521 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daoNamespace."\\".$daoFactoryClassName.'" is not autoloadable.'); |
|
| 522 | + } |
|
| 523 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 524 | + |
|
| 525 | + $this->ensureDirectoryExist($possibleFileName); |
|
| 526 | + file_put_contents($possibleFileName ,$str); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
| 531 | + * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
| 532 | + * |
|
| 533 | + * @param $str string |
|
| 534 | + * @return string |
|
| 535 | + */ |
|
| 536 | + public static function toCamelCase($str) { |
|
| 537 | + $str = strtoupper(substr($str,0,1)).substr($str,1); |
|
| 538 | + while (true) { |
|
| 539 | + if (strpos($str, "_") === false && strpos($str, " ") === false) { |
|
| 540 | + break; |
|
| 541 | 541 | } |
| 542 | 542 | |
| 543 | - $pos = strpos($str, "_"); |
|
| 544 | - if ($pos === false) { |
|
| 545 | - $pos = strpos($str, " "); |
|
| 546 | - } |
|
| 547 | - $before = substr($str,0,$pos); |
|
| 548 | - $after = substr($str,$pos+1); |
|
| 549 | - $str = $before.strtoupper(substr($after,0,1)).substr($after,1); |
|
| 550 | - } |
|
| 551 | - return $str; |
|
| 552 | - } |
|
| 543 | + $pos = strpos($str, "_"); |
|
| 544 | + if ($pos === false) { |
|
| 545 | + $pos = strpos($str, " "); |
|
| 546 | + } |
|
| 547 | + $before = substr($str,0,$pos); |
|
| 548 | + $after = substr($str,$pos+1); |
|
| 549 | + $str = $before.strtoupper(substr($after,0,1)).substr($after,1); |
|
| 550 | + } |
|
| 551 | + return $str; |
|
| 552 | + } |
|
| 553 | 553 | |
| 554 | - /** |
|
| 555 | - * Tries to put string to the singular form (if it is plural). |
|
| 556 | - * We assume the table names are in english. |
|
| 557 | - * |
|
| 558 | - * @param $str string |
|
| 559 | - * @return string |
|
| 560 | - */ |
|
| 561 | - public static function toSingular($str) { |
|
| 562 | - // Workaround for autoload files not loaded by Mouf |
|
| 563 | - require_once __DIR__.'/../../../../../../../icanboogie/inflector/lib/helpers.php'; |
|
| 564 | - return \ICanBoogie\singularize($str, "en"); |
|
| 565 | - } |
|
| 554 | + /** |
|
| 555 | + * Tries to put string to the singular form (if it is plural). |
|
| 556 | + * We assume the table names are in english. |
|
| 557 | + * |
|
| 558 | + * @param $str string |
|
| 559 | + * @return string |
|
| 560 | + */ |
|
| 561 | + public static function toSingular($str) { |
|
| 562 | + // Workaround for autoload files not loaded by Mouf |
|
| 563 | + require_once __DIR__.'/../../../../../../../icanboogie/inflector/lib/helpers.php'; |
|
| 564 | + return \ICanBoogie\singularize($str, "en"); |
|
| 565 | + } |
|
| 566 | 566 | |
| 567 | - /** |
|
| 568 | - * Put the first letter of the string in lower case. |
|
| 569 | - * Very useful to transform a class name into a variable name. |
|
| 570 | - * |
|
| 571 | - * @param $str string |
|
| 572 | - * @return string |
|
| 573 | - */ |
|
| 574 | - public static function toVariableName($str) { |
|
| 575 | - return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * Ensures the file passed in parameter can be written in its directory. |
|
| 580 | - * @param string $fileName |
|
| 581 | - */ |
|
| 582 | - private function ensureDirectoryExist($fileName) { |
|
| 583 | - $dirName = dirname($fileName); |
|
| 584 | - if (!file_exists($dirName)) { |
|
| 585 | - $old = umask(0); |
|
| 586 | - $result = mkdir($dirName, 0775, true); |
|
| 587 | - umask($old); |
|
| 588 | - if ($result == false) { |
|
| 589 | - echo "Unable to create directory: ".$dirName."."; |
|
| 590 | - exit; |
|
| 591 | - } |
|
| 592 | - } |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * @param string $rootPath |
|
| 597 | - */ |
|
| 598 | - public function setRootPath($rootPath) |
|
| 599 | - { |
|
| 600 | - $this->rootPath = $rootPath; |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - /** |
|
| 604 | - * Transforms a DBAL type into a PHP type (for PHPDoc purpose) |
|
| 605 | - * |
|
| 606 | - * @param Type $type The DBAL type |
|
| 607 | - * @return string The PHP type |
|
| 608 | - */ |
|
| 609 | - public static function dbalTypeToPhpType(Type $type) { |
|
| 610 | - $map = [ |
|
| 611 | - Type::TARRAY => 'array', |
|
| 612 | - Type::SIMPLE_ARRAY => 'array', |
|
| 613 | - Type::JSON_ARRAY => 'array', |
|
| 614 | - Type::BIGINT => 'string', |
|
| 615 | - Type::BOOLEAN => 'bool', |
|
| 616 | - Type::DATETIME => '\DateTimeInterface', |
|
| 617 | - Type::DATETIMETZ => '\DateTimeInterface', |
|
| 618 | - Type::DATE => '\DateTimeInterface', |
|
| 619 | - Type::TIME => '\DateTimeInterface', |
|
| 620 | - Type::DECIMAL => 'float', |
|
| 621 | - Type::INTEGER => 'int', |
|
| 622 | - Type::OBJECT => 'string', |
|
| 623 | - Type::SMALLINT => 'int', |
|
| 624 | - Type::STRING => 'string', |
|
| 625 | - Type::TEXT => 'string', |
|
| 626 | - Type::BINARY => 'string', |
|
| 627 | - Type::BLOB => 'string', |
|
| 628 | - Type::FLOAT => 'float', |
|
| 629 | - Type::GUID => 'string' |
|
| 630 | - ]; |
|
| 631 | - |
|
| 632 | - return isset($map[$type->getName()])?$map[$type->getName()]:$type->getName(); |
|
| 633 | - } |
|
| 634 | - |
|
| 635 | - /** |
|
| 636 | - * |
|
| 637 | - * @param string $beanNamespace |
|
| 638 | - * @return \string[] Returns a map mapping table name to beans name |
|
| 639 | - */ |
|
| 640 | - public function buildTableToBeanMap($beanNamespace) { |
|
| 641 | - $tableToBeanMap = []; |
|
| 642 | - |
|
| 643 | - $tables = $this->schema->getTables(); |
|
| 644 | - |
|
| 645 | - foreach ($tables as $table) { |
|
| 646 | - $tableName = $table->getName(); |
|
| 647 | - $tableToBeanMap[$tableName] = $beanNamespace . "\\" . self::getBeanNameFromTableName($tableName); |
|
| 648 | - } |
|
| 649 | - return $tableToBeanMap; |
|
| 650 | - } |
|
| 567 | + /** |
|
| 568 | + * Put the first letter of the string in lower case. |
|
| 569 | + * Very useful to transform a class name into a variable name. |
|
| 570 | + * |
|
| 571 | + * @param $str string |
|
| 572 | + * @return string |
|
| 573 | + */ |
|
| 574 | + public static function toVariableName($str) { |
|
| 575 | + return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * Ensures the file passed in parameter can be written in its directory. |
|
| 580 | + * @param string $fileName |
|
| 581 | + */ |
|
| 582 | + private function ensureDirectoryExist($fileName) { |
|
| 583 | + $dirName = dirname($fileName); |
|
| 584 | + if (!file_exists($dirName)) { |
|
| 585 | + $old = umask(0); |
|
| 586 | + $result = mkdir($dirName, 0775, true); |
|
| 587 | + umask($old); |
|
| 588 | + if ($result == false) { |
|
| 589 | + echo "Unable to create directory: ".$dirName."."; |
|
| 590 | + exit; |
|
| 591 | + } |
|
| 592 | + } |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * @param string $rootPath |
|
| 597 | + */ |
|
| 598 | + public function setRootPath($rootPath) |
|
| 599 | + { |
|
| 600 | + $this->rootPath = $rootPath; |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + /** |
|
| 604 | + * Transforms a DBAL type into a PHP type (for PHPDoc purpose) |
|
| 605 | + * |
|
| 606 | + * @param Type $type The DBAL type |
|
| 607 | + * @return string The PHP type |
|
| 608 | + */ |
|
| 609 | + public static function dbalTypeToPhpType(Type $type) { |
|
| 610 | + $map = [ |
|
| 611 | + Type::TARRAY => 'array', |
|
| 612 | + Type::SIMPLE_ARRAY => 'array', |
|
| 613 | + Type::JSON_ARRAY => 'array', |
|
| 614 | + Type::BIGINT => 'string', |
|
| 615 | + Type::BOOLEAN => 'bool', |
|
| 616 | + Type::DATETIME => '\DateTimeInterface', |
|
| 617 | + Type::DATETIMETZ => '\DateTimeInterface', |
|
| 618 | + Type::DATE => '\DateTimeInterface', |
|
| 619 | + Type::TIME => '\DateTimeInterface', |
|
| 620 | + Type::DECIMAL => 'float', |
|
| 621 | + Type::INTEGER => 'int', |
|
| 622 | + Type::OBJECT => 'string', |
|
| 623 | + Type::SMALLINT => 'int', |
|
| 624 | + Type::STRING => 'string', |
|
| 625 | + Type::TEXT => 'string', |
|
| 626 | + Type::BINARY => 'string', |
|
| 627 | + Type::BLOB => 'string', |
|
| 628 | + Type::FLOAT => 'float', |
|
| 629 | + Type::GUID => 'string' |
|
| 630 | + ]; |
|
| 631 | + |
|
| 632 | + return isset($map[$type->getName()])?$map[$type->getName()]:$type->getName(); |
|
| 633 | + } |
|
| 634 | + |
|
| 635 | + /** |
|
| 636 | + * |
|
| 637 | + * @param string $beanNamespace |
|
| 638 | + * @return \string[] Returns a map mapping table name to beans name |
|
| 639 | + */ |
|
| 640 | + public function buildTableToBeanMap($beanNamespace) { |
|
| 641 | + $tableToBeanMap = []; |
|
| 642 | + |
|
| 643 | + $tables = $this->schema->getTables(); |
|
| 644 | + |
|
| 645 | + foreach ($tables as $table) { |
|
| 646 | + $tableName = $table->getName(); |
|
| 647 | + $tableToBeanMap[$tableName] = $beanNamespace . "\\" . self::getBeanNameFromTableName($tableName); |
|
| 648 | + } |
|
| 649 | + return $tableToBeanMap; |
|
| 650 | + } |
|
| 651 | 651 | } |
@@ -29,7 +29,7 @@ |
||
| 29 | 29 | <div class="control-group"> |
| 30 | 30 | <label class="control-label">Store dates / timestamps in UTC:</label> |
| 31 | 31 | <div class="controls"> |
| 32 | - <input type="checkbox" name="storeInUtc" value="1" <?php echo $this->storeInUtc?'checked="checked"':"" ?>></input> |
|
| 32 | + <input type="checkbox" name="storeInUtc" value="1" <?php echo $this->storeInUtc ? 'checked="checked"' : "" ?>></input> |
|
| 33 | 33 | <span class="help-block">Select this option if you want timestamps to be stored in UTC. |
| 34 | 34 | If your application supports several time zones, you should select this option to store all dates in |
| 35 | 35 | the same time zone.</span> |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | * @param string $columnName |
| 86 | 86 | * @param array<string> $values |
| 87 | 87 | */ |
| 88 | - public function __construct($tableName=null, $columnName=null, $values=array()) { |
|
| 88 | + public function __construct($tableName = null, $columnName = null, $values = array()) { |
|
| 89 | 89 | $this->tableName = $tableName; |
| 90 | 90 | $this->columnName = $columnName; |
| 91 | 91 | $this->values = $values; |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - return $this->tableName.'.'.$this->columnName.' IN ('.implode(',',$values_sql).")"; |
|
| 120 | + return $this->tableName.'.'.$this->columnName.' IN ('.implode(',', $values_sql).")"; |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | /** |
@@ -86,7 +86,7 @@ |
||
| 86 | 86 | * @param string $columnName |
| 87 | 87 | * @param string $order |
| 88 | 88 | */ |
| 89 | - public function __construct($tableName=null, $columnName=null, $order=null) { |
|
| 89 | + public function __construct($tableName = null, $columnName = null, $order = null) { |
|
| 90 | 90 | $this->tableName = $tableName; |
| 91 | 91 | $this->columnName = $columnName; |
| 92 | 92 | $this->order = $order; |
@@ -59,7 +59,7 @@ |
||
| 59 | 59 | * |
| 60 | 60 | * @param FilterInterface $filter |
| 61 | 61 | */ |
| 62 | - public function __construct($filter=null) { |
|
| 62 | + public function __construct($filter = null) { |
|
| 63 | 63 | $this->filter = $filter; |
| 64 | 64 | } |
| 65 | 65 | |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | * |
| 63 | 63 | * @param string $sqlString |
| 64 | 64 | */ |
| 65 | - public function __construct($sqlString=null) { |
|
| 65 | + public function __construct($sqlString = null) { |
|
| 66 | 66 | $this->sqlString = $sqlString; |
| 67 | 67 | } |
| 68 | 68 | |
@@ -95,32 +95,32 @@ discard block |
||
| 95 | 95 | // First, let's remove all the stuff in quotes: |
| 96 | 96 | |
| 97 | 97 | // Let's remove all the \' found |
| 98 | - $work_str = str_replace("\\'",'',$this->sqlString); |
|
| 98 | + $work_str = str_replace("\\'", '', $this->sqlString); |
|
| 99 | 99 | // Now, let's split the string using ' |
| 100 | 100 | $work_table = explode("'", $work_str); |
| 101 | 101 | |
| 102 | - if (count($work_table)==0) |
|
| 102 | + if (count($work_table) == 0) |
|
| 103 | 103 | return ''; |
| 104 | 104 | |
| 105 | 105 | // if we start with a ', let's remove the first text |
| 106 | - if (strstr($work_str,"'")===0) |
|
| 106 | + if (strstr($work_str, "'") === 0) |
|
| 107 | 107 | array_shift($work_table); |
| 108 | 108 | |
| 109 | - if (count($work_table)==0) |
|
| 109 | + if (count($work_table) == 0) |
|
| 110 | 110 | return ''; |
| 111 | 111 | |
| 112 | 112 | // Now, let's take only the stuff outside the quotes. |
| 113 | 113 | $work_str2 = ''; |
| 114 | 114 | |
| 115 | - $i=0; |
|
| 115 | + $i = 0; |
|
| 116 | 116 | foreach ($work_table as $str_fragment) { |
| 117 | - if (($i % 2) == 0) |
|
| 117 | + if (($i%2) == 0) |
|
| 118 | 118 | $work_str2 .= $str_fragment.' '; |
| 119 | 119 | $i++; |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | // Now, let's run a regexp to find all the strings matching the pattern xxx.yyy |
| 123 | - preg_match_all('/([a-zA-Z_](?:[a-zA-Z0-9_]*))\.(?:[a-zA-Z_](?:[a-zA-Z0-9_]*))/', $work_str2,$capture_result); |
|
| 123 | + preg_match_all('/([a-zA-Z_](?:[a-zA-Z0-9_]*))\.(?:[a-zA-Z_](?:[a-zA-Z0-9_]*))/', $work_str2, $capture_result); |
|
| 124 | 124 | |
| 125 | 125 | $tables_used = $capture_result[1]; |
| 126 | 126 | // remove doubles: |
@@ -99,23 +99,27 @@ |
||
| 99 | 99 | // Now, let's split the string using ' |
| 100 | 100 | $work_table = explode("'", $work_str); |
| 101 | 101 | |
| 102 | - if (count($work_table)==0) |
|
| 103 | - return ''; |
|
| 102 | + if (count($work_table)==0) { |
|
| 103 | + return ''; |
|
| 104 | + } |
|
| 104 | 105 | |
| 105 | 106 | // if we start with a ', let's remove the first text |
| 106 | - if (strstr($work_str,"'")===0) |
|
| 107 | - array_shift($work_table); |
|
| 107 | + if (strstr($work_str,"'")===0) { |
|
| 108 | + array_shift($work_table); |
|
| 109 | + } |
|
| 108 | 110 | |
| 109 | - if (count($work_table)==0) |
|
| 110 | - return ''; |
|
| 111 | + if (count($work_table)==0) { |
|
| 112 | + return ''; |
|
| 113 | + } |
|
| 111 | 114 | |
| 112 | 115 | // Now, let's take only the stuff outside the quotes. |
| 113 | 116 | $work_str2 = ''; |
| 114 | 117 | |
| 115 | 118 | $i=0; |
| 116 | 119 | foreach ($work_table as $str_fragment) { |
| 117 | - if (($i % 2) == 0) |
|
| 118 | - $work_str2 .= $str_fragment.' '; |
|
| 120 | + if (($i % 2) == 0) { |
|
| 121 | + $work_str2 .= $str_fragment.' '; |
|
| 122 | + } |
|
| 119 | 123 | $i++; |
| 120 | 124 | } |
| 121 | 125 | |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | * |
| 50 | 50 | * @param array<FilterInterface> $filters |
| 51 | 51 | */ |
| 52 | - public function __construct(array $filters=array()) { |
|
| 52 | + public function __construct(array $filters = array()) { |
|
| 53 | 53 | $this->filters = $filters; |
| 54 | 54 | } |
| 55 | 55 | |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | if (count($filters_sql)>0) { |
| 94 | - return '('.implode(' AND ',$filters_sql).')'; |
|
| 94 | + return '('.implode(' AND ', $filters_sql).')'; |
|
| 95 | 95 | } else { |
| 96 | 96 | return ''; |
| 97 | 97 | } |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | throw new TDBMException("Error in AndFilter: One of the parameters is not a filter."); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - $tables = array_merge($tables,$filter->getUsedTables()); |
|
| 117 | + $tables = array_merge($tables, $filter->getUsedTables()); |
|
| 118 | 118 | } |
| 119 | 119 | // Remove tables in double. |
| 120 | 120 | $tables = array_flip(array_flip($tables)); |
@@ -9,14 +9,14 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class FilterUtils |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @param string|null|DateTimeInterface $value |
|
| 14 | - */ |
|
| 15 | - public static function valueToSql($value, Connection $dbConnection) { |
|
| 16 | - if ($value instanceof DateTimeInterface) { |
|
| 17 | - return "'".$value->format('Y-m-d H:i:s')."'"; |
|
| 18 | - } else { |
|
| 19 | - return $dbConnection->quote($value); |
|
| 20 | - } |
|
| 21 | - } |
|
| 12 | + /** |
|
| 13 | + * @param string|null|DateTimeInterface $value |
|
| 14 | + */ |
|
| 15 | + public static function valueToSql($value, Connection $dbConnection) { |
|
| 16 | + if ($value instanceof DateTimeInterface) { |
|
| 17 | + return "'".$value->format('Y-m-d H:i:s')."'"; |
|
| 18 | + } else { |
|
| 19 | + return $dbConnection->quote($value); |
|
| 20 | + } |
|
| 21 | + } |
|
| 22 | 22 | } |
@@ -98,8 +98,8 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @param string $tableName |
| 100 | 100 | * @param string $columnName |
| 101 | - * @param string $value1 |
|
| 102 | - * @param string $value2 |
|
| 101 | + * @param string $value1 |
|
| 102 | + * @param string $value2 |
|
| 103 | 103 | */ |
| 104 | 104 | public function __construct($tableName=null, $columnName=null, $value1=null, $value2=null) { |
| 105 | 105 | $this->tableName = $tableName; |
@@ -113,9 +113,9 @@ discard block |
||
| 113 | 113 | * |
| 114 | 114 | * @param Connection $dbConnection |
| 115 | 115 | * @return string |
| 116 | - * @throws \Mouf\Database\TDBM\TDBMException |
|
| 116 | + * @throws \Mouf\Database\TDBM\TDBMException |
|
| 117 | 117 | */ |
| 118 | - public function toSql(Connection $dbConnection) { |
|
| 118 | + public function toSql(Connection $dbConnection) { |
|
| 119 | 119 | if ($this->enableCondition != null && !$this->enableCondition->isOk()) { |
| 120 | 120 | return ""; |
| 121 | 121 | } |
@@ -101,7 +101,7 @@ |
||
| 101 | 101 | * @param string $value1 |
| 102 | 102 | * @param string $value2 |
| 103 | 103 | */ |
| 104 | - public function __construct($tableName=null, $columnName=null, $value1=null, $value2=null) { |
|
| 104 | + public function __construct($tableName = null, $columnName = null, $value1 = null, $value2 = null) { |
|
| 105 | 105 | $this->tableName = $tableName; |
| 106 | 106 | $this->columnName = $columnName; |
| 107 | 107 | $this->value1 = $value1; |