Test Failed
Push — master ( a0e5bb...bfd6ab )
by Php Easy Api
04:23
created

RouteProvider::fireEvent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 2
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Router;
4
5
use Closure;
6
use Resta\Traits\NamespaceForRoute;
7
use Resta\Container\DIContainerManager;
8
use Resta\Foundation\ApplicationProvider;
9
use Resta\Foundation\PathManager\StaticPathModel;
10
11
class RouteProvider extends ApplicationProvider
12
{
13
    //get namespace for route
14
    use NamespaceForRoute;
0 ignored issues
show
introduced by
The trait Resta\Traits\NamespaceForRoute requires some properties which are not provided by Resta\Router\RouteProvider: $urlComponent, $routeParameters
Loading history...
15
16
    /**
17
     * resolving event fire for response
18
     *
19
     * @param null $event
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $event is correct as it would always require null to be passed?
Loading history...
20
     * @param bool $return
21
     * @return mixed|void
22
     */
23
    private function fireEvent($event=null,$return=false)
24
    {
25
        // if an array of response-events is registered
26
        // in the container system, the event will fire.
27
        if($this->app->has('response-event.'.$event)){
28
            $event = $this->app->get('response-event.'.$event);
29
30
            // the event to be fired must be
31
            // a closure instance.
32
            if($event instanceof Closure){
33
                $eventResolved = $event($this->app);
34
35
                if($return){
36
                    return $eventResolved;
37
                }
38
            }
39
        }
40
    }
41
42
    /**
43
     * call controller for routing
44
     *
45
     * @return mixed
46
     *
47
     * @throws \DI\DependencyException
48
     * @throws \DI\NotFoundException
49
     */
50
    private function callController()
51
    {
52
        $this->fireEvent('before',true);
53
54
        if($this->app->has('output')){
55
            return $this->app->get('output');
56
        }
57
58
        if(is_array($event = $this->fireEvent('output',true))){
59
            $controller = $event;
60
        }
61
        else{
62
63
            //the singleton eager class is a class built to temporarily prevent
64
            //the use of user-side kernel objects used by the rest system.
65
            //Objects in this class are destroyed when their work is finished.
66
            $this->singletonEagerForRoute();
67
68
            //call service together with controller method
69
            $controller = $this->getCallBindController();
70
        }
71
72
        $this->app->register('output',$controller);
73
74
        $this->fireEvent('after',true);
75
76
        return $controller;
77
    }
78
79
    /**
80
     * get call bind controller
81
     *
82
     * @return mixed
83
     */
84
    private function getCallBindController()
85
    {
86
        //we finally process the method of the class invoked by the user as a process and prepare it for the response
87
        return app()->resolve(RouteWatch::class)->watch(function(){
88
89
            // if the method in the instance object exists,
90
            // this method is executed to produce the output.
91
            if(method_exists($this->app['instanceController'],$this->app['method'])){
92
                return $this->app['di']($this->app['instanceController'],$this->app['method']);
93
            }
94
95
            //throw exception as unsuccessful
96
            exception()->badMethodCall('The name of the method to be executed does not exist in the object.');
97
        });
98
    }
99
100
    /**
101
     * route application handle
102
     *
103
     * @return mixed
104
     *
105
     * @throws \DI\DependencyException
106
     * @throws \DI\NotFoundException
107
     */
108
    public function handle()
109
    {
110
        $this->app->register('routerResult',$this->callController());
111
112
        //we call our services as controller
113
        return $this->app['routerResult'];
114
    }
115
116
    /**
117
     * get route
118
     *
119
     * @throws \DI\DependencyException
120
     * @throws \DI\NotFoundException
121
     */
122
    public function route()
123
    {
124
        $namespace = $this->getControllerNamespace();
125
126
        //utils make bind via dependency injection named as service container
127
        $this->app->register('serviceConf',$this->app['fileSystem']->callFile(StaticPathModel::getServiceConf()));
128
        $this->app->register('instanceController',$this->app->resolve($namespace));
129
    }
130
131
    /**
132
     * route service configuration
133
     *
134
     * @param $parameters
135
     * @return void
136
     */
137
    public function routeServiceConfiguration($parameters)
138
    {
139
        // we record the route parameter with
140
        // the controller method in the serviceConf variable in the kernel..
141
        $method = strtolower($this->app['urlComponent']['method']);
142
143
        // based on the serviceConf variable,
144
        // we are doing parameter bindings in the method context in the routeParameters array key.
145
        $this->app->register('serviceConf','routeParameters',[$method=>$parameters]);
146
147
    }
148
149
    /**
150
     * set method name via define
151
     *
152
     * @param $methodName
153
     * @return void|mixed
154
     */
155
    public function setMethodNameViaDefine($methodName)
156
    {
157
        define('methodName',strtolower($methodName));
158
    }
159
160
161
    /**
162
     * singleton eager for route
163
     *
164
     * @throws \DI\DependencyException
165
     * @throws \DI\NotFoundException
166
     */
167
    private function singletonEagerForRoute()
168
    {
169
        //the singleton eager class is a class built to temporarily prevent
170
        //the use of user-side kernel objects used by the rest system.
171
        //Objects in this class are destroyed when their work is finished.
172
        $this->route();
173
174
        //we update the existing route parameter to make a new assignment on
175
        //the kernel object to extract the method name from the original route parameters.
176
        $this->substractMethodNameFromRouteParameters($this->checkIfExistsMethod($this));
177
    }
178
179
    /**
180
     * @param $method
181
     * @return void
182
     */
183
    public function substractMethodNameFromRouteParameters($method)
184
    {
185
        $fromRoutes = Route::getRouteResolve();
186
        $method = $fromRoutes['method'] ?? $method;
187
188
        $this->app->register('method',$method);
189
        $this->app->register('routeParameters', $this->routeParametersAssign($this->resolveMethod($method)));
190
191
    }
192
}