|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SandFoxMe\PhpStorm\Metadata\Containers\Zend; |
| 4 | + |
| 5 | +use SandFoxMe\PhpStorm\Metadata\Common\Helpers\TypeStrings; |
| 6 | +use SandFoxMe\PhpStorm\Metadata\Containers\ContainerIterator; |
| 7 | +use Zend\ServiceManager\ServiceManager; |
| 8 | + |
| 9 | +class ServiceManagerIterator implements ContainerIterator |
| 10 | +{ |
| 11 | + const DEFAULT_OPTIONS = [ |
| 12 | + 'overrides' => [ |
| 13 | + '\\Psr\\Container\\ContainerInterface::get(0)', |
| 14 | + '\\Zend\\ServiceManager\\ServiceLocatorInterface::get(0)', |
| 15 | + ], |
| 16 | + ]; |
| 17 | + |
| 18 | + private $serviceManager; |
| 19 | + |
| 20 | + public function __construct(ServiceManager $serviceManager) |
| 21 | + { |
| 22 | + $this->serviceManager = $serviceManager; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @return \Traversable Iterator for all container items $key => $entry |
| 27 | + */ |
| 28 | + public function getIterator(): \Traversable |
| 29 | + { |
| 30 | + $keysFunc = function () { |
| 31 | + $keys = array_unique(array_merge( |
| 32 | + array_keys($this->aliases), |
| 33 | + array_keys($this->services), |
| 34 | + array_keys($this->factories) |
| 35 | + )); |
| 36 | + |
| 37 | + return $keys; |
| 38 | + }; |
| 39 | + |
| 40 | + $keys = ($keysFunc->bindTo($this->serviceManager, $this->serviceManager))(); |
| 41 | + |
| 42 | + foreach ($keys as $key) { |
| 43 | + try { |
| 44 | + yield $key => TypeStrings::getTypeStringByInstance($this->serviceManager->get($key)); |
| 45 | + } catch (\Throwable $exception) { |
| 46 | + yield $key => TypeStrings::getTypeStringByInstance($exception) . |
| 47 | + ' /* Error message: "' . $exception->getMessage() . '" */'; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments