Skip to content

Commit 5aa45af

Browse files
committed
Make a better hook skeleton
1 parent d8c60e7 commit 5aa45af

File tree

4 files changed

+99
-16
lines changed

4 files changed

+99
-16
lines changed

src/Commands/MakeCommand.php

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function fire()
2828
public function handle()
2929
{
3030
$name = $this->argument('name');
31+
$name = kebab_case($name);
3132

3233
$this->hooks->make($name);
3334

src/Hooks.php

+51-16
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@ public function disable($name)
430430
*/
431431
public function make($name)
432432
{
433+
$studlyCase = studly_case($name);
434+
433435
// Check if already exists
434436
if ($this->downloaded($name)) {
435437
throw new Exceptions\HookAlreadyExistsException("Hook [{$name}] already exists.");
@@ -443,26 +445,51 @@ public function make($name)
443445
}
444446

445447
// Create folder for the new hook
448+
$this->filesystem->deleteDirectory(base_path("hooks/{$name}"));
446449
$this->filesystem->makeDirectory(base_path("hooks/{$name}"));
447450

448451
// make stub files
449-
/*
450-
$this->filesystem->put(
451-
base_path("hooks/{$name}/hook.json"),
452-
json_encode($data)
453-
);
454-
*/
452+
$this->makeStubFiles($name);
455453

456-
// Make composer.json
457-
$composer = [
458-
'name' => $name,
454+
event(new Events\MadeHook($name));
455+
}
456+
457+
protected function makeStubFiles($name)
458+
{
459+
$replaces = [
460+
'kebab-case' => $name,
461+
'snake_case' => snake_case($name),
462+
'camcelCase' => camel_case($name),
463+
'StudlyCase' => studly_case($name),
459464
];
460-
$this->filesystem->put(
461-
base_path("hooks/{$name}/composer.json"),
462-
json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
463-
);
464465

465-
event(new Events\MadeHook($name));
466+
$files = $this->filesystem->allFiles(__DIR__.'/../stub');
467+
468+
foreach ($files as $file) {
469+
if ($path = $file->getRelativePath()) {
470+
$parts = explode('/', $path);
471+
472+
$location = base_path("hooks/{$name}");
473+
474+
foreach ($parts as $part) {
475+
$location .= "/{$part}";
476+
477+
if (!$this->filesystem->isDirectory($location)) {
478+
$this->filesystem->makeDirectory($location);
479+
}
480+
}
481+
}
482+
483+
$content = $this->replace($this->filesystem->get($file->getRealPath()), $replaces);
484+
$filename = $this->replace($file->getRelativePathname(), $replaces);
485+
486+
$this->filesystem->put(base_path("hooks/{$name}/{$filename}"), $content);
487+
}
488+
}
489+
490+
protected function replace($content, array $replaces)
491+
{
492+
return str_replace(array_keys($replaces), array_values($replaces), $content);
466493
}
467494

468495
/**
@@ -522,8 +549,12 @@ public function local($name)
522549
*/
523550
public function downloaded($name)
524551
{
525-
return $this->filesystem->isDirectory(base_path("hooks/{$name}"))
526-
|| $this->filesystem->isDirectory(base_path("vendor/{$name}"));
552+
if ($this->local($name)) {
553+
return $this->filesystem->isDirectory(base_path("hooks/{$name}"))
554+
&& $this->filesystem->exists(base_path("hooks/{$name}/composer.json"));
555+
}
556+
557+
return $this->filesystem->isDirectory(base_path("vendor/{$name}"));
527558
}
528559

529560
/**
@@ -726,6 +757,10 @@ public function readLocalHooks()
726757
$hooks = [];
727758
$directories = array_except($this->filesystem->directories(base_path('hooks')), ['.', '..']);
728759
foreach ($directories as $directory) {
760+
if (!$this->filesystem->exists($directory.'/composer.json')) {
761+
continue;
762+
}
763+
729764
$composer = json_decode($this->filesystem->get($directory.'/composer.json'), true);
730765

731766
if (!is_null($composer) && isset($composer['name'])) {

stub/composer.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "kebab-case",
3+
"description": "This is my first hook.",
4+
"require": {
5+
"larapack/hooks": "~1.0"
6+
},
7+
"autoload": {
8+
"psr-4": {
9+
"StudlyCase\\": "src/"
10+
}
11+
},
12+
"extra": {
13+
"hook": {
14+
"providers": [
15+
"StudlyCase\\StudlyCaseServiceProvider"
16+
]
17+
}
18+
}
19+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace StudlyCase;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class StudlyCaseServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap any application services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Register any application services.
21+
*
22+
* @return void
23+
*/
24+
public function register()
25+
{
26+
//
27+
}
28+
}

0 commit comments

Comments
 (0)