Skip to content

Commit

Permalink
[snippet] Add new command. (#3511)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas committed Sep 9, 2017
1 parent 38b92be commit 4ddcdad
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/services/misc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ services:
class: Drupal\Console\Command\ShellCommand
tags:
- { name: drupal.command }
console.snippet:
class: Drupal\Console\Command\SnippetCommand
arguments: ['@console.root', '@app.root']
tags:
- { name: drupal.command }
130 changes: 130 additions & 0 deletions src/Command/SnippetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Drupal\Console\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\Command;
use Symfony\Component\Filesystem\Filesystem;
use Drupal\Console\Core\Style\DrupalStyle;

/**
* Class SnippetCommand
*
* @package Drupal\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)
{
$io = new DrupalStyle($input, $output);
$file = $input->getOption('file');
$code = $input->getOption('code');
$showCode = $input->getOption('show-code');

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

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

return 1;
}

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

return 1;
}

if ($showCode) {
$code = file_get_contents($file);
$io->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;
}
}

0 comments on commit 4ddcdad

Please sign in to comment.