RepositoryEloquentGenerator.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Console\Commands;
  3. use Prettus\Repository\Generators\Generator;
  4. use Prettus\Repository\Generators\Migrations\SchemaParser;
  5. /**
  6. * Class RepositoryEloquentGenerator
  7. * @package Prettus\Repository\Generators
  8. * @author Anderson Andrade <contato@andersonandra.de>
  9. */
  10. class RepositoryEloquentGenerator extends Generator
  11. {
  12. /**
  13. * Get stub name.
  14. *
  15. * @var string
  16. */
  17. protected $stub = 'repository/eloquent';
  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 'repositories';
  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() . 'RepositoryEloquent.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. $repository = parent::getRootNamespace() . parent::getConfigGeneratorClassPath('interfaces') . '\\' . $this->name . 'Repository;';
  62. $repository = str_replace([
  63. "\\",
  64. '/'
  65. ], '\\', $repository);
  66. return array_merge(parent::getReplacements(), [
  67. 'fillable' => $this->getFillable(),
  68. 'use_validator' => $this->getValidatorUse(),
  69. 'validator' => $this->getValidatorMethod(),
  70. 'repository' => $repository,
  71. 'model' => isset($this->options['model']) ? $this->options['model'] : ''
  72. ]);
  73. }
  74. /**
  75. * Get the fillable attributes.
  76. *
  77. * @return string
  78. */
  79. public function getFillable()
  80. {
  81. if (!$this->fillable) {
  82. return '[]';
  83. }
  84. $results = '[' . PHP_EOL;
  85. foreach ($this->getSchemaParser()->toArray() as $column => $value) {
  86. $results .= "\t\t'{$column}'," . PHP_EOL;
  87. }
  88. return $results . "\t" . ']';
  89. }
  90. /**
  91. * Get schema parser.
  92. *
  93. * @return SchemaParser
  94. */
  95. public function getSchemaParser()
  96. {
  97. return new SchemaParser($this->fillable);
  98. }
  99. public function getValidatorUse()
  100. {
  101. $validator = $this->getValidator();
  102. return "use {$validator};";
  103. }
  104. public function getValidator()
  105. {
  106. $validatorGenerator = new ValidatorGenerator([
  107. 'name' => $this->name,
  108. 'rules' => $this->rules,
  109. 'force' => $this->force,
  110. ]);
  111. $validator = $validatorGenerator->getRootNamespace() . '\\' . $validatorGenerator->getName();
  112. return str_replace([
  113. "\\",
  114. '/'
  115. ], '\\', $validator) . 'Validator';
  116. }
  117. public function getValidatorMethod()
  118. {
  119. if ($this->validator != 'yes') {
  120. return '';
  121. }
  122. $class = $this->getClass();
  123. return '/**' . PHP_EOL . ' * Specify Validator class name' . PHP_EOL . ' *' . PHP_EOL . ' * @return mixed' . PHP_EOL . ' */' . PHP_EOL . ' public function validator()' . PHP_EOL . ' {' . PHP_EOL . PHP_EOL . ' return ' . $class . 'Validator::class;' . PHP_EOL . ' }' . PHP_EOL;
  124. }
  125. }