Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp-cli commands #16

Merged
merged 3 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/Acorn/Bootstrap/WPCLI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Roots\Acorn\Bootstrap;

use Roots\Acorn\Application;

class WPCLI
{
protected $app;

public function bootstrap(Application $app)
{
$this->app = $app;
if ($app->runningInConsole() && \defined('WP_CLI') && WP_CLI) {
$this->registerCommands();
}
}

protected function registerCommands()
{
\WP_CLI::add_command(
'acorn view:cache',
$this->app->make(\Roots\Acorn\Console\ViewCacheCommand::class)
);

\WP_CLI::add_command(
'acorn view:clear',
$this->app->make(\Roots\Acorn\Console\ViewClearCommand::class)
);

\WP_CLI::add_command(
'acorn vendor:publish',
$this->app->make(\Roots\Acorn\Console\VendorPublishCommand::class)
);

\WP_CLI::add_command(
'acorn make:provider',
$this->app->make(\Roots\Acorn\Console\ProviderMakeCommand::class)
);

\WP_CLI::add_command(
'acorn make:composer',
$this->app->make(\Roots\Acorn\Console\ComposerMakeCommand::class)
);
}
}
188 changes: 188 additions & 0 deletions src/Acorn/Console/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

namespace Roots\Acorn\Console;

use function Roots\value;
use Illuminate\Support\Traits\Macroable;
use Roots\Acorn\Application;
use Roots\Acorn\Filesystem\Filesystem;

abstract class Command
{
use Macroable;

/** @var \Acorn\Application The Laravel application instance. */
protected $app;

/** @var \Illuminate\Filesystem\Filesystem The filesystem instance. */
protected $files;

/**
* Create a new command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Acorn\Application $app
* @return void
*/
public function __construct(Filesystem $files, Application $app)
{
$this->files = $files;
$this->app = $app;
}

/**
* Parse associated args from WP-CLI
*
* @param array $assoc_args
*/
public function parse(array $assoc_args)
{
foreach ($assoc_args as $key => $value) {
if (! property_exists($this, $key)) {
continue;
}
$this->{$key} = is_array($this->{$key})
? explode(',', $value)
: $value;
}
}

/**
* Entry point for WP-CLI.
*
* Consider using PHPDoc to annotate this method when implemented. The
* annotations should describe the command and provide examples.
*
* @link https://make.wordpress.org/cli/handbook/commands-cookbook/#annotating-with-phpdoc
*
* @param array $args
* @param array $assoc_args
*/
abstract public function __invoke($args, $assoc_args);

/**
* Run a WP-CLI Acorn command
*
* @param string $command WP-CLI Acorn command to run, including arguments.
* @param array $options Configuration options for command execution
*
* @return mixed
*/
protected function call($command, $options = [])
{
return \WP_CLI::runcommand("acorn {$command}", $options);
}

/**
* Display success message prefixed with "Success: ".
*
* Success message is written to STDOUT.
*
* Typically recommended to inform user of successful script conclusion.
*
* @param string $message Message to write to STDOUT.
*
* @return void
*/
protected function success($message)
{
\WP_CLI::success($message);
}

/**
* Display warning message prefixed with "Warning: ".
*
* Warning message is written to STDERR.
*
* @param string $message Message to write to STDOUT.
*
* @return void
*/
protected function warning($message)
{
\WP_CLI::warning($message);
}

/**
* Display error message prefixed with "Error: " and exit script.
*
* Error message is written to STDERR.
*
* @param string|array $message Message to write to STDOUT.
* @param bool|int $exit Exit code for application
*
* @return void
*/
protected function error($message, $exit = true)
{
\WP_CLI::{is_array($message) ? 'error_multi_line' : 'error'}($message);
}

/**
* Display informational message without prefix, and ignore `--quiet`.
*
* Message is written to STDOUT.
*
* @param string $message Message to write to STDOUT.
*
* @return void
*/
protected function line($message)
{
\WP_CLI::line($message);
}

/**
* Display informational message without prefix.
*
* Message is written to STDOUT, or discarded when `--quiet` flag is supplied.
*
* @param string $message Message to write to STDOUT.
*
* @return void
*/
protected function info($message)
{
\WP_CLI::log($message);
}

/**
* Display debug message prefixed with "Debug: " when `--debug` is used.
*
* Debug message is written to STDERR, and includes script execution time.
*
* @param string $message Message to write to STDOUT.
* @param string|bool $group Organize debug message to a specific group.
*
* @return void
*/
protected function debug($message, $group = false)
{
\WP_CLI::debug($message, $group);
}

/**
* Display success message prefixed with "Success: ".
*
* @param string $message Message to write to STDOUT.
*
* @return void
*/
protected function choice($message, iterable $choices)
{
fwrite(STDOUT, "$message\n\n ");
foreach ($choices as $key => $value) {
fwrite(STDOUT, "[{$key}] - {$value}\n ");
}
fwrite(STDOUT, "\n >");

$answer = trim(fgets(STDIN));

if (! isset($choices[$answer])) {
$this->info("{$answer} is not a valid option.");
return $this->choice($message, $choices);
}

return $choices[$answer];
}
}
56 changes: 56 additions & 0 deletions src/Acorn/Console/ComposerMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Roots\Acorn\Console;

class ComposerMakeCommand extends GeneratorCommand
{
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Composer';

/**
* Create a new composer class
*
* ## OPTIONS
*
* <name>
* : The name of the composer.
*
* [--force]
* : Overwrite any existing files
*
* ## EXAMPLES
*
* wp acorn make:composer
*/
public function __invoke($args, $assoc_args)
{
list($name) = $args;
$this->parse($assoc_args + compact('name'));
$this->handle();
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__ . '/stubs/composer.stub';
}

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