RepositoryInterfaceGenerator.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Console\Commands;
  3. use Prettus\Repository\Generators\Generator;
  4. use Prettus\Repository\Generators\Migrations\SchemaParser;
  5. /**
  6. * Class RepositoryInterfaceGenerator
  7. * @package Prettus\Repository\Generators
  8. * @author Anderson Andrade <contato@andersonandra.de>
  9. */
  10. class RepositoryInterfaceGenerator extends Generator
  11. {
  12. /**
  13. * Get stub name.
  14. *
  15. * @var string
  16. */
  17. protected $stub = 'repository/interface';
  18. /**
  19. * Get root namespace.
  20. *
  21. * @return string
  22. */
  23. public function getRootNamespace()
  24. {
  25. return parent::getRootNamespace() . parent::getConfigGeneratorClassPath($this->getPathConfigNode());
  26. }
  27. /**
  28. * Get generator path config node.
  29. *
  30. * @return string
  31. */
  32. public function getPathConfigNode()
  33. {
  34. return 'interfaces';
  35. }
  36. /**
  37. * Get destination path for generated file.
  38. *
  39. * @return string
  40. */
  41. public function getPath()
  42. {
  43. return $this->getBasePath() . '/' . parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . '/' . $this->getName() . 'Repository.php';
  44. }
  45. /**
  46. * Get base path of destination file.
  47. *
  48. * @return string
  49. */
  50. public function getBasePath()
  51. {
  52. return config('repository.generator.basePath', app()->path());
  53. }
  54. /**
  55. * Get array replacements.
  56. *
  57. * @return array
  58. */
  59. public function getReplacements()
  60. {
  61. return array_merge(parent::getReplacements(), [
  62. 'fillable' => $this->getFillable()
  63. ]);
  64. }
  65. /**
  66. * Get the fillable attributes.
  67. *
  68. * @return string
  69. */
  70. public function getFillable()
  71. {
  72. if (!$this->fillable) {
  73. return '[]';
  74. }
  75. $results = '[' . PHP_EOL;
  76. foreach ($this->getSchemaParser()->toArray() as $column => $value) {
  77. $results .= "\t\t'{$column}'," . PHP_EOL;
  78. }
  79. return $results . "\t" . ']';
  80. }
  81. /**
  82. * Get schema parser.
  83. *
  84. * @return SchemaParser
  85. */
  86. public function getSchemaParser()
  87. {
  88. return new SchemaParser($this->fillable);
  89. }
  90. }