1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Lookyman\Nette\Proxy\DI; |
5
|
|
|
|
6
|
|
|
use Lookyman\Nette\Proxy\Console\GenerateProxiesCommand; |
7
|
|
|
use Nette\DI\CompilerExtension; |
8
|
|
|
use Nette\DI\Container; |
9
|
|
|
use Nette\DI\Helpers; |
10
|
|
|
use Nette\DI\Statement; |
11
|
|
|
use Nette\PhpGenerator\ClassType; |
12
|
|
|
use Nette\PhpGenerator\Method; |
13
|
|
|
use Nette\Utils\Validators; |
14
|
|
|
use ProxyManager\Configuration; |
15
|
|
|
use ProxyManager\Factory\LazyLoadingValueHolderFactory; |
16
|
|
|
use ProxyManager\FileLocator\FileLocator; |
17
|
|
|
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy; |
18
|
|
|
|
19
|
|
|
class ProxyExtension extends CompilerExtension |
20
|
|
|
{ |
21
|
|
|
const TAG_LAZY = 'lookyman.lazy'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $defaults = [ |
27
|
|
|
'proxyDir' => '%appDir%/../temp/proxies', |
28
|
|
|
'default' => false, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $excluded = [ |
35
|
|
|
'Nette\ComponentModel\Component', |
36
|
|
|
'Nette\Database\Connection', |
37
|
|
|
'Nette\Http\Request', |
38
|
|
|
'Nette\Security\User', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
public function loadConfiguration() |
42
|
|
|
{ |
43
|
|
|
$builder = $this->getContainerBuilder(); |
44
|
|
|
$config = $this->validateConfig($this->defaults); |
45
|
|
|
Validators::assertField($config, 'default', 'bool'); |
46
|
|
|
|
47
|
|
|
// create proxy dir |
48
|
|
|
Validators::assertField($config, 'proxyDir', 'string'); |
49
|
|
|
$config['proxyDir'] = Helpers::expand($config['proxyDir'], $builder->parameters); |
50
|
|
|
if (!@mkdir($config['proxyDir'], 0777, true) && !is_dir($config['proxyDir'])) { |
51
|
|
|
// @codeCoverageIgnoreStart |
52
|
|
|
throw new \RuntimeException(sprintf('Cannot create proxy directory %s', $config['proxyDir'])); |
53
|
|
|
// @codeCoverageIgnoreEnd |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// generator strategy |
57
|
|
|
$builder->addDefinition($this->prefix('generatorStrategy')) |
58
|
|
|
->setClass(FileWriterGeneratorStrategy::class, [new Statement(FileLocator::class, [$config['proxyDir']])]) |
59
|
|
|
->setAutowired(false) |
60
|
|
|
->addTag(self::TAG_LAZY, false); |
61
|
|
|
|
62
|
|
|
// configuration |
63
|
|
|
$builder->addDefinition($this->prefix('configuration')) |
64
|
|
|
->setClass(Configuration::class) |
65
|
|
|
->addSetup('setProxiesTargetDir', [$config['proxyDir']]) |
66
|
|
|
->addSetup('setGeneratorStrategy', [$this->prefix('@generatorStrategy')]) |
67
|
|
|
->setAutowired(false) |
68
|
|
|
->addTag(self::TAG_LAZY, false); |
69
|
|
|
|
70
|
|
|
// proxy factory |
71
|
|
|
$builder->addDefinition($this->prefix('lazyLoadingValueHolderFactory')) |
72
|
|
|
->setClass(LazyLoadingValueHolderFactory::class, [$this->prefix('@configuration')]) |
73
|
|
|
->setAutowired(false) |
74
|
|
|
->addTag(self::TAG_LAZY, false); |
75
|
|
|
|
76
|
|
|
// command |
77
|
|
|
/** @var \Kdyby\Console\DI\ConsoleExtension $extension */ |
78
|
|
|
foreach ($this->compiler->getExtensions('Kdyby\Console\DI\ConsoleExtension') as $extension) { |
79
|
|
|
$builder->addDefinition($this->prefix('generateProxiesCommand')) |
80
|
|
|
->setClass(GenerateProxiesCommand::class) |
81
|
|
|
->addTag($extension::TAG_COMMAND); |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function beforeCompile() |
87
|
|
|
{ |
88
|
|
|
$builder = $this->getContainerBuilder(); |
89
|
|
|
$config = $this->getConfig(); |
90
|
|
|
|
91
|
|
|
// do not proxy these services |
92
|
|
|
// @see https://ocramius.github.io/ProxyManager/docs/lazy-loading-value-holder.html#known-limitations |
93
|
|
|
foreach ($this->excluded as $type) { |
94
|
|
|
foreach ($builder->findByType($type) as $def) { |
95
|
|
|
$def->addTag(self::TAG_LAZY, false); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// add service type as tag attribute |
100
|
|
|
foreach (array_keys($config['default'] ? $builder->getDefinitions() : $builder->findByTag(self::TAG_LAZY)) as $name) { |
101
|
|
|
$def = $builder->getDefinition($name); |
102
|
|
|
if ($def->getTag(self::TAG_LAZY) === false) { |
103
|
|
|
$def->setTags(array_diff_key($def->getTags(), [self::TAG_LAZY => null])); |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
$def->addTag(self::TAG_LAZY, $def->getImplement() ?: $def->getClass()); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param ClassType $class |
112
|
|
|
*/ |
113
|
|
|
public function afterCompile(ClassType $class) |
114
|
|
|
{ |
115
|
|
|
foreach ($this->getContainerBuilder()->findByTag(self::TAG_LAZY) as $name => $type) { |
116
|
|
|
// modify original method body to return proxy instead |
117
|
|
|
$method = $class->getMethod(Container::getMethodName($name)); |
118
|
|
|
$method->setBody(sprintf( |
119
|
|
|
"return \$this->getService('%s')->createProxy(\n\t%s::class,\n\tfunction (&\$wrappedObject, \$proxy, \$method, \$parameters, &\$initializer) {\n\t\t\$wrappedObject = (%s)();\n\t\t\$initializer = null;\n\t}\n);", |
120
|
|
|
$this->prefix('lazyLoadingValueHolderFactory'), $type, ltrim(preg_replace('#^#m', "\t\t", (new Method())->addBody($method->getBody()))) |
121
|
|
|
)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// register proxy autoloader |
125
|
|
|
$init = $class->getMethod('initialize'); |
126
|
|
|
$body = $init->getBody(); |
127
|
|
|
$init->setBody("spl_autoload_register(\$this->getService(?)->getProxyAutoloader());\n", [$this->prefix('configuration')])->addBody($body); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: