Permalink
Browse files

Make Zend\Loader\PluginBroker LocatorAware

- Implement LocatorAware interface
- Tries to load plugin classes from the locator if it has been set
  • Loading branch information...
1 parent 724cb8b commit 3cf47b942058ec704183027fad2d3ba9f13696eb @EvanDotPro EvanDotPro committed Oct 22, 2011
Showing with 58 additions and 7 deletions.
  1. +11 −0 LocatorAware.php
  2. +47 −7 PluginBroker.php
View
@@ -0,0 +1,11 @@
+<?php
+
+namespace Zend\Loader;
+
+use Zend\Di\Locator;
+
+interface LocatorAware
+{
+ public function setLocator(Locator $locator);
+ public function getLocator();
+}
View
@@ -21,6 +21,8 @@
namespace Zend\Loader;
+use Zend\Di\Locator;
+
/**
* Plugin broker base implementation
*
@@ -29,7 +31,7 @@
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
-class PluginBroker implements Broker
+class PluginBroker implements Broker, LocatorAware
{
/**
* @var string Default class loader to utilize with this broker
@@ -57,6 +59,11 @@ class PluginBroker implements Broker
protected $validator;
/**
+ * @var Zend\Di\Locator
+ */
+ protected $locator;
+
+ /**
* Constructor
*
* Allow configuration via options; see {@link setOptions()} for details.
@@ -190,13 +197,25 @@ public function load($plugin, array $options = null)
}
}
- if (empty($options)) {
- $instance = new $class();
- } elseif ($this->isAssocArray($options)) {
- $instance = new $class($options);
+ if ($this->getLocator()) {
+ if (empty($options)) {
+ $instance = $this->getLocator()->get($class);
+ } elseif ($this->isAssocArray($options)) {
+ // This might be inconsistent with what $options should be?
+ $instance = $this->getLocator()->get($class, $options);
+ } else {
+ // @TODO: Clean this up, somehow?
+ $instance = $this->getLocator()->get($class);
+ }
} else {
- $r = new \ReflectionClass($class);
- $instance = $r->newInstanceArgs($options);
+ if (empty($options)) {
+ $instance = new $class();
+ } elseif ($this->isAssocArray($options)) {
+ $instance = new $class($options);
+ } else {
+ $r = new \ReflectionClass($class);
+ $instance = $r->newInstanceArgs($options);
+ }
}
if ($this->getRegisterPluginsOnLoad()) {
@@ -372,4 +391,25 @@ protected function isAssocArray($value)
}
return true;
}
+
+ /**
+ * Get locator.
+ *
+ * @return Zend\Di\Locator
+ */
+ public function getLocator()
+ {
+ return $this->locator;
+ }
+
+ /**
+ * Set locator.
+ *
+ * @param Zend\Di\Locator $locator
+ */
+ public function setLocator(Locator $locator)
+ {
+ $this->locator = $locator;
+ return $this;
+ }
}

0 comments on commit 3cf47b9

Please sign in to comment.