Create New Item
Item Type
File
Folder
Item Name
Search file in folder and subfolders...
Are you sure want to rename?
File Manager
/
vendor
/
theanik
/
laravel-more-command
/
src
/
Commands
:
CreateServiceCommand.php
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
<?php namespace Theanik\LaravelMoreCommand\Commands; use Theanik\LaravelMoreCommand\Support\GenerateFile; use Theanik\LaravelMoreCommand\Support\FileGenerator; use Symfony\Component\Console\Input\InputArgument; use Illuminate\Support\Str; class CreateServiceCommand extends CommandGenerator { /** * argumentName * * @var string */ public $argumentName = 'service'; /** * Name and signature of Command. * name * @var string */ protected $name = 'make:service'; /** * command description. * description * @var string */ protected $description = 'New service create command'; /** * Get command arguments - EX : UserService * getArguments * * @return array */ protected function getArguments(): array { return [ ['service', InputArgument::REQUIRED, 'The name of the service class.'], ]; } /** * __construct * * @return void */ public function __construct() { parent::__construct(); } /** * Return Service name as convention * getServiceName * * @return string */ private function getServiceName(): string { $service = Str::studly($this->argument('service')); if (Str::contains(strtolower($service), 'service') === false) { $service .= 'Service'; } return $service; } /** * Replace App with empty string for resolve namespace * * @return string */ private function resolveNamespace(): string { if (strpos($this->getServiceNamespaceFromConfig(), self::APP_PATH) === 0) { return str_replace(self::APP_PATH, '', $this->getServiceNamespaceFromConfig()); } return '/' . $this->getServiceNamespaceFromConfig(); } /** * Return destination path for class file publish * getDestinationFilePath * * @return string */ protected function getDestinationFilePath(): string { return app_path() . $this->resolveNamespace() .'/Services'.'/'. $this->getServiceName() . '.php'; } /** * Return only service class name * getServiceNameWithoutNamespace * * @return string */ private function getServiceNameWithoutNamespace(): string { return class_basename($this->getServiceName()); } /** * Set Default Namespace * Override CommandGenerator class method * getDefaultNamespace * * @return string */ public function getDefaultNamespace() : string { $configNamespace = $this->getServiceNamespaceFromConfig(); return "$configNamespace\\Services"; } /** * Return stub file path * getStubFilePath * * @return string */ protected function getStubFilePath(): string { return '/stubs/service.stub'; } /** * Generate file content * getTemplateContents * * @return string */ protected function getTemplateContents(): string { return (new GenerateFile(__DIR__.$this->getStubFilePath(), [ 'CLASS_NAMESPACE' => $this->getClassNamespace(), 'CLASS' => $this->getServiceNameWithoutNamespace() ]))->render(); } /** * Execute the console command. * * @return int */ public function handle() { $path = str_replace('\\', '/', $this->getDestinationFilePath()); if (!$this->laravel['files']->isDirectory($dir = dirname($path))) { $this->laravel['files']->makeDirectory($dir, 0777, true); } $contents = $this->getTemplateContents(); try { (new FileGenerator($path, $contents))->generate(); $this->info("Created : {$path}"); } catch (\Exception $e) { $this->error("File : {$e->getMessage()}"); return E_ERROR; } return 0; } }