1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AloiaCms\Console; |
4
|
|
|
|
5
|
|
|
use AloiaCms\Models\Contracts\ModelInterface; |
6
|
|
|
use AloiaCms\Models\Traits\Postable; |
7
|
|
|
use AloiaCms\Models\Traits\Updatable; |
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
use Illuminate\Support\Facades\Config; |
10
|
|
|
use SitemapGenerator\SitemapGenerator; |
11
|
|
|
|
12
|
|
|
class SitemapCommand extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The name and signature of the console command. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $signature = 'aloiacms:sitemap'; |
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'Generate a sitemap for the specified models'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Execute the console command. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function handle() |
33
|
|
|
{ |
34
|
|
|
$models = Config::get('aloiacms.seo.sitemap', []); |
35
|
|
|
|
36
|
|
|
$app_url = Config::get('app.url', 'http://locahost:8000'); |
37
|
|
|
|
38
|
|
|
$sitemap_path = Config::get('aloiacms.seo.sitemap_path'); |
39
|
|
|
|
40
|
|
|
$generator = new SitemapGenerator($app_url); |
41
|
|
|
|
42
|
|
|
foreach ($models as $config) { |
43
|
|
|
|
44
|
|
|
// Determine whether we can use the built-in traits for fetching a "last modified at" date. |
45
|
|
|
$traits = class_uses_recursive($config['model']); |
46
|
|
|
$has_update_date = in_array(Updatable::class, $traits); |
47
|
|
|
$has_post_date = in_array(Postable::class, $traits); |
48
|
|
|
|
49
|
|
|
// Call the all() method to fetch all models of this type |
50
|
|
|
foreach (call_user_func([$config['model'], 'all']) as $model) { |
51
|
|
|
$generator->add( |
52
|
|
|
$this->replaceIdWithFilename($model, $config['path']), |
53
|
|
|
$config['priority'], |
54
|
|
|
$this->getLastModifiedAt($has_update_date, $has_post_date, $model), |
55
|
|
|
$config['change_frequency'], |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
file_put_contents($sitemap_path, $generator->toXML()); |
61
|
|
|
|
62
|
|
|
$relative_path = str_replace(base_path() . "/", "", $sitemap_path); |
63
|
|
|
|
64
|
|
|
$this->info("Create a sitemap at: {$relative_path}"); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the last modification date, defaults to today's date. |
69
|
|
|
* |
70
|
|
|
* @param bool $has_update_date |
71
|
|
|
* @param bool $has_post_date |
72
|
|
|
* @param mixed $model |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
private function getLastModifiedAt(bool $has_update_date, bool $has_post_date, mixed $model): string |
76
|
|
|
{ |
77
|
|
|
$last_modified_at = ""; |
78
|
|
|
|
79
|
|
|
if ($has_update_date && !is_null($model->getUpdateDate())) { |
80
|
|
|
$last_modified_at = $model->getUpdateDate()->format('Y-m-d'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($has_post_date && empty($last_modified_at) && !is_null($model->getPostDate())) { |
84
|
|
|
$last_modified_at = $model->getPostDate()->format('Y-m-d'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (!empty($last_modified_at)) { |
88
|
|
|
return $last_modified_at; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return date('Y-m-d'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Generate the URL for the given model and path |
96
|
|
|
* |
97
|
|
|
* @param ModelInterface $model |
98
|
|
|
* @param string $path |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function replaceIdWithFilename(ModelInterface $model, string $path): string |
102
|
|
|
{ |
103
|
|
|
return str_replace("{id}", $model->filename(), $path); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|