Skip to content

Commit

Permalink
Fix when service is not an object
Browse files Browse the repository at this point in the history
  • Loading branch information
steevanb committed Jun 6, 2018
1 parent c2431d0 commit c149267
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,16 @@
<i class="text-small font-normal">No dependencies</i>
{% endif %}

<a
href="#"
class="sf-toggle text-small font-normal link-inverse"
data-toggle-selector="#container-introspection-fileName-{{ loop.index0 }}"
data-toggle-alt-content="Hide file name"
>View file name</a>
{% if serviceInfos.fileName is not null %}
<a
href="#"
class="sf-toggle text-small font-normal link-inverse"
data-toggle-selector="#container-introspection-fileName-{{ loop.index0 }}"
data-toggle-alt-content="Hide file name"
>View file name</a>
{% else %}
<i class="text-small font-normal">No file name</i>
{% endif %}

<div id="container-introspection-dependencies-{{ loop.index0 }}" class="sf-toggle-content hidden highlight">
<ul>
Expand All @@ -152,7 +156,16 @@
{{ serviceInfos.fileName }}
</div>
</td>
<td>{{ serviceInfos.fqcn }}</td>
<td>
{% if serviceInfos.fqcn is not null %}
{{ serviceInfos.fqcn }}
{% elseif serviceInfos.value is not null %}
<i>Not an object: </i>
{{ serviceInfos.value|raw }}
{% else %}
<i>Unknown value</i>
{% endif %}
</td>
<td>
{% if serviceInfos.ocramiusLazy %}
<span class="label status-warning">Ocramius Lazy</span>
Expand Down
46 changes: 30 additions & 16 deletions ContainerIntrospectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,40 @@ public function getInstantiatedServices(): array
$return = [];
foreach ($services as $id) {
$service = $this->container->get($id);
$ocramiusLazy = $service instanceof VirtualProxyInterface;
$className = ($ocramiusLazy) ? get_parent_class($service) : get_class($service);

$reflection = new \ReflectionClass($className);
$constructor = $reflection->getConstructor();
$dependencies = [];
if ($constructor instanceof \ReflectionMethod) {
foreach ($constructor->getParameters() as $parameter) {
$dependencies[] = [
'type' => $parameter->getType() instanceof \ReflectionNamedType
? $parameter->getType()->getName()
: null,
'name' => $parameter->getName()
];

if (is_object($service)) {
$ocramiusLazy = $service instanceof VirtualProxyInterface;
$className = ($ocramiusLazy) ? get_parent_class($service) : get_class($service);

$reflection = new \ReflectionClass($className);
$constructor = $reflection->getConstructor();
$dependencies = [];
if ($constructor instanceof \ReflectionMethod) {
foreach ($constructor->getParameters() as $parameter) {
$dependencies[] = [
'type' => $parameter->getType() instanceof \ReflectionNamedType
? $parameter->getType()->getName()
: null,
'name' => $parameter->getName()
];
}
}

$value = null;
$fqcn = $reflection->getName();
$fileName = $reflection->getFileName();
} else {
$value = var_export($service, true);
$fqcn = null;
$fileName = null;
$dependencies = [];
$ocramiusLazy = false;
}

$return[$id] = [
'fqcn' => $reflection->getName(),
'fileName' => $reflection->getFileName(),
'value' => $value,
'fqcn' => $fqcn,
'fileName' => $fileName,
'dependencies' => $dependencies,
'ocramiusLazy' => $ocramiusLazy
];
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![version](https://img.shields.io/badge/version-1.0.0-green.svg)](https://github.com/steevanb/symfony-container-introspection/tree/1.0.0)
[![version](https://img.shields.io/badge/version-1.0.1-green.svg)](https://github.com/steevanb/symfony-container-introspection/tree/1.0.1)
[![php](https://img.shields.io/badge/php-^7.1-blue.svg)](https://php.net)
[![symfony](https://img.shields.io/badge/symfony/dependency--injection-^3.4||^4.0-blue.svg)](https://symfony.com)
![Lines](https://img.shields.io/badge/code%20lines-734-green.svg)
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### [1.0.1](../../../compare/1.0.0...1.0.1) - 2018-06-06

- Fix when service is not an object

### 1.0.0 - 2018-06-06

- Get registered services: `ContainerIntrospectionService::getRegisteredServices()`
Expand Down

0 comments on commit c149267

Please sign in to comment.