1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Biurad opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.2 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Biurad\DependencyInjection; |
19
|
|
|
|
20
|
|
|
use Nette; |
21
|
|
|
use Nette\DI\ContainerBuilder; |
22
|
|
|
use Nette\DI\Definitions; |
23
|
|
|
use Nette\DI\PhpGenerator as NettePhpGenerator; |
24
|
|
|
use Throwable; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Container PHP code generator. |
28
|
|
|
* |
29
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class PhpGenerator extends NettePhpGenerator |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Generates PHP classes. First class is the container. |
35
|
|
|
* |
36
|
|
|
* @param string $className |
37
|
|
|
* |
38
|
|
|
* @return Nette\PhpGenerator\ClassType |
39
|
|
|
*/ |
40
|
|
|
public function generate(string $className): Nette\PhpGenerator\ClassType |
41
|
|
|
{ |
42
|
|
|
$class = parent::generate($className); |
43
|
|
|
|
44
|
|
|
$class->getMethod(Container::getMethodName(ContainerBuilder::THIS_CONTAINER)) |
45
|
|
|
->addBody(' //container instance is binded to it self'); |
46
|
|
|
|
47
|
|
|
return $class; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Nette\PhpGenerator\ClassType $class |
52
|
|
|
* |
53
|
|
|
* @throws Throwable |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function toString(Nette\PhpGenerator\ClassType $class): string |
58
|
|
|
{ |
59
|
|
|
$class->setComment(<<<'COMMENT' |
60
|
|
|
/** |
61
|
|
|
* Main DependencyInjection Container. This class has been auto-generated |
62
|
|
|
* by the Nette Dependency Injection Component. |
63
|
|
|
* |
64
|
|
|
* Automatically detects if "container" property are presented in class or uses |
65
|
|
|
* global container as fallback. |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
|
|
COMMENT); |
69
|
|
|
|
70
|
|
|
return parent::toString($class); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function generateMethod(Definitions\Definition $def): Nette\PhpGenerator\Method |
74
|
|
|
{ |
75
|
|
|
$method = parent::generateMethod($def); |
76
|
|
|
$name = $def->getName(); |
77
|
|
|
$comment = 'This service can be accessed by it\'s name in lower case,'; |
78
|
|
|
$comment2 = "thus `%s`, using container get or make methods.\n\n@return %s"; |
79
|
|
|
|
80
|
|
|
$method->setProtected(); |
81
|
|
|
$method->setComment(\sprintf($comment . "\n" . $comment2, $name, $def->getType())); |
82
|
|
|
|
83
|
|
|
return $method; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|