-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathArgument.php
executable file
·110 lines (93 loc) · 2.78 KB
/
Argument.php
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
<?php
/**
* Webiny Framework (http://www.webiny.com/framework)
*
* @copyright Copyright Webiny LTD
*/
namespace Webiny\Component\ServiceManager;
use Webiny\Component\Config\ConfigObject;
use Webiny\Component\StdLib\StdLibTrait;
/**
* Argument class serves as a wrapper for service arguments.<br />
* It contains the value from config file and knows how to get the real value of it, like including other services,
* instantiating classes, etc.
*
* @package Webiny\Component\ServiceManager
*/
class Argument
{
use StdLibTrait;
/**
* Simple value, class name or service name
*/
private $value;
/**
* Create Argument instance
*
* @param mixed $argument Value to wrap into Argument instance
*/
public function __construct($argument)
{
$this->value = $argument;
}
/**
* Get real Argument value
* @return mixed
*/
public function value()
{
/**
* If 'object' key exists - it's either a class or service
**/
if ($this->isArray($this->value) && array_key_exists('Object', $this->value)) {
$this->value = $this->arr($this->value);
$objectArguments = $this->value->key('ObjectArguments', [], true);
$this->value = $this->createValue($this->value->key('Object'), $this->parseArguments($objectArguments));
} else {
$this->value = $this->createValue($this->value);
}
return $this->value;
}
private function parseArguments($arguments)
{
$values = [];
foreach ($arguments as $arg) {
$argument = new Argument($arg);
$values[] = $argument->value();
}
return $values;
}
/**
* Create proper argument value
*
* @param mixed $object
* @param array $arguments
*
* @throws ServiceManagerException
*
* @return mixed|object
*/
private function createValue($object, $arguments = [])
{
if ($this->isInstanceOf($arguments, ConfigObject::class)) {
$arguments = $arguments->toArray();
}
if (!$this->isArray($arguments)) {
throw new ServiceManagerException(ServiceManagerException::INVALID_SERVICE_ARGUMENTS_TYPE, [$object]);
}
if (!$this->isString($object)) {
return $object;
}
$object = $this->str($object);
if ($object->startsWith('@')) {
return ServiceManager::getInstance()->getService($object->trimLeft('@')->val());
} else {
$value = $object->val();
if (class_exists($value)) {
$reflection = new \ReflectionClass($value);
return $reflection->newInstanceArgs($arguments);
}
return $value;
}
}
}