Skip to content

Commit

Permalink
[snippet] Add new command (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjuarez20 authored and enzolutions committed Jan 24, 2019
1 parent ce0a1e0 commit 77a3477
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 2 deletions.
5 changes: 5 additions & 0 deletions config/services/wp-console/misc.yml
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
services:
console.snippet:
class: WP\Console\Command\SnippetCommand
arguments: ['@console.root', '@app.root']
tags:
- { name: wordpress.command }
12 changes: 12 additions & 0 deletions config/translations/en/snippet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
description: 'Snippet command'
options:
file: 'User defined file containing code to get executed.'
code: 'Code to get executed.'
show-code: 'Show code to get executed.'
examples:
- description: 'Providing a file option using full path. (DEPRECATED)'
execution: |
wp-console snippet \
--file="/path/to/file/chain-file.yml"
128 changes: 128 additions & 0 deletions src/Command/SnippetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace WP\Console\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use WP\Console\Core\Command\Command;
use Symfony\Component\Filesystem\Filesystem;

/**
* Class SnippetCommand
*
* @package WP\Console\Command
*/
class SnippetCommand extends Command
{

/**
* @var string
*/
protected $consoleRoot;

/**
* @var string
*/
protected $appRoot;

/**
* RestoreCommand constructor.
*
* @param string $appRoot
*/
public function __construct($consoleRoot, $appRoot)
{
$this->consoleRoot = $consoleRoot;
$this->appRoot = $appRoot;
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('snippet')
->addOption(
'file',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.snippet.options.file')
)
->addOption(
'code',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.snippet.options.code')
)
->addOption(
'show-code',
null,
InputOption::VALUE_NONE,
$this->trans('commands.snippet.options.show-code')
)
->setDescription($this->trans('commands.snippet.description'))
->setHelp($this->trans('commands.snippet.help'));
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$file = $input->getOption('file');
$code = $input->getOption('code');
$showCode = $input->getOption('show-code');

if ($code) {
eval($code);
return 0;
}

if (!$file) {
$this->getIo()->error($this->trans('commands.snippet.errors.invalid-options'));

return 1;
}

$file = $this->getFileAsAbsolutePath($file);
if (!$file) {
$this->getIo()->error($this->trans('commands.snippet.errors.invalid-file'));

return 1;
}

if ($showCode) {
$code = file_get_contents($file);
$this->getIo()->writeln($code);
}

include_once $file;

return 0;
}

private function getFileAsAbsolutePath($file)
{
$fs = new Filesystem();

if ($fs->isAbsolutePath($file)) {
return $fs->exists($file)?$file:null;
}

$files = [
$this->consoleRoot.'/'.$file,
$this->appRoot.'/'.$file
];

foreach ($files as $file) {
if ($fs->exists($file)) {
return $file;
}
}

return null;
}
}
4 changes: 2 additions & 2 deletions src/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,11 @@ public function getData($filterNamespaces = null, $excludeNamespaces = [], $excl
'help',
'init',
'list',
'server'
'server',
'snippet'
];

$data = [];

// Exclude misc if it is inside the $excludeNamespaces array.
if (!in_array('misc', $excludeNamespaces)) {
foreach ($singleCommands as $singleCommand) {
Expand Down

0 comments on commit 77a3477

Please sign in to comment.