Skip to content

Commit 194b58e

Browse files
committed
added Plugin\Loader class
1 parent 8be1f1a commit 194b58e

4 files changed

Lines changed: 53 additions & 83 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
}
88
],
99
"autoload":{
10-
"psr-0":{"":"src/"}
10+
"psr-4":{"":"src/"}
1111
},
1212
"require": {
1313
"phine/path": "~1.0",

src/Application/BaseApplication.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
use Command\ErrorCommand;
88
use DI\Container;
99
use DI\ContainerBuilder;
10-
use Plugin\Package;
10+
use Plugin\Loader;
1111

1212
abstract class BaseApplication
1313
{
1414
public function __construct($noFsIO)
1515
{
1616
$this->noFsIO = $noFsIO;
1717
$this->createContainer();
18-
$this->pluginsPackage = $this->container->get(Package::class);
18+
$this->pluginsLoader = $this->container->get(Loader::class);
1919
$this->loadPlugins();
2020
}
2121
public function handle($request, $response, $data)
@@ -68,21 +68,7 @@ protected function getRouter()
6868
}
6969
protected function loadPlugins()
7070
{
71-
$plugins = $this->pluginsPackage->getPluginsList();
72-
foreach ($plugins as $pluginName) {
73-
$parts = explode("/", $pluginName);
74-
$className = implode("\\", array_map(function ($part) {
75-
return ucfirst($part);
76-
}, $parts));
77-
$className .= "\\Plugin";
78-
try {
79-
/** @var \Plugin\PluginInterface */
80-
$plugin = $this->container->get($className);
81-
$plugin->init();
82-
} catch (\Exception $e) {
83-
printf("Plugin Error: %s\n", $e->getMessage());
84-
}
85-
}
71+
return $this->pluginsLoader->load();
8672
}
8773
abstract protected function getCommandName($request);
8874
protected function createContainer()
@@ -94,8 +80,8 @@ protected function createContainer()
9480
}
9581

9682
protected $router;
97-
/** @var Package */
98-
protected $pluginsPackage;
83+
/** @var Loader */
84+
protected $pluginsLoader;
9985
static protected $projectsPool = [];
10086
static protected $currentProject = null;
10187
/** @var Container */

src/Plugin/Loader.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Plugin;
4+
5+
use DI\Container;
6+
7+
class Loader
8+
{
9+
public function __construct(Package $package, Container $container)
10+
{
11+
$this->package = $package;
12+
$this->container = $container;
13+
}
14+
public function load()
15+
{
16+
$plugins = $this->package->getPluginsList();
17+
foreach ($plugins as $pluginName) {
18+
$this->loadPlugin($pluginName);
19+
}
20+
}
21+
public function loadPlugin($pluginName)
22+
{
23+
$parts = explode("/", $pluginName);
24+
$className = implode("\\", array_map(function ($part) {
25+
return implode("", array_map(
26+
function ($part) {
27+
return ucfirst($part);
28+
},
29+
explode("-", $part)
30+
));
31+
}, $parts));
32+
$className .= "\\" . $this->pluginClassName;
33+
try {
34+
/** @var \Plugin\PluginInterface */
35+
$plugin = $this->container->get($className);
36+
$plugin->init();
37+
} catch (\Exception $e) {
38+
printf("Plugin Error: %s\n", $e->getMessage());
39+
}
40+
}
41+
42+
private $pluginClassName = "Plugin";
43+
/** @var Package */
44+
private $package;
45+
/** @var Container */
46+
private $container;
47+
}

src/Plugin/Test.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)