Passed
Push — 4.3 ( 06c551...66bb88 )
by David
01:20
created
src/Mouf/Database/TDBM/Utils/TDBMDaoGenerator.php 2 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -9,7 +9,6 @@
 block discarded – undo
9 9
 use Mouf\Database\TDBM\ConfigurationInterface;
10 10
 use Mouf\Database\TDBM\TDBMException;
11 11
 use Mouf\Database\TDBM\TDBMSchemaAnalyzer;
12
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13 12
 use Symfony\Component\Filesystem\Filesystem;
14 13
 
15 14
 /**
Please login to merge, or discard this patch.
Indentation   +356 added lines, -356 removed lines patch added patch discarded remove patch
@@ -17,132 +17,132 @@  discard block
 block discarded – undo
17 17
  */
18 18
 class TDBMDaoGenerator
19 19
 {
20
-    /**
21
-     * @var Schema
22
-     */
23
-    private $schema;
24
-
25
-    /**
26
-     * Name of composer file.
27
-     *
28
-     * @var string
29
-     */
30
-    private $composerFile;
31
-
32
-    /**
33
-     * @var TDBMSchemaAnalyzer
34
-     */
35
-    private $tdbmSchemaAnalyzer;
36
-
37
-    /**
38
-     * @var GeneratorListenerInterface
39
-     */
40
-    private $eventDispatcher;
41
-
42
-    /**
43
-     * @var NamingStrategyInterface
44
-     */
45
-    private $namingStrategy;
46
-    /**
47
-     * @var ConfigurationInterface
48
-     */
49
-    private $configuration;
50
-
51
-    /**
52
-     * Constructor.
53
-     *
54
-     * @param ConfigurationInterface $configuration
55
-     * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer
56
-     */
57
-    public function __construct(ConfigurationInterface $configuration, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer)
58
-    {
59
-        $this->configuration = $configuration;
60
-        $this->schema = $tdbmSchemaAnalyzer->getSchema();
61
-        $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer;
62
-        $this->namingStrategy = $configuration->getNamingStrategy();
63
-        $this->eventDispatcher = $configuration->getGeneratorEventDispatcher();
64
-    }
65
-
66
-    /**
67
-     * Generates all the daos and beans.
68
-     *
69
-     * @throws TDBMException
70
-     */
71
-    public function generateAllDaosAndBeans(): void
72
-    {
73
-        // TODO: check that no class name ends with "Base". Otherwise, there will be name clash.
74
-
75
-        $tableList = $this->schema->getTables();
76
-
77
-        // Remove all beans and daos from junction tables
78
-        $junctionTables = $this->configuration->getSchemaAnalyzer()->detectJunctionTables(true);
79
-        $junctionTableNames = array_map(function (Table $table) {
80
-            return $table->getName();
81
-        }, $junctionTables);
82
-
83
-        $tableList = array_filter($tableList, function (Table $table) use ($junctionTableNames) {
84
-            return !in_array($table->getName(), $junctionTableNames);
85
-        });
86
-
87
-        $beanDescriptors = [];
88
-
89
-        foreach ($tableList as $table) {
90
-            $beanDescriptors[] = $this->generateDaoAndBean($table);
91
-        }
92
-
93
-
94
-        $this->generateFactory($tableList);
95
-
96
-        // Let's call the list of listeners
97
-        $this->eventDispatcher->onGenerate($this->configuration, $beanDescriptors);
98
-    }
99
-
100
-    /**
101
-     * Generates in one method call the daos and the beans for one table.
102
-     *
103
-     * @param Table $table
104
-     *
105
-     * @return BeanDescriptor
106
-     * @throws TDBMException
107
-     */
108
-    private function generateDaoAndBean(Table $table) : BeanDescriptor
109
-    {
110
-        $tableName = $table->getName();
111
-        $daoName = $this->namingStrategy->getDaoClassName($tableName);
112
-        $beanName = $this->namingStrategy->getBeanClassName($tableName);
113
-        $baseBeanName = $this->namingStrategy->getBaseBeanClassName($tableName);
114
-        $baseDaoName = $this->namingStrategy->getBaseDaoClassName($tableName);
115
-
116
-        $beanDescriptor = new BeanDescriptor($table, $this->configuration->getSchemaAnalyzer(), $this->schema, $this->tdbmSchemaAnalyzer, $this->namingStrategy);
117
-        $this->generateBean($beanDescriptor, $beanName, $baseBeanName, $table);
118
-        $this->generateDao($beanDescriptor, $daoName, $baseDaoName, $beanName, $table);
119
-        return $beanDescriptor;
120
-    }
121
-
122
-    /**
123
-     * Writes the PHP bean file with all getters and setters from the table passed in parameter.
124
-     *
125
-     * @param BeanDescriptor  $beanDescriptor
126
-     * @param string          $className       The name of the class
127
-     * @param string          $baseClassName   The name of the base class which will be extended (name only, no directory)
128
-     * @param Table           $table           The table
129
-     *
130
-     * @throws TDBMException
131
-     */
132
-    public function generateBean(BeanDescriptor $beanDescriptor, $className, $baseClassName, Table $table)
133
-    {
134
-        $beannamespace = $this->configuration->getBeanNamespace();
135
-        $str = $beanDescriptor->generatePhpCode($beannamespace);
136
-
137
-        $possibleBaseFileName = $this->configuration->getPathFinder()->getPath($beannamespace.'\\Generated\\'.$baseClassName)->getPathname();
138
-
139
-        $this->dumpFile($possibleBaseFileName, $str);
140
-
141
-        $possibleFileName = $this->configuration->getPathFinder()->getPath($beannamespace.'\\'.$className)->getPathname();
142
-
143
-        if (!file_exists($possibleFileName)) {
144
-            $tableName = $table->getName();
145
-            $str = "<?php
20
+	/**
21
+	 * @var Schema
22
+	 */
23
+	private $schema;
24
+
25
+	/**
26
+	 * Name of composer file.
27
+	 *
28
+	 * @var string
29
+	 */
30
+	private $composerFile;
31
+
32
+	/**
33
+	 * @var TDBMSchemaAnalyzer
34
+	 */
35
+	private $tdbmSchemaAnalyzer;
36
+
37
+	/**
38
+	 * @var GeneratorListenerInterface
39
+	 */
40
+	private $eventDispatcher;
41
+
42
+	/**
43
+	 * @var NamingStrategyInterface
44
+	 */
45
+	private $namingStrategy;
46
+	/**
47
+	 * @var ConfigurationInterface
48
+	 */
49
+	private $configuration;
50
+
51
+	/**
52
+	 * Constructor.
53
+	 *
54
+	 * @param ConfigurationInterface $configuration
55
+	 * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer
56
+	 */
57
+	public function __construct(ConfigurationInterface $configuration, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer)
58
+	{
59
+		$this->configuration = $configuration;
60
+		$this->schema = $tdbmSchemaAnalyzer->getSchema();
61
+		$this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer;
62
+		$this->namingStrategy = $configuration->getNamingStrategy();
63
+		$this->eventDispatcher = $configuration->getGeneratorEventDispatcher();
64
+	}
65
+
66
+	/**
67
+	 * Generates all the daos and beans.
68
+	 *
69
+	 * @throws TDBMException
70
+	 */
71
+	public function generateAllDaosAndBeans(): void
72
+	{
73
+		// TODO: check that no class name ends with "Base". Otherwise, there will be name clash.
74
+
75
+		$tableList = $this->schema->getTables();
76
+
77
+		// Remove all beans and daos from junction tables
78
+		$junctionTables = $this->configuration->getSchemaAnalyzer()->detectJunctionTables(true);
79
+		$junctionTableNames = array_map(function (Table $table) {
80
+			return $table->getName();
81
+		}, $junctionTables);
82
+
83
+		$tableList = array_filter($tableList, function (Table $table) use ($junctionTableNames) {
84
+			return !in_array($table->getName(), $junctionTableNames);
85
+		});
86
+
87
+		$beanDescriptors = [];
88
+
89
+		foreach ($tableList as $table) {
90
+			$beanDescriptors[] = $this->generateDaoAndBean($table);
91
+		}
92
+
93
+
94
+		$this->generateFactory($tableList);
95
+
96
+		// Let's call the list of listeners
97
+		$this->eventDispatcher->onGenerate($this->configuration, $beanDescriptors);
98
+	}
99
+
100
+	/**
101
+	 * Generates in one method call the daos and the beans for one table.
102
+	 *
103
+	 * @param Table $table
104
+	 *
105
+	 * @return BeanDescriptor
106
+	 * @throws TDBMException
107
+	 */
108
+	private function generateDaoAndBean(Table $table) : BeanDescriptor
109
+	{
110
+		$tableName = $table->getName();
111
+		$daoName = $this->namingStrategy->getDaoClassName($tableName);
112
+		$beanName = $this->namingStrategy->getBeanClassName($tableName);
113
+		$baseBeanName = $this->namingStrategy->getBaseBeanClassName($tableName);
114
+		$baseDaoName = $this->namingStrategy->getBaseDaoClassName($tableName);
115
+
116
+		$beanDescriptor = new BeanDescriptor($table, $this->configuration->getSchemaAnalyzer(), $this->schema, $this->tdbmSchemaAnalyzer, $this->namingStrategy);
117
+		$this->generateBean($beanDescriptor, $beanName, $baseBeanName, $table);
118
+		$this->generateDao($beanDescriptor, $daoName, $baseDaoName, $beanName, $table);
119
+		return $beanDescriptor;
120
+	}
121
+
122
+	/**
123
+	 * Writes the PHP bean file with all getters and setters from the table passed in parameter.
124
+	 *
125
+	 * @param BeanDescriptor  $beanDescriptor
126
+	 * @param string          $className       The name of the class
127
+	 * @param string          $baseClassName   The name of the base class which will be extended (name only, no directory)
128
+	 * @param Table           $table           The table
129
+	 *
130
+	 * @throws TDBMException
131
+	 */
132
+	public function generateBean(BeanDescriptor $beanDescriptor, $className, $baseClassName, Table $table)
133
+	{
134
+		$beannamespace = $this->configuration->getBeanNamespace();
135
+		$str = $beanDescriptor->generatePhpCode($beannamespace);
136
+
137
+		$possibleBaseFileName = $this->configuration->getPathFinder()->getPath($beannamespace.'\\Generated\\'.$baseClassName)->getPathname();
138
+
139
+		$this->dumpFile($possibleBaseFileName, $str);
140
+
141
+		$possibleFileName = $this->configuration->getPathFinder()->getPath($beannamespace.'\\'.$className)->getPathname();
142
+
143
+		if (!file_exists($possibleFileName)) {
144
+			$tableName = $table->getName();
145
+			$str = "<?php
146 146
 /*
147 147
  * This file has been automatically generated by TDBM.
148 148
  * You can edit this file as it will not be overwritten.
@@ -160,73 +160,73 @@  discard block
 block discarded – undo
160 160
 }
161 161
 ";
162 162
 
163
-            $this->dumpFile($possibleFileName, $str);
164
-        }
165
-    }
166
-
167
-    /**
168
-     * Tries to find a @defaultSort annotation in one of the columns.
169
-     *
170
-     * @param Table $table
171
-     *
172
-     * @return array First item: column name, Second item: column order (asc/desc)
173
-     */
174
-    private function getDefaultSortColumnFromAnnotation(Table $table)
175
-    {
176
-        $defaultSort = null;
177
-        $defaultSortDirection = null;
178
-        foreach ($table->getColumns() as $column) {
179
-            $comments = $column->getComment();
180
-            $matches = [];
181
-            if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) {
182
-                $defaultSort = $column->getName();
183
-                if (count($matches) === 3) {
184
-                    $defaultSortDirection = $matches[2];
185
-                } else {
186
-                    $defaultSortDirection = 'ASC';
187
-                }
188
-            }
189
-        }
190
-
191
-        return [$defaultSort, $defaultSortDirection];
192
-    }
193
-
194
-    /**
195
-     * Writes the PHP bean DAO with simple functions to create/get/save objects.
196
-     *
197
-     * @param BeanDescriptor  $beanDescriptor
198
-     * @param string          $className       The name of the class
199
-     * @param string          $baseClassName
200
-     * @param string          $beanClassName
201
-     * @param Table           $table
202
-     *
203
-     * @throws TDBMException
204
-     */
205
-    private function generateDao(BeanDescriptor $beanDescriptor, string $className, string $baseClassName, string $beanClassName, Table $table)
206
-    {
207
-        $daonamespace = $this->configuration->getDaoNamespace();
208
-        $beannamespace = $this->configuration->getBeanNamespace();
209
-        $tableName = $table->getName();
210
-        $primaryKeyColumns = $table->getPrimaryKeyColumns();
211
-
212
-        list($defaultSort, $defaultSortDirection) = $this->getDefaultSortColumnFromAnnotation($table);
213
-
214
-        // FIXME: lowercase tables with _ in the name should work!
215
-        $tableCamel = self::toSingular(self::toCamelCase($tableName));
216
-
217
-        $beanClassWithoutNameSpace = $beanClassName;
218
-        $beanClassName = $beannamespace.'\\'.$beanClassName;
219
-
220
-        list($usedBeans, $findByDaoCode) = $beanDescriptor->generateFindByDaoCode($beannamespace, $beanClassWithoutNameSpace);
221
-
222
-        $usedBeans[] = $beanClassName;
223
-        // Let's suppress duplicates in used beans (if any)
224
-        $usedBeans = array_flip(array_flip($usedBeans));
225
-        $useStatements = array_map(function ($usedBean) {
226
-            return "use $usedBean;\n";
227
-        }, $usedBeans);
228
-
229
-        $str = "<?php
163
+			$this->dumpFile($possibleFileName, $str);
164
+		}
165
+	}
166
+
167
+	/**
168
+	 * Tries to find a @defaultSort annotation in one of the columns.
169
+	 *
170
+	 * @param Table $table
171
+	 *
172
+	 * @return array First item: column name, Second item: column order (asc/desc)
173
+	 */
174
+	private function getDefaultSortColumnFromAnnotation(Table $table)
175
+	{
176
+		$defaultSort = null;
177
+		$defaultSortDirection = null;
178
+		foreach ($table->getColumns() as $column) {
179
+			$comments = $column->getComment();
180
+			$matches = [];
181
+			if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) {
182
+				$defaultSort = $column->getName();
183
+				if (count($matches) === 3) {
184
+					$defaultSortDirection = $matches[2];
185
+				} else {
186
+					$defaultSortDirection = 'ASC';
187
+				}
188
+			}
189
+		}
190
+
191
+		return [$defaultSort, $defaultSortDirection];
192
+	}
193
+
194
+	/**
195
+	 * Writes the PHP bean DAO with simple functions to create/get/save objects.
196
+	 *
197
+	 * @param BeanDescriptor  $beanDescriptor
198
+	 * @param string          $className       The name of the class
199
+	 * @param string          $baseClassName
200
+	 * @param string          $beanClassName
201
+	 * @param Table           $table
202
+	 *
203
+	 * @throws TDBMException
204
+	 */
205
+	private function generateDao(BeanDescriptor $beanDescriptor, string $className, string $baseClassName, string $beanClassName, Table $table)
206
+	{
207
+		$daonamespace = $this->configuration->getDaoNamespace();
208
+		$beannamespace = $this->configuration->getBeanNamespace();
209
+		$tableName = $table->getName();
210
+		$primaryKeyColumns = $table->getPrimaryKeyColumns();
211
+
212
+		list($defaultSort, $defaultSortDirection) = $this->getDefaultSortColumnFromAnnotation($table);
213
+
214
+		// FIXME: lowercase tables with _ in the name should work!
215
+		$tableCamel = self::toSingular(self::toCamelCase($tableName));
216
+
217
+		$beanClassWithoutNameSpace = $beanClassName;
218
+		$beanClassName = $beannamespace.'\\'.$beanClassName;
219
+
220
+		list($usedBeans, $findByDaoCode) = $beanDescriptor->generateFindByDaoCode($beannamespace, $beanClassWithoutNameSpace);
221
+
222
+		$usedBeans[] = $beanClassName;
223
+		// Let's suppress duplicates in used beans (if any)
224
+		$usedBeans = array_flip(array_flip($usedBeans));
225
+		$useStatements = array_map(function ($usedBean) {
226
+			return "use $usedBean;\n";
227
+		}, $usedBeans);
228
+
229
+		$str = "<?php
230 230
 
231 231
 /*
232 232
  * This file has been automatically generated by TDBM.
@@ -302,10 +302,10 @@  discard block
 block discarded – undo
302 302
     }
303 303
     ";
304 304
 
305
-        if (count($primaryKeyColumns) === 1) {
306
-            $primaryKeyColumn = $primaryKeyColumns[0];
307
-            $primaryKeyPhpType = self::dbalTypeToPhpType($table->getColumn($primaryKeyColumn)->getType());
308
-            $str .= "
305
+		if (count($primaryKeyColumns) === 1) {
306
+			$primaryKeyColumn = $primaryKeyColumns[0];
307
+			$primaryKeyPhpType = self::dbalTypeToPhpType($table->getColumn($primaryKeyColumn)->getType());
308
+			$str .= "
309 309
     /**
310 310
      * Get $beanClassWithoutNameSpace specified by its ID (its primary key)
311 311
      * If the primary key does not exist, an exception is thrown.
@@ -320,8 +320,8 @@  discard block
 block discarded – undo
320 320
         return \$this->tdbmService->findObjectByPk('$tableName', ['$primaryKeyColumn' => \$id], [], \$lazyLoading);
321 321
     }
322 322
     ";
323
-        }
324
-        $str .= "
323
+		}
324
+		$str .= "
325 325
     /**
326 326
      * Deletes the $beanClassWithoutNameSpace passed in parameter.
327 327
      *
@@ -421,19 +421,19 @@  discard block
 block discarded – undo
421 421
     }
422 422
 ";
423 423
 
424
-        $str .= $findByDaoCode;
425
-        $str .= '}
424
+		$str .= $findByDaoCode;
425
+		$str .= '}
426 426
 ';
427 427
 
428
-        $possibleBaseFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\Generated\\'.$baseClassName)->getPathname();
428
+		$possibleBaseFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\Generated\\'.$baseClassName)->getPathname();
429 429
 
430
-        $this->dumpFile($possibleBaseFileName, $str);
430
+		$this->dumpFile($possibleBaseFileName, $str);
431 431
 
432
-        $possibleFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\'.$className)->getPathname();
432
+		$possibleFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\'.$className)->getPathname();
433 433
 
434
-        // Now, let's generate the "editable" class
435
-        if (!file_exists($possibleFileName)) {
436
-            $str = "<?php
434
+		// Now, let's generate the "editable" class
435
+		if (!file_exists($possibleFileName)) {
436
+			$str = "<?php
437 437
 
438 438
 /*
439 439
  * This file has been automatically generated by TDBM.
@@ -451,24 +451,24 @@  discard block
 block discarded – undo
451 451
 {
452 452
 }
453 453
 ";
454
-            $this->dumpFile($possibleFileName, $str);
455
-        }
456
-    }
454
+			$this->dumpFile($possibleFileName, $str);
455
+		}
456
+	}
457 457
 
458
-    /**
459
-     * Generates the factory bean.
460
-     *
461
-     * @param Table[] $tableList
462
-     * @throws TDBMException
463
-     */
464
-    private function generateFactory(array $tableList) : void
465
-    {
466
-        $daoNamespace = $this->configuration->getDaoNamespace();
467
-        $daoFactoryClassName = $this->namingStrategy->getDaoFactoryClassName();
458
+	/**
459
+	 * Generates the factory bean.
460
+	 *
461
+	 * @param Table[] $tableList
462
+	 * @throws TDBMException
463
+	 */
464
+	private function generateFactory(array $tableList) : void
465
+	{
466
+		$daoNamespace = $this->configuration->getDaoNamespace();
467
+		$daoFactoryClassName = $this->namingStrategy->getDaoFactoryClassName();
468 468
 
469
-        // For each table, let's write a property.
469
+		// For each table, let's write a property.
470 470
 
471
-        $str = "<?php
471
+		$str = "<?php
472 472
 
473 473
 /*
474 474
  * This file has been automatically generated by TDBM.
@@ -478,13 +478,13 @@  discard block
 block discarded – undo
478 478
 namespace {$daoNamespace}\\Generated;
479 479
 
480 480
 ";
481
-        foreach ($tableList as $table) {
482
-            $tableName = $table->getName();
483
-            $daoClassName = $this->namingStrategy->getDaoClassName($tableName);
484
-            $str .= "use {$daoNamespace}\\".$daoClassName.";\n";
485
-        }
481
+		foreach ($tableList as $table) {
482
+			$tableName = $table->getName();
483
+			$daoClassName = $this->namingStrategy->getDaoClassName($tableName);
484
+			$str .= "use {$daoNamespace}\\".$daoClassName.";\n";
485
+		}
486 486
 
487
-        $str .= "
487
+		$str .= "
488 488
 /**
489 489
  * The $daoFactoryClassName provides an easy access to all DAOs generated by TDBM.
490 490
  *
@@ -493,12 +493,12 @@  discard block
 block discarded – undo
493 493
 {
494 494
 ";
495 495
 
496
-        foreach ($tableList as $table) {
497
-            $tableName = $table->getName();
498
-            $daoClassName = $this->namingStrategy->getDaoClassName($tableName);
499
-            $daoInstanceName = self::toVariableName($daoClassName);
496
+		foreach ($tableList as $table) {
497
+			$tableName = $table->getName();
498
+			$daoClassName = $this->namingStrategy->getDaoClassName($tableName);
499
+			$daoInstanceName = self::toVariableName($daoClassName);
500 500
 
501
-            $str .= '    /**
501
+			$str .= '    /**
502 502
      * @var '.$daoClassName.'
503 503
      */
504 504
     private $'.$daoInstanceName.';
@@ -522,131 +522,131 @@  discard block
 block discarded – undo
522 522
     {
523 523
         $this->'.$daoInstanceName.' = $'.$daoInstanceName.';
524 524
     }';
525
-        }
525
+		}
526 526
 
527
-        $str .= '
527
+		$str .= '
528 528
 }
529 529
 ';
530 530
 
531
-        $possibleFileName = $this->configuration->getPathFinder()->getPath($daoNamespace.'\\Generated\\'.$daoFactoryClassName)->getPathname();
532
-
533
-        $this->dumpFile($possibleFileName, $str);
534
-    }
535
-
536
-    /**
537
-     * Transforms a string to camelCase (except the first letter will be uppercase too).
538
-     * Underscores and spaces are removed and the first letter after the underscore is uppercased.
539
-     *
540
-     * @param $str string
541
-     *
542
-     * @return string
543
-     */
544
-    public static function toCamelCase($str)
545
-    {
546
-        $str = strtoupper(substr($str, 0, 1)).substr($str, 1);
547
-        while (true) {
548
-            if (strpos($str, '_') === false && strpos($str, ' ') === false) {
549
-                break;
550
-            }
551
-
552
-            $pos = strpos($str, '_');
553
-            if ($pos === false) {
554
-                $pos = strpos($str, ' ');
555
-            }
556
-            $before = substr($str, 0, $pos);
557
-            $after = substr($str, $pos + 1);
558
-            $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1);
559
-        }
560
-
561
-        return $str;
562
-    }
563
-
564
-    /**
565
-     * Tries to put string to the singular form (if it is plural).
566
-     * We assume the table names are in english.
567
-     *
568
-     * @param $str string
569
-     *
570
-     * @return string
571
-     */
572
-    public static function toSingular($str)
573
-    {
574
-        return Inflector::singularize($str);
575
-    }
576
-
577
-    /**
578
-     * Put the first letter of the string in lower case.
579
-     * Very useful to transform a class name into a variable name.
580
-     *
581
-     * @param $str string
582
-     *
583
-     * @return string
584
-     */
585
-    public static function toVariableName($str)
586
-    {
587
-        return strtolower(substr($str, 0, 1)).substr($str, 1);
588
-    }
589
-
590
-    /**
591
-     * Ensures the file passed in parameter can be written in its directory.
592
-     *
593
-     * @param string $fileName
594
-     *
595
-     * @throws TDBMException
596
-     */
597
-    private function ensureDirectoryExist(string $fileName)
598
-    {
599
-        $dirName = dirname($fileName);
600
-        if (!file_exists($dirName)) {
601
-            $old = umask(0);
602
-            $result = mkdir($dirName, 0775, true);
603
-            umask($old);
604
-            if ($result === false) {
605
-                throw new TDBMException("Unable to create directory: '".$dirName."'.");
606
-            }
607
-        }
608
-    }
609
-
610
-    private function dumpFile(string $fileName, string $content) : void
611
-    {
612
-        $this->ensureDirectoryExist($fileName);
613
-        $fileSystem = new Filesystem();
614
-        $fileSystem->dumpFile($fileName, $content);
615
-        @chmod($fileName, 0664);
616
-    }
617
-
618
-    /**
619
-     * Transforms a DBAL type into a PHP type (for PHPDoc purpose).
620
-     *
621
-     * @param Type $type The DBAL type
622
-     *
623
-     * @return string The PHP type
624
-     */
625
-    public static function dbalTypeToPhpType(Type $type)
626
-    {
627
-        $map = [
628
-            Type::TARRAY => 'array',
629
-            Type::SIMPLE_ARRAY => 'array',
630
-            'json' => 'array',  // 'json' is supported from Doctrine DBAL 2.6 only.
631
-            Type::JSON_ARRAY => 'array',
632
-            Type::BIGINT => 'string',
633
-            Type::BOOLEAN => 'bool',
634
-            Type::DATETIME => '\DateTimeInterface',
635
-            Type::DATETIMETZ => '\DateTimeInterface',
636
-            Type::DATE => '\DateTimeInterface',
637
-            Type::TIME => '\DateTimeInterface',
638
-            Type::DECIMAL => 'float',
639
-            Type::INTEGER => 'int',
640
-            Type::OBJECT => 'string',
641
-            Type::SMALLINT => 'int',
642
-            Type::STRING => 'string',
643
-            Type::TEXT => 'string',
644
-            Type::BINARY => 'string',
645
-            Type::BLOB => 'string',
646
-            Type::FLOAT => 'float',
647
-            Type::GUID => 'string',
648
-        ];
649
-
650
-        return isset($map[$type->getName()]) ? $map[$type->getName()] : $type->getName();
651
-    }
531
+		$possibleFileName = $this->configuration->getPathFinder()->getPath($daoNamespace.'\\Generated\\'.$daoFactoryClassName)->getPathname();
532
+
533
+		$this->dumpFile($possibleFileName, $str);
534
+	}
535
+
536
+	/**
537
+	 * Transforms a string to camelCase (except the first letter will be uppercase too).
538
+	 * Underscores and spaces are removed and the first letter after the underscore is uppercased.
539
+	 *
540
+	 * @param $str string
541
+	 *
542
+	 * @return string
543
+	 */
544
+	public static function toCamelCase($str)
545
+	{
546
+		$str = strtoupper(substr($str, 0, 1)).substr($str, 1);
547
+		while (true) {
548
+			if (strpos($str, '_') === false && strpos($str, ' ') === false) {
549
+				break;
550
+			}
551
+
552
+			$pos = strpos($str, '_');
553
+			if ($pos === false) {
554
+				$pos = strpos($str, ' ');
555
+			}
556
+			$before = substr($str, 0, $pos);
557
+			$after = substr($str, $pos + 1);
558
+			$str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1);
559
+		}
560
+
561
+		return $str;
562
+	}
563
+
564
+	/**
565
+	 * Tries to put string to the singular form (if it is plural).
566
+	 * We assume the table names are in english.
567
+	 *
568
+	 * @param $str string
569
+	 *
570
+	 * @return string
571
+	 */
572
+	public static function toSingular($str)
573
+	{
574
+		return Inflector::singularize($str);
575
+	}
576
+
577
+	/**
578
+	 * Put the first letter of the string in lower case.
579
+	 * Very useful to transform a class name into a variable name.
580
+	 *
581
+	 * @param $str string
582
+	 *
583
+	 * @return string
584
+	 */
585
+	public static function toVariableName($str)
586
+	{
587
+		return strtolower(substr($str, 0, 1)).substr($str, 1);
588
+	}
589
+
590
+	/**
591
+	 * Ensures the file passed in parameter can be written in its directory.
592
+	 *
593
+	 * @param string $fileName
594
+	 *
595
+	 * @throws TDBMException
596
+	 */
597
+	private function ensureDirectoryExist(string $fileName)
598
+	{
599
+		$dirName = dirname($fileName);
600
+		if (!file_exists($dirName)) {
601
+			$old = umask(0);
602
+			$result = mkdir($dirName, 0775, true);
603
+			umask($old);
604
+			if ($result === false) {
605
+				throw new TDBMException("Unable to create directory: '".$dirName."'.");
606
+			}
607
+		}
608
+	}
609
+
610
+	private function dumpFile(string $fileName, string $content) : void
611
+	{
612
+		$this->ensureDirectoryExist($fileName);
613
+		$fileSystem = new Filesystem();
614
+		$fileSystem->dumpFile($fileName, $content);
615
+		@chmod($fileName, 0664);
616
+	}
617
+
618
+	/**
619
+	 * Transforms a DBAL type into a PHP type (for PHPDoc purpose).
620
+	 *
621
+	 * @param Type $type The DBAL type
622
+	 *
623
+	 * @return string The PHP type
624
+	 */
625
+	public static function dbalTypeToPhpType(Type $type)
626
+	{
627
+		$map = [
628
+			Type::TARRAY => 'array',
629
+			Type::SIMPLE_ARRAY => 'array',
630
+			'json' => 'array',  // 'json' is supported from Doctrine DBAL 2.6 only.
631
+			Type::JSON_ARRAY => 'array',
632
+			Type::BIGINT => 'string',
633
+			Type::BOOLEAN => 'bool',
634
+			Type::DATETIME => '\DateTimeInterface',
635
+			Type::DATETIMETZ => '\DateTimeInterface',
636
+			Type::DATE => '\DateTimeInterface',
637
+			Type::TIME => '\DateTimeInterface',
638
+			Type::DECIMAL => 'float',
639
+			Type::INTEGER => 'int',
640
+			Type::OBJECT => 'string',
641
+			Type::SMALLINT => 'int',
642
+			Type::STRING => 'string',
643
+			Type::TEXT => 'string',
644
+			Type::BINARY => 'string',
645
+			Type::BLOB => 'string',
646
+			Type::FLOAT => 'float',
647
+			Type::GUID => 'string',
648
+		];
649
+
650
+		return isset($map[$type->getName()]) ? $map[$type->getName()] : $type->getName();
651
+	}
652 652
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/Utils/PathFinder/PathFinder.php 1 patch
Indentation   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -5,58 +5,58 @@
 block discarded – undo
