-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce0a1e0
commit 77a3477
Showing
4 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters