Completed
Push — master ( c6d651...f3cc34 )
by Pavel
02:14
created

src/DI/ApiRouterExtension.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2016 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\ApiRouter\DI;
10
11
use Ublaboo\ApiRouter\ApiRoute;
12
use Nette\Reflection\ClassType;
13
use Nette;
14
use Doctrine\Common\Annotations\AnnotationReader;
15
use Doctrine\Common\Annotations\AnnotationRegistry;
16
use Doctrine\Common\Annotations\CachedReader;
17
use Doctrine\Common\Cache\FilesystemCache;
18
19
class ApiRouterExtension extends Nette\DI\CompilerExtension
20
{
21
22
	/**
23
	 * @var array
24
	 */
25
	private $defaults = [
26
		'ignoreAnnotation' => []
27
	];
28
29
30
	/**
31
	 * @return void
32
	 */
33
	public function beforeCompile()
34
	{
35
		$config = $this->_getConfig();
36
37
		$builder = $this->getContainerBuilder();
38
		$compiler_config = $this->compiler->getConfig();
39
40
		$routes = $this->findRoutes($builder, $compiler_config, $config);
41
42
		$builder->addDefinition($this->prefix('resolver'))
43
			->setClass('Ublaboo\ApiRouter\DI\ApiRoutesResolver')
44
			->addSetup('prepandRoutes', [$builder->getDefinition('router'), $routes])
45
			->addTag('run');
46
	}
47
48
49
	/**
50
	 * @param  Nette\DI\ContainerBuilder $builder
51
	 * @param  array $compiler_config
52
	 * @param  array $config
53
	 * @return array
54
	 */
55
	private function findRoutes(Nette\DI\ContainerBuilder $builder, $compiler_config, $config)
56
	{
57
		/**
58
		 * Prepare AnnotationRegistry
59
		 */
60
		AnnotationRegistry::registerFile(__DIR__ . '/../ApiRoute.php');
61
		AnnotationRegistry::registerFile(__DIR__ . '/../ApiRouteSpec.php');
62
63
		AnnotationReader::addGlobalIgnoredName('persistent');
64
		AnnotationReader::addGlobalIgnoredName('inject');
65
66
		foreach ($config['ignoreAnnotation'] as $ignore) {
67
			AnnotationReader::addGlobalIgnoredName($ignore);
68
		}
69
70
		$cache_path = $compiler_config['parameters']['tempDir'] . '/cache/ApiRouter.Annotations';
71
72
		/**
73
		 * Prepare AnnotationReader - use cached values
74
		 */
75
		$reader = new CachedReader(
76
			new AnnotationReader,
77
			new FilesystemCache($cache_path),
78
			$debug = $compiler_config['parameters']['debugMode']
79
		);
80
81
		/**
82
		 * Find all presenters and their routes
83
		 */
84
		$presenter = $builder->findByTag('nette.presenter');
85
		$routes = [];
86
87
		foreach ($presenter as $presenter) {
88
			$r = ClassType::from($presenter);
89
90
			$route = $reader->getClassAnnotation($r, ApiRoute::class);
0 ignored issues
show
Are you sure the assignment to $route is correct as $reader->getClassAnnotat...Router\ApiRoute::class) (which targets Doctrine\Common\Annotati...r::getClassAnnotation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
91
92
			if ($route) {
93
				/**
94
				 * Add route to priority-sorted list
95
				 */
96
				if (empty($routes[$route->getPriority()])) {
97
					$routes[$route->getPriority()] = [];
98
				}
99
100
				$route->setDescription($r->getAnnotation('description'));
101
102
				if (!$route->getPresenter()) {
103
					$route->setPresenter(preg_replace('/Presenter$/', '', $r->getShortName()));
104
				}
105
106
				/**
107
				 * Find apropriate methods
108
				 */
109
				foreach ($r->getMethods() as $method_r) {
110
					$route_action = $reader->getMethodAnnotation($method_r, ApiRoute::class);
111
112
					/**
113
					 * Get action without that ^action string
114
					 */
115
					$action = lcfirst(preg_replace('/^action/', '', $method_r->name));
116
117
					/**
118
					 * Route can be defined also for perticular action
119
					 */
120
					if ($route_action) {
121
						if ($method_r instanceof Nette\Reflection\Method) {
122
							$route_action->setDescription($method_r->getAnnotation('description'));
123
						}
124
125
						/**
126
						 * Action route will inherit presenter name nad priority from parent route
127
						 */
128
						if (!$route_action->getPresenter()) {
129
							$route_action->setPresenter($route->getPresenter());
130
						}
131
132
						if (!$route_action->getPriority()) {
133
							$route_action->setPriority($route->getPriority());
134
						}
135
136
						if (!$route_action->getFormat()) {
137
							$route_action->setFormat($route->getFormat());
138
						}
139
140
						if (!$route_action->getSection()) {
141
							$route_action->setSection($route->getSection());
142
						}
143
144
						if ($route_action->getMethod()) {
145
							$route_action->setAction($action, $route_action->getMethod());
146
						} else {
147
							$route_action->setAction($action);
148
						}
149
150
						$routes[$route->getPriority()][] = $route_action;
151
					} else {
152
						$route->setAction($action);
153
					}
154
				}
155
156
				$routes[$route->getPriority()][] = $route;
157
			}
158
		}
159
160
		/**
161
		 * Return routes sorted by priority
162
		 */
163
		$return = [];
164
165
		foreach ($routes as $priority => $priority_routes) {
166
			foreach ($priority_routes as $route) {
167
				$return[] = $route;
168
			}
169
		}
170
171
		return $return;
172
	}
173
174
175
	/**
176
	 * @return array
177
	 */
178
	private function _getConfig()
179
	{
180
		$config = $this->validateConfig($this->defaults, $this->config);
181
182
		if (!is_array($config['ignoreAnnotation'])) {
183
			$config['ignoreAnnotation'] = [$config['ignoreAnnotation']];
184
		}
185
186
		return (array) $config;
187
	}
188
189
}
190