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

Feature/20 #37

Closed
wants to merge 2 commits into from
Closed
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
98 changes: 98 additions & 0 deletions Application/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/*
* This file is part of the CoreSphereConsoleBundle.
*
* (c) Laszlo Korte <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CoreSphere\ConsoleBundle\Application;

use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application as BaseApplication;

/**
* Class Application
*
* @package CoreSphere\ConsoleBundle\Application
*/
class Application extends BaseApplication
{
const FILTER_WHITELIST = 'whitelist';
const FILTER_BLACKLIST = 'blacklist';

/**
* @var array
*/
private $config = array();

/**
* Constructor.
*
* @param KernelInterface $kernel
* @param array $config
*/
public function __construct(KernelInterface $kernel, $config = array())
{
parent::__construct($kernel);

$this->config = $config;

foreach ($kernel->getBundles() as $bundle) {
$bundle->registerCommands($this);
}

// Reflection Property
$propertyReflection = new \ReflectionProperty('\\Symfony\\Bundle\\FrameworkBundle\\Console\\Application', 'commandsRegistered');
$propertyReflection->setAccessible(true);
$propertyReflection->setValue($this, true);

if (isset($config['filtering'])) {
$filterOptions = (count($config['filtering']['whitelist']) > 0 ? self::FILTER_WHITELIST : self::FILTER_BLACKLIST);
$this->filter($config['filtering'][$filterOptions], $filterOptions);
}
}

/**
* Get Configs
*
* @return array
*/
public function getConfigs()
{
return $this->config;
}

/**
* Filter
*
* @param array $commandList
* @param string $filterOption
*/
public function filter($commandList = array(), $filterOption = self::FILTER_WHITELIST)
{
// Sets
$allCommands = $this->all();
$availableCommands = array();

// Check Filtering
if (empty($commandList)) {
return;
}

$filterOption = ($filterOption == self::FILTER_WHITELIST ? true : false);
foreach ($allCommands as $key => $command) {
if (preg_match('/' . implode('|', $commandList) . '/', $key) == $filterOption) {
$availableCommands[$key] = $command;
}
}

// Reflection Property
$propertyReflection = new \ReflectionProperty('\\Symfony\\Component\\Console\\Application', 'commands');
$propertyReflection->setAccessible(true);
$propertyReflection->setValue($this, $availableCommands);
}
}
44 changes: 26 additions & 18 deletions Controller/ConsoleController.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,48 @@

namespace CoreSphere\ConsoleBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Console\Application;

use CoreSphere\ConsoleBundle\Executer\CommandExecuter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
* Class ConsoleController
*
* @package CoreSphere\ConsoleBundle\Controller
*/
class ConsoleController extends Controller
{
/**
* Console Action
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function consoleAction()
{
$kernel = $this->get('kernel');
$application = new Application($kernel);

chdir($kernel->getRootDir().'/..');

foreach ($kernel->getBundles() as $bundle) {
$bundle->registerCommands($application);
}
$application = $this->get('coresphere_console.application');

return $this->render('CoreSphereConsoleBundle:Console:console.html.twig', array(
'working_dir' => getcwd(),
'environment' => $kernel->getEnvironment(),
'commands' => $application->all(),
'working_dir' => getcwd(),
'environment' => $application->getKernel()->getEnvironment(),
'commands' => $application->all(),
));
}

/**
* Executer Action
*
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function execAction(Request $request)
{
$executer = new CommandExecuter($this->get('kernel'));
$commands = $request->request->get('commands');
$executedCommands = array();
$commandExecuter = $this->get('coresphere_console.services.command_executer');

$executedCommands = array();
foreach ($commands as $command) {
$result = $executer->execute($command);

$result = $commandExecuter->execute($command);
$executedCommands[] = $result;

if (0 !== $result['error_code']) {
Expand Down
5 changes: 5 additions & 0 deletions CoreSphereConsoleBundle.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
namespace CoreSphere\ConsoleBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class CoreSphereConsoleBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
}
}
40 changes: 40 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace CoreSphere\ConsoleBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* Class Configuration
*
* @package CoreSphere\ConsoleBundle\DependencyInjection
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('core_sphere_console');

$rootNode
->children()
->arrayNode('filtering')
->children()
->arrayNode('whitelist')
->prototype('scalar')->end()
->end()
->arrayNode('blacklist')
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
;

return $treeBuilder;
}
}
25 changes: 23 additions & 2 deletions DependencyInjection/CoreSphereConsoleExtension.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,37 @@

namespace CoreSphere\ConsoleBundle\DependencyInjection;

use CoreSphere\ConsoleBundle\Application\Application;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Config\FileLocator;

/**
* Class CoreSphereConsoleExtension
*
* @package CoreSphere\ConsoleBundle\DependencyInjection
*/
class CoreSphereConsoleExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
// Load Yaml File
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('toolbar_listener.yml');
$loader->load('services.yml');

// Process Configurations
$config = $this->processConfiguration(new Configuration(), $configs);

// Check Filtering
if (isset($config['filtering']) && (count($config['filtering']['whitelist']) > 0 && count($config['filtering']['blacklist']) > 0)) {
throw new \LogicException('Use the whitelist or blacklist configuration.');
}

// Set Container Parameter
$container->setParameter('coresphere_console.config', $config);
}
}
1 change: 0 additions & 1 deletion Resources/config/routing.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ console:
defaults: { _controller: CoreSphereConsoleBundle:Console:console }
methods: [GET]


console_exec:
path: /commands.{_format}
defaults: { _controller: CoreSphereConsoleBundle:Console:exec, _format: json }
Expand Down
14 changes: 14 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:

coresphere_console.toolbar:
class: CoreSphere\ConsoleBundle\DataCollector\DataCollector
tags:
- { name: data_collector, template: "CoreSphereConsoleBundle:Toolbar:toolbar", id: "coresphere_console" }

coresphere_console.application:
class: CoreSphere\ConsoleBundle\Application\Application
arguments: ['@kernel', '%coresphere_console.config%']

coresphere_console.services.command_executer:
class: CoreSphere\ConsoleBundle\Services\Executer\CommandExecuter
arguments: ['@kernel', '%coresphere_console.config%']
5 changes: 0 additions & 5 deletions Resources/config/toolbar_listener.yml

This file was deleted.

Loading