2
0

MichelPaperORMPackage.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace PhpDevCommunity\PaperORM\Michel\Package;
  3. use PhpDevCommunity\Michel\Package\PackageInterface;
  4. use PhpDevCommunity\PaperORM\Collector\EntityDirCollector;
  5. use PhpDevCommunity\PaperORM\Command\DatabaseCreateCommand;
  6. use PhpDevCommunity\PaperORM\Command\DatabaseDropCommand;
  7. use PhpDevCommunity\PaperORM\Command\DatabaseSyncCommand;
  8. use PhpDevCommunity\PaperORM\Command\Migration\MigrationDiffCommand;
  9. use PhpDevCommunity\PaperORM\Command\Migration\MigrationMigrateCommand;
  10. use PhpDevCommunity\PaperORM\Command\QueryExecuteCommand;
  11. use PhpDevCommunity\PaperORM\Command\ShowTablesCommand;
  12. use PhpDevCommunity\PaperORM\EntityManager;
  13. use PhpDevCommunity\PaperORM\EntityManagerInterface;
  14. use PhpDevCommunity\PaperORM\Migration\PaperMigration;
  15. use PhpDevCommunity\PaperORM\PaperConfiguration;
  16. use Psr\Container\ContainerInterface;
  17. use Psr\Log\LoggerInterface;
  18. class MichelPaperORMPackage implements PackageInterface
  19. {
  20. public function getDefinitions(): array
  21. {
  22. return [
  23. PaperConfiguration::class => static function (ContainerInterface $container) {
  24. $paperConf = PaperConfiguration::fromDsn(
  25. $container->get('paper.orm.dsn'),
  26. $container->get('paper.orm.debug')
  27. );
  28. $logger = $container->get('paper.orm.logger');
  29. if ($logger) {
  30. $paperConf->withLogger($container->get('paper.orm.logger'));
  31. }
  32. return $paperConf;
  33. },
  34. EntityDirCollector::class => static function (ContainerInterface $container) {
  35. return EntityDirCollector::bootstrap([$container->get('paper.orm.entity_dir')]);
  36. },
  37. EntityManagerInterface::class => static function (ContainerInterface $container) {
  38. return $container->get(EntityManager::class);
  39. },
  40. EntityManager::class => static function (ContainerInterface $container) {
  41. return EntityManager::createFromConfig($container->get(PaperConfiguration::class));
  42. },
  43. PaperMigration::class => static function (ContainerInterface $container) {
  44. return PaperMigration::create(
  45. $container->get(EntityManagerInterface::class),
  46. $container->get('paper.orm.migrations_table'),
  47. $container->get('paper.orm.migrations_dir')
  48. );
  49. },
  50. MigrationDiffCommand::class => static function (ContainerInterface $container) {
  51. return new MigrationDiffCommand($container->get(PaperMigration::class), $container->get(EntityDirCollector::class));
  52. },
  53. DatabaseDropCommand::class => static function (ContainerInterface $container) {
  54. return new DatabaseDropCommand($container->get(EntityManagerInterface::class), $container->get('michel.environment'));
  55. },
  56. DatabaseSyncCommand::class => static function (ContainerInterface $container) {
  57. return new DatabaseSyncCommand($container->get(PaperMigration::class), $container->get(EntityDirCollector::class), $container->get('michel.environment'));
  58. }
  59. ];
  60. }
  61. public function getParameters(): array
  62. {
  63. return [
  64. 'paper.orm.dsn' => getenv('DATABASE_URL') ?? '',
  65. 'paper.orm.debug' => static function (ContainerInterface $container) {
  66. return $container->get('michel.debug');
  67. },
  68. 'paper.orm.logger' => static function (ContainerInterface $container) {
  69. if ($container->has(LoggerInterface::class)) {
  70. return $container->get(LoggerInterface::class);
  71. }
  72. return null;
  73. },
  74. 'paper.orm.entity_dir' => getenv('PAPER_ORM_ENTITY_DIR') ?: static function (ContainerInterface $container) {
  75. $folder = $container->get('michel.project_dir') . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Entity';
  76. if (!is_dir($folder)) {
  77. mkdir($folder, 0777, true);
  78. }
  79. return $folder;
  80. },
  81. 'paper.orm.migrations_dir' => getenv('PAPER_ORM_MIGRATIONS_DIR') ?: static function (ContainerInterface $container) {
  82. $folder = $container->get('michel.project_dir') . DIRECTORY_SEPARATOR . 'migrations';
  83. if (!is_dir($folder)) {
  84. mkdir($folder, 0777, true);
  85. }
  86. return $folder;
  87. },
  88. 'paper.orm.migrations_table' => getenv('PAPER_ORM_MIGRATIONS_TABLE') ?: 'paper_mig_version',
  89. ];
  90. }
  91. public function getRoutes(): array
  92. {
  93. return [];
  94. }
  95. public function getControllerSources(): array
  96. {
  97. return [];
  98. }
  99. public function getListeners(): array
  100. {
  101. return [];
  102. }
  103. public function getCommandSources(): array
  104. {
  105. return [
  106. DatabaseCreateCommand::class,
  107. DatabaseDropCommand::class,
  108. DatabaseSyncCommand::class,
  109. MigrationDiffCommand::class,
  110. MigrationMigrateCommand::class,
  111. QueryExecuteCommand::class,
  112. ShowTablesCommand::class,
  113. ];
  114. }
  115. }