5 5
 
6 6
 class PathFinder implements PathFinderInterface
7 7
 {
8
-    /**
9
-     * @var string
10
-     */
11
-    private $composerFile;
12
-
13
-    /**
14
-     * @var string
15
-     */
16
-    private $rootPath;
17
-
18
-    /**
19
-     * @var bool
20
-     */
21
-    private $useAutoloadDev;
22
-
23
-    /**
24
-     * @var ClassNameMapper
25
-     */
26
-    private $classNameMapper;
27
-
28
-    public function __construct(string $composerFile = null, string $rootPath = null, bool $useAutoloadDev = false)
29
-    {
30
-        $this->composerFile = $composerFile;
31
-        $this->useAutoloadDev = $useAutoloadDev;
32
-        if ($rootPath === null) {
33
-            $this->rootPath = dirname(__DIR__, 9);
34
-        } else {
35
-            $this->rootPath = $rootPath;
36
-        }
37
-    }
38
-
39
-    private function getClassNameMapper() : ClassNameMapper
40
-    {
41
-        if ($this->classNameMapper === null) {
42
-            $this->classNameMapper = ClassNameMapper::createFromComposerFile($this->composerFile, $this->rootPath, $this->useAutoloadDev);
43
-        }
44
-        return $this->classNameMapper;
45
-    }
46
-
47
-    /**
48
-     * Returns the path of a class file given the fully qualified class name.
49
-     *
50
-     * @param string $className
51
-     * @return \SplFileInfo
52
-     * @throws NoPathFoundException
53
-     */
54
-    public function getPath(string $className): \SplFileInfo
55
-    {
56
-        $paths = $this->getClassNameMapper()->getPossibleFileNames($className);
57
-        if (empty($paths)) {
58
-            throw NoPathFoundException::create($className);
59
-        }
60
-        return new \SplFileInfo($this->rootPath.'/'.$paths[0]);
61
-    }
8
+	/**
9
+	 * @var string
10
+	 */
11
+	private $composerFile;
12
+
13
+	/**
14
+	 * @var string
15
+	 */
16
+	private $rootPath;
17
+
18
+	/**
19
+	 * @var bool
20
+	 */
21
+	private $useAutoloadDev;
22
+
23
+	/**
24
+	 * @var ClassNameMapper
25
+	 */
26
+	private $classNameMapper;
27
+
28
+	public function __construct(string $composerFile = null, string $rootPath = null, bool $useAutoloadDev = false)
29
+	{
30
+		$this->composerFile = $composerFile;
31
+		$this->useAutoloadDev = $useAutoloadDev;
32
+		if ($rootPath === null) {
33
+			$this->rootPath = dirname(__DIR__, 9);
34
+		} else {
35
+			$this->rootPath = $rootPath;
36
+		}
37
+	}
38
+
39
+	private function getClassNameMapper() : ClassNameMapper
40
+	{
41
+		if ($this->classNameMapper === null) {
42
+			$this->classNameMapper = ClassNameMapper::createFromComposerFile($this->composerFile, $this->rootPath, $this->useAutoloadDev);
43
+		}
44
+		return $this->classNameMapper;
45
+	}
46
+
47
+	/**
48
+	 * Returns the path of a class file given the fully qualified class name.
49
+	 *
50
+	 * @param string $className
51
+	 * @return \SplFileInfo
52
+	 * @throws NoPathFoundException
53
+	 */
54
+	public function getPath(string $className): \SplFileInfo
55
+	{
56
+		$paths = $this->getClassNameMapper()->getPossibleFileNames($className);
57
+		if (empty($paths)) {
58
+			throw NoPathFoundException::create($className);
59
+		}
60
+		return new \SplFileInfo($this->rootPath.'/'.$paths[0]);
61
+	}
62 62
 }
Please login to merge, or discard this patch.