ReaderPluginManager.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Config;
  10. use Interop\Container\ContainerInterface;
  11. use Zend\ServiceManager\AbstractPluginManager;
  12. use Zend\ServiceManager\Exception\InvalidServiceException;
  13. use Zend\ServiceManager\Factory\InvokableFactory;
  14. class ReaderPluginManager extends AbstractPluginManager
  15. {
  16. protected $instanceOf = Reader\ReaderInterface::class;
  17. protected $aliases = [
  18. 'ini' => Reader\Ini::class,
  19. 'Ini' => Reader\Ini::class,
  20. 'json' => Reader\Json::class,
  21. 'Json' => Reader\Json::class,
  22. 'xml' => Reader\Xml::class,
  23. 'Xml' => Reader\Xml::class,
  24. 'yaml' => Reader\Yaml::class,
  25. 'Yaml' => Reader\Yaml::class,
  26. 'javaproperties' => Reader\JavaProperties::class,
  27. 'javaProperties' => Reader\JavaProperties::class,
  28. 'JavaProperties' => Reader\JavaProperties::class,
  29. ];
  30. protected $factories = [
  31. Reader\Ini::class => InvokableFactory::class,
  32. Reader\Json::class => InvokableFactory::class,
  33. Reader\Xml::class => InvokableFactory::class,
  34. Reader\Yaml::class => InvokableFactory::class,
  35. Reader\JavaProperties::class => InvokableFactory::class,
  36. // Legacy (v2) due to alias resolution; canonical form of resolved
  37. // alias is used to look up the factory, while the non-normalized
  38. // resolved alias is used as the requested name passed to the factory.
  39. 'zendconfigreaderini' => InvokableFactory::class,
  40. 'zendconfigreaderjson' => InvokableFactory::class,
  41. 'zendconfigreaderxml' => InvokableFactory::class,
  42. 'zendconfigreaderyaml' => InvokableFactory::class,
  43. 'zendconfigreaderjavaproperties' => InvokableFactory::class,
  44. ];
  45. /**
  46. * Validate the plugin is of the expected type (v3).
  47. *
  48. * Validates against `$instanceOf`.
  49. *
  50. * @param mixed $instance
  51. * @throws InvalidServiceException
  52. */
  53. public function validate($instance)
  54. {
  55. if (! $instance instanceof $this->instanceOf) {
  56. throw new InvalidServiceException(sprintf(
  57. '%s can only create instances of %s; %s is invalid',
  58. get_class($this),
  59. $this->instanceOf,
  60. (is_object($instance) ? get_class($instance) : gettype($instance))
  61. ));
  62. }
  63. }
  64. /**
  65. * Validate the plugin is of the expected type (v2).
  66. *
  67. * Proxies to `validate()`.
  68. *
  69. * @param mixed $instance
  70. * @throws Exception\InvalidArgumentException
  71. */
  72. public function validatePlugin($instance)
  73. {
  74. try {
  75. $this->validate($instance);
  76. } catch (InvalidServiceException $e) {
  77. throw new Exception\InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  78. }
  79. }
  80. public function __construct(ContainerInterface $container, array $config = [])
  81. {
  82. $config = array_merge_recursive(['aliases' => $this->aliases], $config);
  83. parent::__construct($container, $config);
  84. }
  85. }