Skip to content

Commit

Permalink
Merge branch 'feature/locator-aware-pluginbroker' of https://github.c…
Browse files Browse the repository at this point in the history
…om/EvanDotPro/zf2 into feature/broker-locator_aware
  • Loading branch information
weierophinney committed Nov 17, 2011
2 parents 1b0d659 + 22c975b commit b3a768f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
11 changes: 11 additions & 0 deletions library/Zend/Loader/LocatorAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Zend\Loader;

use Zend\Di\Locator;

interface LocatorAware
{
public function setLocator(Locator $locator);
public function getLocator();
}
54 changes: 47 additions & 7 deletions library/Zend/Loader/PluginBroker.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

namespace Zend\Loader;

use Zend\Di\Locator;

/**
* Plugin broker base implementation
*
Expand All @@ -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
Expand All @@ -56,6 +58,11 @@ class PluginBroker implements Broker
*/
protected $validator;

/**
* @var Zend\Di\Locator
*/
protected $locator;

/**
* Constructor
*
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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 b3a768f

Please sign in to comment.