123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace Encore\Admin\Helpers\Scaffold;
- class ControllerCreator
- {
- /**
- * Controller full name.
- *
- * @var string
- */
- protected $name;
- /**
- * The filesystem instance.
- *
- * @var \Illuminate\Filesystem\Filesystem
- */
- protected $files;
- /**
- * ControllerCreator constructor.
- *
- * @param string $name
- * @param null $files
- */
- public function __construct($name, $files = null)
- {
- $this->name = $name;
- $this->files = $files ?: app('files');
- }
- /**
- * Create a controller.
- *
- * @param string $model
- *
- * @throws \Exception
- *
- * @return string
- */
- public function create($model)
- {
- $path = $this->getpath($this->name);
- if ($this->files->exists($path)) {
- throw new \Exception("Controller [$this->name] already exists!");
- }
- $stub = $this->files->get($this->getStub());
- $this->files->put($path, $this->replace($stub, $this->name, $model));
- return $path;
- }
- /**
- * @param string $stub
- * @param string $name
- * @param string $model
- *
- * @return string
- */
- protected function replace($stub, $name, $model)
- {
- $stub = $this->replaceClass($stub, $name);
- return str_replace(
- ['DummyModelNamespace', 'DummyModel'],
- [$model, class_basename($model)],
- $stub
- );
- }
- /**
- * Get controller namespace from giving name.
- *
- * @param string $name
- *
- * @return string
- */
- protected function getNamespace($name)
- {
- return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
- }
- /**
- * Replace the class name for the given stub.
- *
- * @param string $stub
- * @param string $name
- *
- * @return string
- */
- protected function replaceClass($stub, $name)
- {
- $class = str_replace($this->getNamespace($name).'\\', '', $name);
- return str_replace(['DummyClass', 'DummyNamespace'], [$class, $this->getNamespace($name)], $stub);
- }
- /**
- * Get file path from giving controller name.
- *
- * @param $name
- *
- * @return string
- */
- public function getPath($name)
- {
- $segments = explode('\\', $name);
- array_shift($segments);
- return app_path(implode('/', $segments)).'.php';
- }
- /**
- * Get stub file path.
- *
- * @return string
- */
- public function getStub()
- {
- return __DIR__.'/stubs/controller.stub';
- }
- }
|