したがって、モジュール構造は主に制御反転の原理に基づいています 。 IoCコンテナと独自のライブラリを使用します 。
モジュール管理ライブラリを作成することから始めましょう。 私はそれをModularと呼びました。
最初にcomposer.jsonについて説明します。
{ "name":"elfet/modular", "type":"library", "autoload": { "psr-0": { "Modular": "src/" } }, "require":{ "php":">=5.3.0", "elfet/ioc":"dev-master" } }
「モジュラー」を使用する場所では、IoCを接続します。
モジュラーシステムの提案された構造は次のとおりです。
index.php - app/ app.ini - ModuleOne/ module.ini - ModuleTwo/
Appコントローラーの前面のクラスについて説明します。
namespace Modular; use IoC\Container; use Composer\Autoload\ClassLoader; // /vendor/autoload.php class App { protected $rootDir; // app/ protected $ioc; // ioc protected $loader; // . public function __construct($rootDir, ClassLoader $classLoader) { $this->rootDir = $rootDir; $this->ioc = Container::getInstance(); $this->loader = new Loader($this->ioc, $classLoader); } public function load() { $appConfig = parse_ini_file($this->rootDir . '/app.ini', true); // app.ini // // . foreach ($appConfig as $module => $config) { // Modular\Module $config = array_merge(array( 'class' => 'Modular\Module', 'path' => $this->rootDir . '/module/' . $module, ), $config); // $this->loader->load( $module, $config['class'], $this->rootDir . '/' . $config['path'] ); } } public function run() { $this->load(); } }
モジュールのロードがどのように機能するかを見てみましょう。
public function load($moduleName, $moduleClass, $moduleDir) { // // ( PSR-0) $this->classLoader->add($moduleName, dirname($moduleDir)); // $module = new $moduleClass; $module->setModuleDir($moduleDir); // / IoC. // load // module.ini $module->load($this->ioc); }
モジュールを記述するモジュールクラスを作成します。
namespace Modular; use IoC\Container; use IoC\Assoc\Service; class Module { private $moduleDir; // . public function load(Container $container) { $this->loadFromFile($container, $this->getModuleDir() . '/module.ini'); } protected function loadFromFile(Container $container, $file) { $module = parse_ini_file($file, true); foreach ($module as $class => $params) { // // IoC Reflection ( ). $interfaces = isset($params['interface']) ? (array)$params['interface'] : array(); // . unset($params['interface']); // - . // $class . // . $serviceAssoc = new Service($class, $params); $container->assoc($serviceAssoc, $interfaces); } } ... }
次に、モジュールを作成してから展開してみましょう。 簡単にするために、ノートブックを作成してみてください。 すべてのコードはここにあります 。
composer.jsonを作成します。
{ "require":{ "php":">=5.3.0", "elfet/modular":"dev-master" } }
そしてcomposer installを実行します。 これで、必要なものがすべて揃ったベンダー/フォルダーができました。
アプリ/メモ帳/フォルダーを作成し、StorageInterfaceストレージインターフェイスを作成することから始めます。
namespace Notepad; interface StorageInterface { public function set($key, $value); public function get($key); public function save(); public function load(); }
また、 FileStorageの簡単な実装。
コード
namespace Notepad; use Notepad\StorageInterface; class FileStorage implements StorageInterface { protected $store = array(); protected $file; public function __construct($file = 'store.json') { $this->file = realpath(__DIR__ . '/../cache/' . $file); } public function set($key, $value) { $this->store[$key] = $value; } public function get($key) { return isset($this->store[$key]) ? $this->store[$key] : null; } public function save() { file_put_contents($this->file, json_encode($this->store)); } public function load() { $content = file_get_contents($this->file); $this->store = (array)json_decode($content); } }
module.iniでこのクラスを説明します 。
[Notepad\FileStorage] interface = Notepad\StorageInterface file = store.json
これで、StorageInterfaceが含まれるコンストラクターのクラス(たとえばNotepad \ Controller )はFileStorageを受け取ります。
public function __construct(StorageInterface $storage)
すべてのメモ帳コードはこちらから入手できます 。
Notepadモジュールを拡張するMyNotepadモジュールを作成してみましょう。 たとえば、DbStorageを使用したいとします。 app / MyNotepad / DbStorage.phpを作成し、 app / MyNotepad / module.iniで記述します。
[MyNotepad\DbStorage] database = mystore.db
モジュールをapp.iniに追加します
[Notepad] path = Notepad/ [MyNotepad] path = MyNotepad/
これで、Notepad \ Controllerクラスは、作成時にMyNotepad \ DbStorageクラスのインスタンスを受け取ります。 そのように、メモ帳モジュールを変更せずに、その機能を拡張しました。 githubで、メモ帳の他の部分をオーバーライドする方法を確認できます。