Skip to content

Commit a84ba4d

Browse files
committed
Zend service manager iterator
1 parent 17d77d0 commit a84ba4d

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

src/Generator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use SandFoxMe\PhpStorm\Metadata\Common\Metadata;
77
use SandFoxMe\PhpStorm\Metadata\Containers\Pimple\PimpleIterator;
88
use SandFoxMe\PhpStorm\Metadata\Containers\StaticMap\StaticMapIterator;
9+
use SandFoxMe\PhpStorm\Metadata\Containers\Zend\ServiceManagerIterator;
10+
use Zend\ServiceManager\ServiceManager;
911

1012
abstract class Generator
1113
{
@@ -75,6 +77,10 @@ private static function getIteratorClass($container): string
7577
return PimpleIterator::class;
7678
}
7779

80+
if ($container instanceof ServiceManager) {
81+
return ServiceManagerIterator::class;
82+
}
83+
7884
throw new \RuntimeException('Unsupported container');
7985
}
8086
}

0 commit comments

Comments
 (0)