Completed
Push — master ( 313ff4...885da7 )
by Jean-Christophe
02:12
created

UrlParser::parseUrl()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
rs 9.2
c 1
b 0
f 0
cc 4
eloc 13
nc 4
nop 2
1
<?php
2
3
namespace Ubiquity\seo;
4
5
use Ubiquity\cache\CacheManager;
6
use Ubiquity\utils\base\UIntrospection;
7
use Ubiquity\utils\base\UFileSystem;
8
use Ubiquity\controllers\Startup;
9
10
class UrlParser {
11
	public static $frequencies=[ 'always','hourly','daily','weekly','monthly','yearly','never' ];
12
	private $urls;
13
	private $config;
14
15
	public function __construct() {
16
		$this->urls=[];
17
		$this->config=Startup::getConfig();
18
	}
19
20
	public function parse() {
21
		$routes=CacheManager::getRoutes();
22
		foreach ( $routes as $path => $route ) {
23
			$url=$this->parseUrl($path, $route);
24
			if (isset($url)) {
25
				if(!isset($this->urls[$path]))
26
					$this->urls[$path]=$url;
27
			}
28
		}
29
	}
30
31
	public function parseArray($array,$existing=true){
32
		foreach ($array as $url){
33
			$this->urls[$url['location']]=Url::fromArray($url,$existing);
34
		}
35
	}
36
37
	protected function parseUrl($path, $route) {
38
		if (isset($route["controller"])) {
39
			$controller=$route["controller"];
40
			$action=$route["action"];
41
		} elseif (isset($route["get"])) {
42
			return $this->parseUrl($path,$route["get"]);
43
		} else {
44
			return;
45
		}
46
		$lastModified=self::getLastModified($controller, $action);
47
		if($lastModified!==false){
48
			$url=new Url($path, $lastModified);
49
			return $url;
50
		}
51
		return;
52
	}
53
54
	public static function getLastModified($controller, $action) {
55
		if(\class_exists($controller)){
56
			$classCode=UIntrospection::getClassCode($controller);
57
			$lastModified=UFileSystem::lastModified(UIntrospection::getFileName($controller));
58
			if(\is_array($classCode)){
59
				$reflexAction=new \ReflectionMethod($controller.'::'.$action);
60
				$views=UIntrospection::getLoadedViews($reflexAction, $classCode);
61
				foreach ( $views as $view ) {
62
					$file=ROOT . DS . "views" . DS . $view;
63
					$viewDate=UFileSystem::lastModified($file);
64
					if ($viewDate > $lastModified)
65
						$lastModified=$viewDate;
66
				}
67
			}
68
			return $lastModified;
69
		}
70
		return false;
71
	}
72
	/**
73
	 * @return multitype:
0 ignored issues
show
Documentation introduced by
The doc-type multitype: could not be parsed: Unknown type name "multitype:" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
74
	 */
75
	public function getUrls() {
76
		return $this->urls;
77
	}
78
79
}
80
81