1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Model\Front\Sitemap; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\App; |
6
|
|
|
use Ffcms\Core\Arch\Model; |
7
|
|
|
use Ffcms\Core\Exception\SyntaxException; |
8
|
|
|
use Ffcms\Core\Helper\Date; |
9
|
|
|
use Ffcms\Core\Helper\FileSystem\Directory; |
10
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
11
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
12
|
|
|
use Ffcms\Core\Helper\Type\Str; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class EntityIndexList. List sitemap files in special directory |
16
|
|
|
* @package Apps\Model\Front\Sitemap |
17
|
|
|
*/ |
18
|
|
|
class EntityIndexList extends Model |
19
|
|
|
{ |
20
|
|
|
const INDEX_PATH = '/upload/sitemap'; |
21
|
|
|
|
22
|
|
|
public $files; |
23
|
|
|
private $info; |
24
|
|
|
|
25
|
|
|
private $_lang; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* EntityIndexList constructor. Pass current language from controller request |
29
|
|
|
* @param bool $currentLang |
30
|
|
|
*/ |
31
|
|
|
public function __construct($currentLang) |
32
|
|
|
{ |
33
|
|
|
$this->_lang = $currentLang; |
34
|
|
|
parent::__construct(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Try to find sitemap indexes in storage directory |
39
|
|
|
* @throws SyntaxException |
40
|
|
|
*/ |
41
|
|
|
public function before() |
42
|
|
|
{ |
43
|
|
|
if (!Directory::exist(static::INDEX_PATH)) { |
44
|
|
|
throw new SyntaxException(__('Directory %dir% for sitemaps is not exists', ['dir' => static::INDEX_PATH])); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$scan = File::listFiles(static::INDEX_PATH, ['.xml'], true); |
48
|
|
|
if (Obj::isArray($scan)) { |
49
|
|
|
foreach ($scan as $file) { |
50
|
|
|
if (!Str::contains('.' . $this->_lang, $file)) { |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
$this->files[] = static::INDEX_PATH . '/' . $file; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Build sitemap index files information - location, last modify time |
60
|
|
|
*/ |
61
|
|
|
public function make() |
62
|
|
|
{ |
63
|
|
|
if (!Obj::isArray($this->files)) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// build file information data |
68
|
|
|
foreach ($this->files as $file) { |
69
|
|
|
$this->info[] = [ |
70
|
|
|
'loc' => App::$Alias->scriptUrl . $file, |
71
|
|
|
'lastmod' => Date::convertToDatetime(File::mTime($file), 'c') |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get sitemap index files info as array |
78
|
|
|
* @return array|null |
79
|
|
|
*/ |
80
|
|
|
public function getInfo() |
81
|
|
|
{ |
82
|
|
|
return $this->info; |
83
|
|
|
} |
84
|
|
|
} |