PhpGenerator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 22
c 3
b 0
f 0
dl 0
loc 52
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 8 1
A toString() 0 13 1
A generateMethod() 0 11 1
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->setExtends(Container::class);
60
        $class->setComment(<<<'COMMENT'
61
62
Main DependencyInjection Container. This class has been auto-generated
63
by the Nette Dependency Injection Component.
64
65
Automatically detects if "container" property are presented in class or uses
66
global container as fallback.
67
COMMENT);
68
69
        return parent::toString($class);
70
    }
71
72
    public function generateMethod(Definitions\Definition $def): Nette\PhpGenerator\Method
73
    {
74
        $method   = parent::generateMethod($def);
75
        $name     = $def->getName();
76
        $comment  = 'This service can be accessed by it\'s name in lower case,';
77
        $comment2 = "thus `%s`, using container get or make methods.\n\n@return %s";
78
79
        $method->setProtected();
80
        $method->setComment(\sprintf($comment . "\n" . $comment2, $name, $def->getType()));
81
82
        return $method;
83
    }
84
}
85