Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2 into zf11884
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Kellner committed Nov 19, 2011
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/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();
}
3 changes: 3 additions & 0 deletions src/ModuleAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Zend\Loader;

// Grab SplAutoloader interface
require_once __DIR__ . '/SplAutoloader.php';

use SplFileInfo,
Traversable;

Expand Down
54 changes: 47 additions & 7 deletions src/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;
}
}
17 changes: 16 additions & 1 deletion test/PluginBrokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

namespace ZendTest\Loader;

use Zend\Loader\PluginBroker,
use stdClass,
Zend\Loader\PluginBroker,
Zend\Loader\PluginClassLoader;

/**
Expand Down Expand Up @@ -274,4 +275,18 @@ public function testAllowsConfigurationViaConstructor()
$loader = $broker->getClassLoader();
$this->assertInstanceOf('ZendTest\Loader\TestAsset\CustomClassLoader', $loader);
}

public function testWillPullFromLocatorIfAttached()
{
$locator = new TestAsset\ServiceLocator();
$plugin = new stdClass;
$locator->set('ZendTest\Loader\TestAsset\Foo', $plugin);

$loader = $this->broker->getClassLoader();
$loader->registerPlugin('foo', 'ZendTest\Loader\TestAsset\Foo');
$this->broker->setLocator($locator);

$test = $this->broker->load('foo');
$this->assertSame($plugin, $test);
}
}
24 changes: 24 additions & 0 deletions test/TestAsset/ServiceLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace ZendTest\Loader\TestAsset;

use Zend\Di\Locator;

class ServiceLocator implements Locator
{
protected $services = array();

public function get($name, array $params = array())
{
if (!isset($this->services[$name])) {
return null;
}

return $this->services[$name];
}

public function set($name, $object)
{
$this->services[$name] = $object;
}
}

0 comments on commit df0d05e

Please sign in to comment.