Skip to content

Commit

Permalink
Helper command
Browse files Browse the repository at this point in the history
  • Loading branch information
EsdertCO committed Sep 10, 2020
1 parent 051b997 commit f37a625
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Config/laravel-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
'facade' => [
'namespace' => '\App\Facades'
],
/**
* make:helper
*
* Allows you to change the location of the helpers.
*/
'helper' => [
'folder' => 'Helpers'
],
/**
* make:interface
*
Expand Down
101 changes: 101 additions & 0 deletions src/Console/Make/MakeHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Creativeorange\LaravelStubs\Console\Make;

use Creativeorange\LaravelStubs\Console\CustomGeneratorCommand;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;

class MakeHelper extends CustomGeneratorCommand
{

/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'make:helper';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new helper';

protected $signature = 'make:helper
{name : The name of the helper}';

protected $type = 'Helper';

protected function getStub()
{
return $this->resolveStubPath('/../stubs/helper.stub');
}

protected function replaceNamespace(&$stub, $name)
{
$this->parseFunctionName($stub);

return parent::replaceNamespace($stub, $name);
}

protected function parseFunctionName(&$stub)
{
$functionName = $this->getNameInput();
$functionName = \preg_replace('/helper$/i', '', $functionName);
$functionName = Str::snake($functionName);

$stub = \str_replace('DummyFunction', $functionName, $stub);
}

public function handle()
{
$name = $this->getNameInput();

$path = $this->getPath($name);

// First we will check to see if the class already exists. If it does, we don't want
// to create the class and overwrite the user's code. So, we will bail out so the
// code is untouched. Otherwise, we will continue generating this class' files.
if ((! $this->hasOption('force') ||
! $this->option('force')) &&
$this->alreadyExists($this->getNameInput())) {
$this->error($this->type.' already exists!');

return false;
}

// Next, we will generate the path to the location where this class' file should get
// written. Then, we will build the class and make the proper replacements on the
// stub files so that it gets the correctly formatted namespace and class name.
$this->makeDirectory($path);

$this->files->put($path, $this->sortImports($this->buildClass($name)));

$this->info($this->type.' created successfully.');
}

protected function getPath($name)
{
return $this->laravel['path'].'/'.config('laravel-stubs.make.helper.folder').'/'.str_replace('\\', '/', $name).'.php';
}

protected function getNameInput()
{
$name = trim($this->argument('name'));

$name = Str::endsWith($name, 'Helper')
? $name
: $name . 'Helper';

return $name;
}

protected function getArguments()
{
return [
['name', InputArgument::REQUIRED, 'The name of the helper']
];
}
}
1 change: 1 addition & 0 deletions src/LaravelStubsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function boot()
Console\Make\MakeInterface::class,
Console\Make\MakeViewComposer::class,
Console\Make\MakeFacade::class,
Console\Make\MakeHelper::class,
/** Patch */
Console\Patch::class,
/** Publish */
Expand Down
15 changes: 15 additions & 0 deletions src/stubs/helper.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

if (!function_exists('DummyFunction')) {

/**
* description
*
* @param
* @return
*/
function DummyFunction()
{

}
}

0 comments on commit f37a625

Please sign in to comment.