MichelPaperORMPackage.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace PhpDevCommunity\PaperORM\Michel\Package;
  3. use LogicException;
  4. use PhpDevCommunity\Michel\Package\PackageInterface;
  5. use PhpDevCommunity\PaperORM\Command\DatabaseCreateCommand;
  6. use PhpDevCommunity\PaperORM\Command\DatabaseDropCommand;
  7. use PhpDevCommunity\PaperORM\Command\MigrationDiffCommand;
  8. use PhpDevCommunity\PaperORM\Command\MigrationMigrateCommand;
  9. use PhpDevCommunity\PaperORM\Command\QueryExecuteCommand;
  10. use PhpDevCommunity\PaperORM\Command\ShowTablesCommand;
  11. use PhpDevCommunity\PaperORM\EntityManager;
  12. use PhpDevCommunity\PaperORM\EntityManagerInterface;
  13. use PhpDevCommunity\PaperORM\Migration\PaperMigration;
  14. use PhpDevCommunity\PaperORM\Parser\DSNParser;
  15. use Psr\Container\ContainerInterface;
  16. class MichelPaperORMPackage implements PackageInterface
  17. {
  18. public function getDefinitions(): array
  19. {
  20. return [
  21. EntityManagerInterface::class => static function (ContainerInterface $container) {
  22. return $container->get(EntityManager::class);
  23. },
  24. EntityManager::class => static function (ContainerInterface $container) {
  25. $dsn = $container->get('database.dsn');
  26. if (!is_string($dsn) || empty($dsn)) {
  27. throw new LogicException('Database DSN not found, please set DATABASE_DSN in .env file or database.dsn in config');
  28. }
  29. $params = DSNParser::parse($container->get('database.dsn'));
  30. return new EntityManager($params);
  31. },
  32. PaperMigration::class => static function (ContainerInterface $container) {
  33. return PaperMigration::create(
  34. $container->get(EntityManagerInterface::class),
  35. $container->get('paper.migration.table'),
  36. $container->get('paper.migration.dir')
  37. );
  38. },
  39. MigrationDiffCommand::class => static function (ContainerInterface $container) {
  40. return new MigrationDiffCommand($container->get(PaperMigration::class), $container->get('paper.entity.dir'));
  41. },
  42. DatabaseDropCommand::class => static function (ContainerInterface $container) {
  43. return new DatabaseDropCommand($container->get(EntityManagerInterface::class), $container->get('michel.environment'));
  44. }
  45. ];
  46. }
  47. public function getParameters(): array
  48. {
  49. return [
  50. 'database.dsn' => getenv('DATABASE_DSN') ?? '',
  51. 'paper.migration.dir' => getenv('PAPER_MIGRATION_DIR') ?: function (ContainerInterface $container) {
  52. $folder = $container->get('michel.project_dir') . DIRECTORY_SEPARATOR . 'migrations';
  53. if (!is_dir($folder)) {
  54. mkdir($folder, 0777, true);
  55. }
  56. return $folder;
  57. },
  58. 'paper.migration.table' => getenv('PAPER_MIGRATION_TABLE') ?: 'mig_versions',
  59. 'paper.entity.dir' => getenv('PAPER_ENTITY_DIR') ?: function (ContainerInterface $container) {
  60. $folder = $container->get('michel.project_dir') . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Entity';
  61. if (!is_dir($folder)) {
  62. mkdir($folder, 0777, true);
  63. }
  64. return $folder;
  65. },
  66. ];
  67. }
  68. public function getRoutes(): array
  69. {
  70. return [];
  71. }
  72. public function getListeners(): array
  73. {
  74. return [];
  75. }
  76. public function getCommands(): array
  77. {
  78. return [
  79. DatabaseCreateCommand::class,
  80. DatabaseDropCommand::class,
  81. MigrationDiffCommand::class,
  82. MigrationMigrateCommand::class,
  83. QueryExecuteCommand::class,
  84. ShowTablesCommand::class,
  85. ];
  86. }
  87. }