From 5aa45afe444d2a0d5c176adaef7cbdbd9e30a0ba Mon Sep 17 00:00:00 2001 From: Mark Topper Date: Tue, 22 Aug 2017 18:21:55 +0200 Subject: [PATCH] Make a better hook skeleton --- src/Commands/MakeCommand.php | 1 + src/Hooks.php | 67 ++++++++++++++++++++------ stub/composer.json | 19 ++++++++ stub/src/StudlyCaseServiceProvider.php | 28 +++++++++++ 4 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 stub/composer.json create mode 100644 stub/src/StudlyCaseServiceProvider.php diff --git a/src/Commands/MakeCommand.php b/src/Commands/MakeCommand.php index d927e7a..f45d420 100644 --- a/src/Commands/MakeCommand.php +++ b/src/Commands/MakeCommand.php @@ -28,6 +28,7 @@ public function fire() public function handle() { $name = $this->argument('name'); + $name = kebab_case($name); $this->hooks->make($name); diff --git a/src/Hooks.php b/src/Hooks.php index e80faf4..1004d6f 100644 --- a/src/Hooks.php +++ b/src/Hooks.php @@ -430,6 +430,8 @@ public function disable($name) */ public function make($name) { + $studlyCase = studly_case($name); + // Check if already exists if ($this->downloaded($name)) { throw new Exceptions\HookAlreadyExistsException("Hook [{$name}] already exists."); @@ -443,26 +445,51 @@ public function make($name) } // Create folder for the new hook + $this->filesystem->deleteDirectory(base_path("hooks/{$name}")); $this->filesystem->makeDirectory(base_path("hooks/{$name}")); // make stub files - /* - $this->filesystem->put( - base_path("hooks/{$name}/hook.json"), - json_encode($data) - ); - */ + $this->makeStubFiles($name); - // Make composer.json - $composer = [ - 'name' => $name, + event(new Events\MadeHook($name)); + } + + protected function makeStubFiles($name) + { + $replaces = [ + 'kebab-case' => $name, + 'snake_case' => snake_case($name), + 'camcelCase' => camel_case($name), + 'StudlyCase' => studly_case($name), ]; - $this->filesystem->put( - base_path("hooks/{$name}/composer.json"), - json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) - ); - event(new Events\MadeHook($name)); + $files = $this->filesystem->allFiles(__DIR__.'/../stub'); + + foreach ($files as $file) { + if ($path = $file->getRelativePath()) { + $parts = explode('/', $path); + + $location = base_path("hooks/{$name}"); + + foreach ($parts as $part) { + $location .= "/{$part}"; + + if (!$this->filesystem->isDirectory($location)) { + $this->filesystem->makeDirectory($location); + } + } + } + + $content = $this->replace($this->filesystem->get($file->getRealPath()), $replaces); + $filename = $this->replace($file->getRelativePathname(), $replaces); + + $this->filesystem->put(base_path("hooks/{$name}/{$filename}"), $content); + } + } + + protected function replace($content, array $replaces) + { + return str_replace(array_keys($replaces), array_values($replaces), $content); } /** @@ -522,8 +549,12 @@ public function local($name) */ public function downloaded($name) { - return $this->filesystem->isDirectory(base_path("hooks/{$name}")) - || $this->filesystem->isDirectory(base_path("vendor/{$name}")); + if ($this->local($name)) { + return $this->filesystem->isDirectory(base_path("hooks/{$name}")) + && $this->filesystem->exists(base_path("hooks/{$name}/composer.json")); + } + + return $this->filesystem->isDirectory(base_path("vendor/{$name}")); } /** @@ -726,6 +757,10 @@ public function readLocalHooks() $hooks = []; $directories = array_except($this->filesystem->directories(base_path('hooks')), ['.', '..']); foreach ($directories as $directory) { + if (!$this->filesystem->exists($directory.'/composer.json')) { + continue; + } + $composer = json_decode($this->filesystem->get($directory.'/composer.json'), true); if (!is_null($composer) && isset($composer['name'])) { diff --git a/stub/composer.json b/stub/composer.json new file mode 100644 index 0000000..5b836e0 --- /dev/null +++ b/stub/composer.json @@ -0,0 +1,19 @@ +{ + "name": "kebab-case", + "description": "This is my first hook.", + "require": { + "larapack/hooks": "~1.0" + }, + "autoload": { + "psr-4": { + "StudlyCase\\": "src/" + } + }, + "extra": { + "hook": { + "providers": [ + "StudlyCase\\StudlyCaseServiceProvider" + ] + } + } +} \ No newline at end of file diff --git a/stub/src/StudlyCaseServiceProvider.php b/stub/src/StudlyCaseServiceProvider.php new file mode 100644 index 0000000..a0616c0 --- /dev/null +++ b/stub/src/StudlyCaseServiceProvider.php @@ -0,0 +1,28 @@ +