Skip to content

Commit

Permalink
Refining the PluginRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
burzum authored and markstory committed Feb 13, 2018
1 parent e01065e commit 7367598
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/Core/PluginInterface.php
Expand Up @@ -18,6 +18,14 @@
*/
interface PluginInterface
{
/**
* Load all the application configuration and bootstrap logic.
*
* Override this method to add additional bootstrap logic for your application.
*
* @return void
*/
public function bootstrap();

/**
* Disables route loading for the plugin
Expand Down
4 changes: 2 additions & 2 deletions src/Core/PluginRegistry.php
Expand Up @@ -20,7 +20,7 @@
/**
* Plugin Registry
*/
class PluginRegistry extends ObjectRegistry implements ConsoleApplicationInterface, HttpApplicationInterface
class PluginRegistry extends ObjectRegistry implements PluginRegistryInterface
{

/**
Expand Down Expand Up @@ -79,7 +79,7 @@ protected function _create($class, $alias, $config)
if ((!$instance instanceof HttpApplicationInterface
&& !$instance instanceof ConsoleApplicationInterface)
|| !$instance instanceof PluginInterface) {
throw new \RuntimeException(sprintf(
throw new RuntimeException(sprintf(
'`%s` is not a valid plugin object. It\'s not implementing `%s` or `%s`',
get_class($instance)),
HttpApplicationInterface::class,
Expand Down
29 changes: 29 additions & 0 deletions src/Core/PluginRegistryInterface.php
@@ -0,0 +1,29 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.6.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace Cake\Core;

use Countable;
use IteratorAggregate;

/**
* Plugin Registry Interface
*/
interface PluginRegistryInterface extends
Countable,
IteratorAggregate,
ConsoleApplicationInterface,
HttpApplicationInterface
{

}
44 changes: 40 additions & 4 deletions src/Http/BaseApplication.php
Expand Up @@ -17,8 +17,10 @@
use Cake\Core\ConsoleApplicationInterface;
use Cake\Core\HttpApplicationInterface;
use Cake\Core\PluginRegistry;
use Cake\Core\PluginRegistryInterface;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

Expand All @@ -42,17 +44,51 @@ abstract class BaseApplication implements ConsoleApplicationInterface, HttpAppli
*
* @param \Cake\Core\PluginRegistry
*/
protected $plugins;
protected $pluginRegistry;

/**
* Default Plugin Registry Class
*
* @var string
*/
protected $defaultPluginRegistry = PluginRegistry::class;

/**
* Constructor
*
* @param string $configDir The directory the bootstrap configuration is held in.
* @param string|null $pluginRegistry Plugin Registry Object
*/
public function __construct($configDir)
public function __construct($configDir, $pluginRegistry = null)
{
$this->configDir = $configDir;
$this->plugins = new PluginRegistry();

$this->setPluginRegistry($pluginRegistry);
}

/**
* Sets the plugin registry
*
* @param string|null $pluginRegistry Plugin Registry Object
* @return void
*/
public function setPluginRegistry($pluginRegistry = null)
{
if (empty($pluginRegistry)) {
$this->pluginRegistry = new $this->defaultPluginRegistry();

return;
}

if (!$pluginRegistry instanceof PluginRegistryInterface) {
throw new InvalidArgumentException(sprintf(
'`%s` is not an instance of `%s`',
get_class($pluginRegistry),
PluginRegistryInterface::class
));
}

$this->pluginRegistry = $pluginRegistry;
}

/**
Expand Down Expand Up @@ -134,6 +170,6 @@ protected function getDispatcher()
*/
public function plugins()
{
return $this->plugins;
return $this->pluginRegistry;
}
}

0 comments on commit 7367598

Please sign in to comment.