123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Support\Facades\File;
- use Prettus\Repository\Generators\Generator;
- /**
- * Class BindingsGenerator
- * @package Prettus\Repository\Generators
- * @author Anderson Andrade <contato@andersonandra.de>
- */
- class BindingsGenerator extends Generator
- {
- /**
- * The placeholder for repository bindings
- *
- * @var string
- */
- public $bindPlaceholder = '//:end-bindings:';
- /**
- * Get stub name.
- *
- * @var string
- */
- protected $stub = 'bindings/bindings';
- public function run()
- {
- // Add entity repository binding to the repository service provider
- $provider = File::get($this->getPath());
- $repositoryInterface = '\\' . $this->getRepository() . "::class";
- $repositoryEloquent = '\\' . $this->getEloquentRepository() . "::class";
- File::put($this->getPath(), str_replace($this->bindPlaceholder, "\$this->app->bind({$repositoryInterface}, $repositoryEloquent);" . PHP_EOL . ' ' . $this->bindPlaceholder, $provider));
- }
- /**
- * Get destination path for generated file.
- *
- * @return string
- */
- public function getPath()
- {
- return $this->getBasePath() . '/Providers/' . parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . '.php';
- }
- /**
- * Get base path of destination file.
- *
- * @return string
- */
- public function getBasePath()
- {
- return config('repository.generator.basePath', app()->path());
- }
- /**
- * Get generator path config node.
- *
- * @return string
- */
- public function getPathConfigNode()
- {
- return 'provider';
- }
- /**
- * Gets repository full class name
- *
- * @return string
- */
- public function getRepository()
- {
- $repositoryGenerator = new RepositoryInterfaceGenerator([
- 'name' => $this->name,
- ]);
- $repository = $repositoryGenerator->getRootNamespace() . '\\' . $repositoryGenerator->getName();
- return str_replace([
- "\\",
- '/'
- ], '\\', $repository) . 'Repository';
- }
- /**
- * Gets eloquent repository full class name
- *
- * @return string
- */
- public function getEloquentRepository()
- {
- $repositoryGenerator = new RepositoryEloquentGenerator([
- 'name' => $this->name,
- ]);
- $repository = $repositoryGenerator->getRootNamespace() . '\\' . $repositoryGenerator->getName();
- return str_replace([
- "\\",
- '/'
- ], '\\', $repository) . 'RepositoryEloquent';
- }
- /**
- * Get root namespace.
- *
- * @return string
- */
- public function getRootNamespace()
- {
- return parent::getRootNamespace() . parent::getConfigGeneratorClassPath($this->getPathConfigNode());
- }
- /**
- * Get array replacements.
- *
- * @return array
- */
- public function getReplacements()
- {
- return array_merge(parent::getReplacements(), [
- 'repository' => $this->getRepository(),
- 'eloquent' => $this->getEloquentRepository(),
- 'placeholder' => $this->bindPlaceholder,
- ]);
- }
- }
|