1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace Laminas\I18n\Translator;
use Laminas\I18n\Exception;
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Zend\I18n\Translator\Loader\Gettext;
use Zend\I18n\Translator\Loader\Ini;
use Zend\I18n\Translator\Loader\PhpArray;
use function get_class;
use function gettype;
use function is_object;
use function sprintf;
/**
* Plugin manager implementation for translation loaders.
*
* Enforces that loaders retrieved are either instances of
* Loader\FileLoaderInterface or Loader\RemoteLoaderInterface. Additionally,
* it registers a number of default loaders.
*
* If you are wanting to use the ability to load translation files from the
* include_path, you will need to create a factory to override the defaults
* defined in this class. A simple factory might look like:
*
* <code>
* function ($translators) {
* $adapter = new Gettext();
* $adapter->setUseIncludePath(true);
* return $adapter;
* }
* </code>
*
* You may need to override the Translator service factory to make this happen
* more easily. That can be done by extending it:
*
* <code>
* use Laminas\I18n\Translator\TranslatorServiceFactory;
* // or Laminas\Mvc\I18n\TranslatorServiceFactory
* use Laminas\ServiceManager\ServiceLocatorInterface;
*
* class MyTranslatorServiceFactory extends TranslatorServiceFactory
* {
* public function createService(ServiceLocatorInterface $services)
* {
* $translator = parent::createService($services);
* $translator->getLoaderPluginManager()->setFactory(...);
* return $translator;
* }
* }
* </code>
*
* You would then specify your custom factory in your service configuration.
*/
class LoaderPluginManager extends AbstractPluginManager
{
/** @var array<string, class-string> */
protected $aliases = [
'gettext' => Loader\Gettext::class,
'getText' => Loader\Gettext::class,
'GetText' => Loader\Gettext::class,
'ini' => Loader\Ini::class,
'phparray' => Loader\PhpArray::class,
'phpArray' => Loader\PhpArray::class,
'PhpArray' => Loader\PhpArray::class,
// Legacy Zend Framework aliases
Gettext::class => Loader\Gettext::class,
Ini::class => Loader\Ini::class,
PhpArray::class => Loader\PhpArray::class,
// v2 normalized FQCNs
'zendi18ntranslatorloadergettext' => Loader\Gettext::class,
'zendi18ntranslatorloaderini' => Loader\Ini::class,
'zendi18ntranslatorloaderphparray' => Loader\PhpArray::class,
];
/** @var array<string, class-string> */
protected $factories = [
Loader\Gettext::class => InvokableFactory::class,
Loader\Ini::class => InvokableFactory::class,
Loader\PhpArray::class => InvokableFactory::class,
// Legacy (v2) due to alias resolution; canonical form of resolved
// alias is used to look up the factory, while the non-normalized
// resolved alias is used as the requested name passed to the factory.
'laminasi18ntranslatorloadergettext' => InvokableFactory::class,
'laminasi18ntranslatorloaderini' => InvokableFactory::class,
'laminasi18ntranslatorloaderphparray' => InvokableFactory::class,
];
/**
* Validate the plugin.
*
* Checks that the filter loaded is an instance of
* Loader\FileLoaderInterface or Loader\RemoteLoaderInterface.
*
* @param mixed $plugin
* @return void
* @throws Exception\RuntimeException If invalid.
*/
public function validate($plugin)
{
if ($plugin instanceof Loader\FileLoaderInterface || $plugin instanceof Loader\RemoteLoaderInterface) {
// we're okay
return;
}
throw new InvalidServiceException(sprintf(
'Plugin of type %s is invalid; must implement %s\Loader\FileLoaderInterface '
. 'or %s\Loader\RemoteLoaderInterface',
is_object($plugin) ? get_class($plugin) : gettype($plugin),
__NAMESPACE__,
__NAMESPACE__
));
}
/**
* Validate the plugin is of the expected type (v2).
*
* Proxies to `validate()`.
*
* @param mixed $plugin
* @throws Exception\RuntimeException
*/
public function validatePlugin($plugin)
{
try {
$this->validate($plugin);
} catch (InvalidServiceException $e) {
throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement %s\Loader\FileLoaderInterface '
. 'or %s\Loader\RemoteLoaderInterface',
is_object($plugin) ? get_class($plugin) : gettype($plugin),
__NAMESPACE__,
__NAMESPACE__
));
}
}
}