Skip to content

Commit

Permalink
Make a better hook skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
marktopper committed Aug 22, 2017
1 parent d8c60e7 commit 5aa45af
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/Commands/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function fire()
public function handle()
{
$name = $this->argument('name');
$name = kebab_case($name);

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

Expand Down
67 changes: 51 additions & 16 deletions src/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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}"));
}

/**
Expand Down Expand Up @@ -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'])) {
Expand Down
19 changes: 19 additions & 0 deletions stub/composer.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
}
28 changes: 28 additions & 0 deletions stub/src/StudlyCaseServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace StudlyCase;

use Illuminate\Support\ServiceProvider;

class StudlyCaseServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}

0 comments on commit 5aa45af

Please sign in to comment.