Skip to content

Commit

Permalink
Log1x/v1.2.5 (#11)
Browse files Browse the repository at this point in the history
* enhance(console): Clean up CLI integration
* bugfix(views): Fix views not properly being converted to slugs
* bugfix(widget): Add missing widget ID to get_field() in the items method
* bugfix(widget): Prevent widget title from rendering on empty widgets
  • Loading branch information
Log1x authored Mar 22, 2020
1 parent 5ec524c commit ae5a500
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 155 deletions.
67 changes: 4 additions & 63 deletions src/Console/BlockMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace Log1x\AcfComposer\Console;

use Illuminate\Support\Str;
use Roots\Acorn\Console\Commands\GeneratorCommand;

class BlockMakeCommand extends GeneratorCommand
class BlockMakeCommand extends MakeCommand
{
/**
* The console command signature.
Expand All @@ -30,46 +27,11 @@ class BlockMakeCommand extends GeneratorCommand
protected $type = 'Block';

/**
* Execute the console command.
* The view stub used when generated.
*
* @return mixed
* @var string|bool
*/
public function handle()
{
parent::handle();

$view = Str::finish(str_replace('.', '/', Str::slug(head($this->argument('name')))), '.blade.php');
$path = $this->getPaths() . '/blocks/';

if (! $this->files->exists($path)) {
$this->files->makeDirectory($path);
}

if ($this->files->exists($path . $view)) {
return $this->error("File {$view} already exists!");
}

$this->files->put($path . $view, $this->files->get($this->getViewStub()));

return $this->info("File {$view} created.");
}

/**
* Return the applications view path.
*
* @param string $name
* @return void
*/
protected function getPaths()
{
$paths = $this->app['view.finder']->getPaths();

if (count($paths) === 1) {
return head($paths);
}

return $this->choice('Where do you want to create the view(s)?', $paths, head($paths));
}
protected $view = 'repeater';

/**
* Get the stub file for the generator.
Expand All @@ -84,25 +46,4 @@ protected function getStub()

return __DIR__ . '/stubs/block.stub';
}

/**
* Get the view stub file for the generator.
*
* @return string
*/
protected function getViewStub()
{
return __DIR__ . '/stubs/views/repeater.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\Blocks';
}
}
15 changes: 1 addition & 14 deletions src/Console/FieldMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Log1x\AcfComposer\Console;

use Roots\Acorn\Console\Commands\GeneratorCommand;

class FieldMakeCommand extends GeneratorCommand
class FieldMakeCommand extends MakeCommand
{
/**
* The console command signature.
Expand Down Expand Up @@ -36,15 +34,4 @@ protected function getStub()
{
return __DIR__ . '/stubs/field.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\Fields';
}
}
188 changes: 188 additions & 0 deletions src/Console/MakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

namespace Log1x\AcfComposer\Console;

use Exception;
use Illuminate\Support\Str;
use Roots\Acorn\Console\Commands\GeneratorCommand;

class MakeCommand extends GeneratorCommand
{
/**
* The view stub used when generated.
*
* @var string|bool
*/
protected $view = false;

/**
* The generated class path.
*
* @var string
*/
protected $path;

/**
* The category (type) of block.
*
* @var string|bool
*/
protected $category = false;

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->path = $this->getPath(
$this->qualifyClass($this->getNameInput())
);
$this->category = strtolower($this->category ?: $this->type);

$this->task("Generating {$this->type} class", function () {
if (
(! $this->hasOption('force') ||
! $this->option('force')) &&
$this->alreadyExists($this->getNameInput())
) {
throw new Exception('File `' . $this->shortenPath($this->path) . '` already exists.');
}

return parent::handle();
});

$this->task("Generating {$this->type} view", function () {
if ($this->view) {
if (! $this->files->exists($this->getViewPath())) {
$this->files->makeDirectory($this->getViewPath());
}

if ($this->files->exists($this->getView())) {
return;
}

$this->files->put($this->getView(), $this->files->get($this->getViewStub()));
}
});

return $this->summary();
}

/**
* Return the full view destination.
*
* @return string
*/
public function getView()
{
return Str::finish($this->getViewPath(), $this->getViewName());
}

/**
* Return the view destination filename.
*
* @return string
*/
public function getViewName()
{
return Str::finish(
str_replace('.', '/', Str::slug(Str::snake($this->getNameInput()))),
'.blade.php'
);
}

/**
* Return the view destination path.
*
* @return string
*/
public function getViewPath()
{
return $this->getPaths() . '/' . Str::slug(Str::plural($this->type)) . '/';
}

/**
* Get the view stub file for the generator.
*
* @return string
*/
protected function getViewStub()
{
return __DIR__ . "/stubs/views/{$this->view}.stub";
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\\' . Str::plural($this->type);
}

/**
* Return the applications view path.
*
* @param string $name
* @return void
*/
protected function getPaths()
{
$paths = $this->app['view.finder']->getPaths();

if (count($paths) === 1) {
return head($paths);
}

return $this->choice('Where do you want to create the view(s)?', $paths, head($paths));
}

/**
* Return the block creation summary.
*
* @return void
*/
protected function summary()
{
$this->line('');
$this->line("🎉 Your {$this->category} <fg=blue;options=bold>{$this->getNameInput()}</> has been composed.");
$this->line('');

$this->line('<fg=blue;options=bold>Class</>');
$this->line(" ⮑ <fg=blue>{$this->shortenPath($this->path)}</>");

if ($this->view) {
$this->line('');
$this->line('<fg=blue;options=bold>View</>');
$this->line(" ⮑ <fg=blue>{$this->shortenPath($this->getView(), 4)}</>");
}
}

/**
* Returns a shortened path.
*
* @param string $path
* @param int $i
* @return string
*/
protected function shortenPath($path, $i = 3)
{
return collect(
explode('/', $path)
)->slice(-$i, $i)->implode('/');
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
//
}
}
22 changes: 8 additions & 14 deletions src/Console/OptionsMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Log1x\AcfComposer\Console;

use Roots\Acorn\Console\Commands\GeneratorCommand;

class OptionsMakeCommand extends GeneratorCommand
class OptionsMakeCommand extends MakeCommand
{
/**
* The console command signature.
Expand All @@ -21,6 +19,13 @@ class OptionsMakeCommand extends GeneratorCommand
*/
protected $description = 'Create a new ACF options page.';

/**
* The label used when referencing the command type.
*
* @var string
*/
protected $label = 'Option Page';

/**
* The type of class being generated.
*
Expand All @@ -41,15 +46,4 @@ protected function getStub()

return __DIR__ . '/stubs/options.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\Fields';
}
}
Loading

0 comments on commit ae5a500

Please sign in to comment.