BindingsGenerator.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Support\Facades\File;
  4. use Prettus\Repository\Generators\Generator;
  5. /**
  6. * Class BindingsGenerator
  7. * @package Prettus\Repository\Generators
  8. * @author Anderson Andrade <contato@andersonandra.de>
  9. */
  10. class BindingsGenerator extends Generator
  11. {
  12. /**
  13. * The placeholder for repository bindings
  14. *
  15. * @var string
  16. */
  17. public $bindPlaceholder = '//:end-bindings:';
  18. /**
  19. * Get stub name.
  20. *
  21. * @var string
  22. */
  23. protected $stub = 'bindings/bindings';
  24. public function run()
  25. {
  26. // Add entity repository binding to the repository service provider
  27. $provider = File::get($this->getPath());
  28. $repositoryInterface = '\\' . $this->getRepository() . "::class";
  29. $repositoryEloquent = '\\' . $this->getEloquentRepository() . "::class";
  30. File::put($this->getPath(), str_replace($this->bindPlaceholder, "\$this->app->bind({$repositoryInterface}, $repositoryEloquent);" . PHP_EOL . ' ' . $this->bindPlaceholder, $provider));
  31. }
  32. /**
  33. * Get destination path for generated file.
  34. *
  35. * @return string
  36. */
  37. public function getPath()
  38. {
  39. return $this->getBasePath() . '/Providers/' . parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . '.php';
  40. }
  41. /**
  42. * Get base path of destination file.
  43. *
  44. * @return string
  45. */
  46. public function getBasePath()
  47. {
  48. return config('repository.generator.basePath', app()->path());
  49. }
  50. /**
  51. * Get generator path config node.
  52. *
  53. * @return string
  54. */
  55. public function getPathConfigNode()
  56. {
  57. return 'provider';
  58. }
  59. /**
  60. * Gets repository full class name
  61. *
  62. * @return string
  63. */
  64. public function getRepository()
  65. {
  66. $repositoryGenerator = new RepositoryInterfaceGenerator([
  67. 'name' => $this->name,
  68. ]);
  69. $repository = $repositoryGenerator->getRootNamespace() . '\\' . $repositoryGenerator->getName();
  70. return str_replace([
  71. "\\",
  72. '/'
  73. ], '\\', $repository) . 'Repository';
  74. }
  75. /**
  76. * Gets eloquent repository full class name
  77. *
  78. * @return string
  79. */
  80. public function getEloquentRepository()
  81. {
  82. $repositoryGenerator = new RepositoryEloquentGenerator([
  83. 'name' => $this->name,
  84. ]);
  85. $repository = $repositoryGenerator->getRootNamespace() . '\\' . $repositoryGenerator->getName();
  86. return str_replace([
  87. "\\",
  88. '/'
  89. ], '\\', $repository) . 'RepositoryEloquent';
  90. }
  91. /**
  92. * Get root namespace.
  93. *
  94. * @return string
  95. */
  96. public function getRootNamespace()
  97. {
  98. return parent::getRootNamespace() . parent::getConfigGeneratorClassPath($this->getPathConfigNode());
  99. }
  100. /**
  101. * Get array replacements.
  102. *
  103. * @return array
  104. */
  105. public function getReplacements()
  106. {
  107. return array_merge(parent::getReplacements(), [
  108. 'repository' => $this->getRepository(),
  109. 'eloquent' => $this->getEloquentRepository(),
  110. 'placeholder' => $this->bindPlaceholder,
  111. ]);
  112. }
  113. }