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

Commit

Permalink
[zen-18][#1858] Refactored to use AbstractPluginManager
Browse files Browse the repository at this point in the history
- s/HelperLoader/HelperConfiguration/ -- configuration for view helpers
- added LoaderPluginManager, which is a context-specific service manager for
  translation loaders. Refactored Translator to use this instead of plugin
  broker
  • Loading branch information
weierophinney committed Jun 26, 2012
1 parent 5547cda commit 4919278
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 38 deletions.
74 changes: 74 additions & 0 deletions library/Zend/I18n/Translator/LoaderPluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_I18n
* @subpackage Translator
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\I18n\Translator;

use Zend\I18n\Exception;
use Zend\ServiceManager\AbstractPluginManager;

/**
* Plugin manager implementation for translation loaders.
*
* Enforces that filters retrieved are either callbacks or instances of
* Loader\LoaderInterface. Additionally, it registers a number of default
* loaders.
*
* @category Zend
* @package Zend_I18n
* @subpackage Translator
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class LoaderPluginManager extends AbstractPluginManager
{
/**
* Default set of loaders
*
* @var array
*/
protected $invokableClasses = array(
'phpArray' => 'Zend\I18n\Translator\Loader\PhpArray',
'gettext' => 'Zend\I18n\Translator\Loader\Gettext',
);

/**
* Validate the plugin
*
* Checks that the filter loaded is an instance of Loader\LoaderInterface.
*
* @param mixed $plugin
* @return void
* @throws Exception\RuntimeException if invalid
*/
public function validatePlugin($plugin)
{
if ($plugin instanceof Loader\LoaderInterface) {
// we're okay
return;
}

throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement %s\Loader\LoaderInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
__NAMESPACE__
));
}
}
4 changes: 2 additions & 2 deletions library/Zend/I18n/Translator/Plural/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public function expression($rightBindingPower = 0)
while ($rightBindingPower < $this->currentToken->leftBindingPower) {
$token = $this->currentToken;
$this->currentToken = $this->getNextToken();
$left = $token->getLeftDenoation($left);
$left = $token->getLeftDenotation($left);
}

return $left;
Expand Down Expand Up @@ -381,4 +381,4 @@ protected function getNextToken()

return $token;
}
}
}
43 changes: 17 additions & 26 deletions library/Zend/I18n/Translator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
namespace Zend\I18n\Translator;

use Locale;
use Zend\Loader\Broker;
use Zend\Loader\PluginBroker;
use Zend\Cache\Storage\StorageInterface as CacheStorage;

/**
Expand Down Expand Up @@ -80,11 +78,11 @@ class Translator
protected $cache;

/**
* Plugin broker.
* Plugin manager for translation loaders.
*
* @var Broker
* @var LoaderPluginManager
*/
protected $pluginBroker;
protected $pluginManager;

/**
* Set the default locale.
Expand Down Expand Up @@ -161,38 +159,31 @@ public function getCache()
}

/**
* Set the plugin broker
* Set the plugin manager for translation loaders
*
* @param Broker $pluginBroker
* @param LoaderPluginManager $pluginManager
* @return Translator
*/
public function setPluginBroker(Broker $pluginBroker)
public function setPluginManager(LoaderPluginManager $pluginManager)
{
$this->pluginBroker = $pluginBroker;
$this->pluginManager = $pluginManager;
return $this;
}

/**
* Retreive the plugin broker.
* Retreive the plugin manager for tranlation loaders.
*
* Lazy loads an instance if none currently set.
*
* @param Broker $broker
* @return Broker
* @return LoaderPluginManager
*/
public function getPluginBroker()
public function getPluginManager()
{
if (!$this->pluginBroker instanceof Broker) {
$broker = new PluginBroker();
$broker->getClassLoader()->registerPlugins(array(
'phpArray' => __NAMESPACE__ . '\Loader\PhpArray',
'gettext' => __NAMESPACE__ . '\Loader\Gettext',
// And so on...
));
$this->setPluginBroker($broker);
if (!$this->pluginManager instanceof LoaderPluginManager) {
$this->setPluginManager(new LoaderPluginManager());
}

return $this->pluginBroker;
return $this->pluginManager;
}

/**
Expand Down Expand Up @@ -384,8 +375,8 @@ protected function loadMessages($textDomain, $locale)
. sprintf($pattern['pattern'], $locale);

if (is_file($filename)) {
$this->messages[$textDomain][$locale] = $this->getPluginBroker()
->load($pattern['type'])
$this->messages[$textDomain][$locale] = $this->getPluginManager()
->get($pattern['type'])
->load($filename, $locale);
}
}
Expand All @@ -398,8 +389,8 @@ protected function loadMessages($textDomain, $locale)
}

$file = $this->files[$textDomain][$currentLocale];
$this->messages[$textDomain][$locale] = $this->getPluginBroker()
->load($file['type'])
$this->messages[$textDomain][$locale] = $this->getPluginManager()
->get($file['type'])
->load($file['filename'], $locale);

unset($this->files[$textDomain][$currentLocale]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,39 @@

namespace Zend\I18n\View;

use Zend\Loader\PluginClassLoader;
use Zend\ServiceManager\ConfigurationInterface;
use Zend\ServiceManager\ServiceManager;

/**
* Plugin Class Loader implementation for i18n view helpers.
* Service manager configuration for i18n view helpers.
*
* @category Zend
* @package Zend_I18n
* @subpackage View
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class HelperLoader extends PluginClassLoader
class HelperLoader implements ConfigurationInterface
{
/**
* $plugins: defined by PluginClassLoader.
*
* @see PluginClassLoader::$plugins
* @var array
* @var array Pre-aliased view helpers
*/
protected $plugins = array(
protected $invokables = array(
'translate' => 'Zend\I18n\View\Helper\Translate',
'translatePlural' => 'Zend\I18n\View\Helper\TranslatePlural',
'translateplural' => 'Zend\I18n\View\Helper\TranslatePlural',
);

/**
* Configure the provided service manager instance with the configuration
* in this class.
*
* @param ServiceManager $serviceManager
* @return void
*/
public function configureServiceManager(ServiceManager $serviceManager)
{
foreach ($this->invokables as $name => $service) {
$serviceManager->setInvokableClass($name, $service);
}
}
}
2 changes: 1 addition & 1 deletion tests/Zend/I18n/Translator/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testTranslate()
{
$loader = new TestLoader();
$loader->textDomain = new TextDomain(array('foo' => 'bar'));
$this->translator->getPluginBroker()->register('test', $loader);
$this->translator->getPluginManager()->setService('test', $loader);
$this->translator->addTranslationFile('test', null);

$this->assertEquals('bar', $this->translator->translate('foo'));
Expand Down

0 comments on commit 4919278

Please sign in to comment.