Test Failed
Push — master ( c1ca68...61d842 )
by Julien
04:49
created

InjectableTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setDI() 0 3 1
A __get() 0 22 4
A getDI() 0 7 2
A __isset() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Di;
13
14
use Phalcon\Di;
15
use Phalcon\Di\DiInterface;
16
use Zemit\Bootstrap;
17
use Zemit\Cli\Router as CliRouter;
18
use Zemit\MVc\Router as MvcRouter;
19
use Zemit\Db\Profiler;
20
use Zemit\Debug;
21
use Zemit\Escaper;
22
use Zemit\Filter;
23
use Zemit\Http\Request;
24
use Zemit\Identity;
25
use Zemit\Locale;
26
use Zemit\Mvc\Dispatcher;
27
use Zemit\Provider\Jwt\Jwt;
28
use Zemit\Security;
29
use Zemit\Tag;
30
use Zemit\Utils;
31
use Phalcon\Logger;
32
use joshtronic\LoremIpsum;
33
use Orhanerday\OpenAi\OpenAi;
34
35
/**
36
 * @property \Phalcon\Mvc\Dispatcher|\Phalcon\Mvc\DispatcherInterface $dispatcher
37
 * @property \Phalcon\Mvc\Router|\Phalcon\Mvc\RouterInterface $router
38
 * @property \Phalcon\Url|\Phalcon\Url\UrlInterface $url
39
 * @property \Phalcon\Http\Request|\Phalcon\Http\RequestInterface $request
40
 * @property \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface $response
41
 * @property \Phalcon\Http\Response\Cookies|\Phalcon\Http\Response\CookiesInterface $cookies
42
 * @property \Phalcon\Filter $filter
43
 * @property \Phalcon\Flash\Direct $flash
44
 * @property \Phalcon\Flash\Session $flashSession
45
 * @property \Phalcon\Session\ManagerInterface $session
46
 * @property \Phalcon\Events\Manager|\Phalcon\Events\ManagerInterface $eventsManager
47
 * @property \Phalcon\Db\Adapter\AdapterInterface $db
48
 * @property \Phalcon\Security $security
49
 * @property \Phalcon\Crypt|\Phalcon\CryptInterface $crypt
50
 * @property \Phalcon\Tag $tag
51
 * @property \Phalcon\Escaper|\Phalcon\Escaper\EscaperInterface $escaper
52
 * @property \Phalcon\Annotations\Adapter\Memory|\Phalcon\Annotations\Adapter $annotations
53
 * @property \Phalcon\Mvc\Model\Manager|\Phalcon\Mvc\Model\ManagerInterface $modelsManager
54
 * @property \Phalcon\Mvc\Model\MetaData\Memory|\Phalcon\Mvc\Model\MetadataInterface $modelsMetadata
55
 * @property \Phalcon\Mvc\Model\Transaction\Manager|\Phalcon\Mvc\Model\Transaction\ManagerInterface $transactionManager
56
 * @property \Phalcon\Assets\Manager $assets
57
 * @property \Phalcon\Di|\Phalcon\Di\DiInterface $di
58
 * @property \Phalcon\Session\Bag|\Phalcon\Session\BagInterface $persistent
59
 * @property \Phalcon\Mvc\View|\Phalcon\Mvc\ViewInterface $view
60
 *
61
 * @property Bootstrap\Config $config
62
 * @property CliRouter|MvcRouter $router
63
 * @property Bootstrap $bootstrap
64
 * @property Debug $debug
65
 * @property Escaper $escaper
66
 * @property Filter $filter
67
 * @property Request $request
68
 * @property Identity $identity
69
 * @property Locale $locale
70
 * @property Dispatcher $dispatcher
71
 * @property Security $security
72
 * @property Tag $tag
73
 * @property Utils $utils
74
 * @property Profiler $profiler
75
 * @property Logger $logger
76
 * @property Jwt $jwt
77
 * @property OpenAi $openAi
78
 * @property LoremIpsum $loremIpsum
79
 */
80
trait InjectableTrait
81
{
82
    public ?DiInterface $container;
83
    
84
    public function __get(string $name)
85
    {
86
        $container = $this->getDI();
87
        
88
        if ($name === 'di') {
89
            $this->{'di'} = $container;
90
            return $container;
91
        }
92
        
93
        if ($name === 'persistent') {
94
            $persistent = $container->get('sessionBag', [get_class($this)]);
95
            $this->{'persistent'} = $persistent;
96
            return $persistent;
97
        }
98
        
99
        if ($container->has($name)) {
100
            $service = $container->getShared($name);
101
            $this->{$name} = $service;
102
            return $service;
103
        }
104
        
105
        trigger_error('Access to undefined property `' . $name . '`');
106
    }
107
    
108
    public function __isset(string $name): bool
109
    {
110
        return $this->getDI()->has($name);
111
    }
112
    
113
    public function getDI(): DiInterface
114
    {
115
        if (!isset($this->container)) {
116
            $this->container = Di::getDefault();
117
        }
118
        
119
        return $this->container;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->container could return the type null which is incompatible with the type-hinted return Phalcon\Di\DiInterface. Consider adding an additional type-check to rule them out.
Loading history...
120
    }
121
    
122
    public function setDI(DiInterface $container)
123
    {
124
        $this->container = $container;
125
    }
126
}
127