Skip to content

Commit 243a74d

Browse files
committed
plugin manager
1 parent 9b285a5 commit 243a74d

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

src/Psy/Command/DemoCommand.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Psy\Command;
4+
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Input\InputOption;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
class DemoCommand extends Command
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
protected function configure()
15+
{
16+
$this
17+
->setName('demo')
18+
->setDefinition(array(
19+
new InputOption('message', 'm', InputOption::VALUE_REQUIRED, 'Message to send.'),
20+
))
21+
->setDescription('Sample command just for testing.')
22+
->setHelp(
23+
<<<HELP
24+
HELP
25+
);
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function execute(InputInterface $input, OutputInterface $output)
32+
{
33+
$message = $input->getOption('message');
34+
$output->writeln(sprintf('<info>Received message "%s". </info>', $message));
35+
}
36+
}

src/Psy/Plugin/AbstractPlugin.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Psy\Plugin;
4+
5+
abstract class AbstractPlugin
6+
{
7+
public static function register()
8+
{
9+
Manager::register(new static(), static::getName());
10+
}
11+
12+
/**
13+
* @return string
14+
*/
15+
final public static function getName()
16+
{
17+
$class = new \ReflectionClass(get_called_class());
18+
19+
return preg_replace('#Plugin$#', '', $class->getShortName());
20+
}
21+
22+
/**
23+
* @param array $configuration
24+
*
25+
* @return array
26+
*/
27+
final public static function getConfiguration($configuration = array())
28+
{
29+
return array_merge_recursive(
30+
$configuration,
31+
array(
32+
'commands' => static::getCommands(),
33+
'presenters' => static::getPresenters(),
34+
'matchers' => static::getMatchers(),
35+
// if any more parts of the config are exposed publicly, remember to add here with the static ref.
36+
)
37+
);
38+
}
39+
40+
// any publicly exposed configuration piece below here ↓
41+
42+
/**
43+
* @return array
44+
*/
45+
public static function getCommands()
46+
{
47+
// add your own commands
48+
return array();
49+
}
50+
51+
/**
52+
* @return array
53+
*/
54+
public static function getPresenters()
55+
{
56+
// add your own presenters
57+
return array();
58+
}
59+
60+
/**
61+
* @return array
62+
*/
63+
public static function getMatchers()
64+
{
65+
// add your own presenters
66+
return array();
67+
}
68+
}

src/Psy/Plugin/DemoPlugin.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Psy\Plugin;
4+
5+
use Psy\Command\DemoCommand;
6+
7+
class DemoPlugin extends AbstractPlugin
8+
{
9+
/**
10+
* @return array
11+
*/
12+
public static function getCommands()
13+
{
14+
return array(
15+
new DemoCommand(),
16+
);
17+
}
18+
}

src/Psy/Plugin/Manager.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Psy\Plugin;
4+
5+
class Manager
6+
{
7+
/** @var AbstractPlugin[] */
8+
protected static $plugins = array();
9+
10+
/**
11+
* @param AbstractPlugin $plugin
12+
* @param $name
13+
*/
14+
public static function register(AbstractPlugin $plugin, $name)
15+
{
16+
self::$plugins[$name] = $plugin;
17+
}
18+
19+
/**
20+
* @param array $configuration
21+
*
22+
* @return array
23+
*/
24+
public static function getConfiguration($configuration = array())
25+
{
26+
foreach (self::$plugins as $plugin) {
27+
$configuration = $plugin::getConfiguration($configuration);
28+
}
29+
30+
return $configuration;
31+
}
32+
}

0 commit comments

Comments
 (0)