janisbiz /
light-orm
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | use Janisbiz\LightOrm\ConnectionPool; |
||
| 4 | use Janisbiz\LightOrm\Generator; |
||
|
0 ignored issues
–
show
|
|||
| 5 | use Janisbiz\LightOrm\Dms\MySQL\Connection\ConnectionConfigUrl as MySQLConnectionConfigUrl; |
||
| 6 | use Janisbiz\LightOrm\Dms\MySQL\Generator\DmsFactory; |
||
| 7 | use Janisbiz\LightOrm\Dms\MySQL\Generator\Writer\BaseEntityClassWriter; |
||
| 8 | use Janisbiz\LightOrm\Dms\MySQL\Generator\Writer\EntityClassWriter; |
||
| 9 | use Janisbiz\LightOrm\Dms\MySQL\Generator\Writer\RepositoryClassWriter; |
||
| 10 | use Janisbiz\LightOrm\Dms\MySQL\Generator\Writer\WriterConfig; |
||
| 11 | |||
| 12 | include_once __DIR__ . '/../vendor/autoload.php'; |
||
| 13 | |||
| 14 | $databaseName = 'light_orm_mysql'; |
||
| 15 | $connectionPool = (new ConnectionPool()) |
||
| 16 | ->addConnectionConfig(new MySQLConnectionConfigUrl(\sprintf('mysql://root:password@mysql/%s', $databaseName))) |
||
| 17 | ; |
||
| 18 | |||
| 19 | $directoryPersistent = \implode( |
||
| 20 | '', |
||
| 21 | [ |
||
| 22 | __DIR__, |
||
| 23 | DIRECTORY_SEPARATOR, |
||
| 24 | '..', |
||
| 25 | DIRECTORY_SEPARATOR, |
||
| 26 | 'tests', |
||
| 27 | DIRECTORY_SEPARATOR, |
||
| 28 | 'Behat', |
||
| 29 | DIRECTORY_SEPARATOR, |
||
| 30 | 'Bootstrap', |
||
| 31 | DIRECTORY_SEPARATOR, |
||
| 32 | 'Generated' |
||
| 33 | ] |
||
| 34 | ); |
||
| 35 | $directoryNonPersistent = \implode( |
||
| 36 | '', |
||
| 37 | [ |
||
| 38 | __DIR__, |
||
| 39 | DIRECTORY_SEPARATOR, |
||
| 40 | '..', |
||
| 41 | DIRECTORY_SEPARATOR, |
||
| 42 | 'var', |
||
| 43 | DIRECTORY_SEPARATOR, |
||
| 44 | 'light-orm', |
||
| 45 | DIRECTORY_SEPARATOR, |
||
| 46 | 'Generated' |
||
| 47 | ] |
||
| 48 | ); |
||
| 49 | |||
| 50 | $namespacePersistent = 'Janisbiz\LightOrm\Tests\Behat\Generated'; |
||
| 51 | $namespaceNonPersistent = 'Janisbiz\LightOrm\Variable\Generated'; |
||
| 52 | |||
| 53 | $baseEntityClassWriter = new BaseEntityClassWriter(new WriterConfig( |
||
| 54 | $directoryNonPersistent, |
||
| 55 | $namespaceNonPersistent, |
||
| 56 | 'Base' |
||
| 57 | )); |
||
| 58 | $entityClassWriter = new EntityClassWriter( |
||
| 59 | new WriterConfig( |
||
| 60 | $directoryPersistent, |
||
| 61 | $namespacePersistent, |
||
| 62 | '', |
||
| 63 | 'Entity' |
||
| 64 | ), |
||
| 65 | $baseEntityClassWriter |
||
| 66 | ); |
||
| 67 | $repositoryClassWriter = new RepositoryClassWriter( |
||
| 68 | new WriterConfig( |
||
| 69 | $directoryPersistent, |
||
| 70 | $namespacePersistent, |
||
| 71 | '', |
||
| 72 | 'Repository' |
||
| 73 | ), |
||
| 74 | $entityClassWriter |
||
| 75 | ); |
||
| 76 | |||
| 77 | (new Generator(new DmsFactory())) |
||
| 78 | ->addWriter($baseEntityClassWriter) |
||
| 79 | ->addWriter($entityClassWriter) |
||
| 80 | ->addWriter($repositoryClassWriter) |
||
| 81 | ->generate($connectionPool->getConnection($databaseName), $databaseName) |
||
| 82 | ; |
||
| 83 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: