Skip to content

Commit

Permalink
plugin manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Markcial committed Mar 2, 2015
1 parent 9b285a5 commit 0aa1a68
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Psy/Command/DemoCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Psy\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class DemoCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('demo')
->setDefinition(array(
new InputOption('message', 'm', InputOption::VALUE_REQUIRED, 'Message to send.'),
))
->setDescription('Sample command just for testing.')
->setHelp(
<<<HELP
HELP
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{

$message = $input->getOption('message');
$output->writeln(sprintf('<info>Received message "%s". </info>', $message));
}
}
68 changes: 68 additions & 0 deletions src/Psy/Plugin/AbstractPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Psy\Plugin;


abstract class AbstractPlugin
{
public static function register()
{
Manager::register(new static, static::getName());
}

/**
* @return string
*/
final public static function getName()
{
$class = new \ReflectionClass(get_called_class());
return preg_replace('#Plugin$#', '', $class->getShortName());
}

/**
* @param array $configuration
*
* @return array
*/
final public static function getConfiguration($configuration = array())
{
return array_merge_recursive(
$configuration,
array(
'commands' => static::getCommands(),
'presenters' => static::getPresenters(),
'matchers' => static::getMatchers()
// if any more parts of the config are exposed publicly, remember to add here with the static ref.
)
);
}

// any publicly exposed configuration piece below here ↓

/**
* @return array
*/
public static function getCommands()
{
// add your own commands
return array();
}

/**
* @return array
*/
public static function getPresenters()
{
// add your own presenters
return array();
}

/**
* @return array
*/
public static function getMatchers()
{
// add your own presenters
return array();
}
}
16 changes: 16 additions & 0 deletions src/Psy/Plugin/DemoPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Psy\Plugin;

use Psy\Command\DemoCommand;

class DemoPlugin extends AbstractPlugin
{

public static function getCommands()
{
return array(
new DemoCommand()
);
}
}
31 changes: 31 additions & 0 deletions src/Psy/Plugin/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Psy\Plugin;

class Manager
{
/** @var AbstractPlugin[] */
protected static $plugins = array();

/**
* @param AbstractPlugin $plugin
* @param $name
*/
public static function register(AbstractPlugin $plugin, $name)
{
self::$plugins[$name] = $plugin;
}

/**
* @param array $configuration
* @return array
*/
public static function getConfiguration($configuration = array())
{
foreach (self::$plugins as $plugin) {
$configuration = $plugin::getConfiguration($configuration);
}

return $configuration;
}
}

0 comments on commit 0aa1a68

Please sign in to comment.