6
6
use GuzzleHttp \Exception \GuzzleException ;
7
7
use Illuminate \Console \Command ;
8
8
use Illuminate \Support \Facades \Config ;
9
- use Illuminate \Support \Facades \Log ;
10
9
use Illuminate \Support \Facades \Route ;
11
- use Illuminate \Support \Facades \Storage ;
12
- use Spatie \ Crawler \ Crawler ;
10
+ Use Illuminate \Support \Facades \Storage ;
11
+ use Illuminate \ Support \ Facades \ Log ;
13
12
use Spatie \Sitemap \SitemapGenerator ;
14
- use Spatie \Sitemap \SitemapIndex ;
15
13
use Spatie \Sitemap \Tags \Url ;
14
+ use Spatie \Crawler \Crawler ;
15
+ use Spatie \Sitemap \SitemapIndex ;
16
16
17
- class SitemapCommand extends Command
17
+ class SitemapGenerateCommand extends Command
18
18
{
19
19
/**
20
20
* The name and signature of the console command.
@@ -31,7 +31,12 @@ class SitemapCommand extends Command
31
31
* when listed in the console. It's shown when you run `php artisan list`
32
32
* or when you specifically view help for this command.
33
33
*/
34
- protected $ description = 'Generate sitemap ' ;
34
+ protected $ description = 'Sitemap Generation ' ;
35
+
36
+ /**
37
+ * TODO
38
+ */
39
+ protected $ diskName ;
35
40
36
41
/**
37
42
* Execute the console command.
@@ -44,22 +49,32 @@ class SitemapCommand extends Command
44
49
*/
45
50
public function handle ()
46
51
{
47
- $ SitemapIndex = ! Config::get ('fv-sitemap.exclude_index ' );
52
+ $ this -> diskName = Config::get ('fv-sitemap.disk ' , ' public ' );
48
53
49
- if ($ SitemapIndex ) {
50
- $ this ->generatePagesSitemap ();
51
- $ this ->generatePostsSitemap ();
54
+ try {
55
+ $ SitemapIndex = !Config::get ('fv-sitemap.exclude_index ' );
52
56
53
- $ sitemapIndex = SitemapIndex:: create ()
54
- -> add ( ' /pages_sitemap.xml ' )
55
- -> add ( ' /posts_sitemap.xml ' );
57
+ if ( $ SitemapIndex ) {
58
+ $ this -> generatePagesSitemap ();
59
+ $ this -> generatePostsSitemap ( );
56
60
57
- $ sitemapIndex ->writeToFile (public_path ('sitemap.xml ' ));
58
- } else {
59
- $ this ->generatePagesSitemap ('sitemap.xml ' );
60
- }
61
+ $ sitemapIndex = SitemapIndex::create ()
62
+ ->add ('/sitemap/pages_sitemap.xml ' )
63
+ ->add ('/sitemap/posts_sitemap.xml ' );
61
64
62
- $ this ->info ('Sitemap generated successfully. ' );
65
+ $ sitemapIndex ->writeToDisk ($ this ->diskName , '/sitemap/sitemap.xml ' , true );
66
+ } else {
67
+ $ this ->generatePagesSitemap ('/sitemap/sitemap.xml ' );
68
+ }
69
+
70
+ $ this ->info ('Sitemap generated successfully. ' );
71
+ } catch (\Exception $ e ) {
72
+ Log::error ('Sitemap generation failed: ' . $ e ->getMessage ());
73
+
74
+ $ this ->error ('Sitemap generation failed: ' . $ e ->getMessage ());
75
+
76
+ return 1 ;
77
+ }
63
78
}
64
79
65
80
/**
@@ -69,14 +84,13 @@ public function handle()
69
84
* It filters out URLs based on various criteria, including predefined excluded routes, paths,
70
85
* and specific URLs. The resulting sitemap is then saved to a specified filename.
71
86
*
72
- * @param string $filename The filename for the generated sitemap, defaulting to 'pages_sitemap.xml'.
87
+ * @param string $filename The filename for the generated sitemap, defaulting to 'pages_sitemap.xml'.
73
88
*/
74
89
protected function generatePagesSitemap ($ filename = 'pages_sitemap.xml ' )
75
90
{
76
- $ diskName = Config::get ('fv-sitemap.disk ' );
77
- $ tempFilePath = tempnam (sys_get_temp_dir (), 'sitemap ' );
78
-
79
- $ excludedRouteNameUrls = $ this ->mapExcludedRoutesToUrls ();
91
+ $ filename = 'sitemap/ ' . $ filename ;
92
+
93
+ $ excludedRouteNameUrls = $ this ->mapExcludedRouteNamesToUrls ();
80
94
$ excludedPaths = $ this ->getExcludedPaths ();
81
95
$ excludedUrls = $ this ->getExcludedUrls ();
82
96
@@ -100,23 +114,12 @@ protected function generatePagesSitemap($filename = 'pages_sitemap.xml')
100
114
$ normalizedUrl = rtrim ($ url ->url , '/ ' );
101
115
if ($ normalizedUrl !== $ baseUrlWithoutSlash ) {
102
116
$ url ->setUrl ($ normalizedUrl );
103
- } elseif ($ url ->url === $ baseUrlWithoutSlash. '/ ' ) {
117
+ } else if ($ url ->url === $ baseUrlWithoutSlash . '/ ' ) {
104
118
return null ;
105
119
}
106
120
107
121
return $ url ;
108
- })->getSitemap ()->writeToFile ($ tempFilePath );
109
-
110
- $ sitemapContent = file_get_contents ($ tempFilePath );
111
-
112
- if ($ diskName !== 'public_path ' ) {
113
- Storage::disk ($ diskName )->put ($ filename , $ sitemapContent );
114
- } else {
115
- file_put_contents (public_path ($ filename ), $ sitemapContent );
116
- }
117
-
118
- // Remove the temporary file after use
119
- @unlink ($ tempFilePath );
122
+ })->getSitemap ()->writeToDisk ($ this ->diskName , $ filename , true );
120
123
}
121
124
122
125
/**
@@ -132,41 +135,74 @@ protected function generatePostsSitemap()
132
135
//
133
136
}
134
137
138
+ /**
139
+ * Determine if a path matches any of the excluded patterns.
140
+ *
141
+ * @param string $path
142
+ * @param array $excludedPatterns
143
+ * @return bool
144
+ */
145
+ protected function isPathExcluded ($ path , $ excludedPatterns )
146
+ {
147
+ foreach ($ excludedPatterns as $ pattern ) {
148
+ if (preg_match ("#^ " . preg_quote ($ pattern , '# ' ) . "# " , $ path )) {
149
+ return true ;
150
+ }
151
+ }
152
+ return false ;
153
+ }
154
+
135
155
/**
136
156
* Check if a given URL is a redirect.
137
157
*
138
- * @param string $url
158
+ * @param string $url
139
159
* @return bool
140
160
*/
141
161
protected function isRedirect ($ url )
142
162
{
143
163
$ excludeRedirects = Config::get ('fv-sitemap.exclude_redirects ' );
144
164
145
- if (! $ excludeRedirects ) {
165
+ if (!$ excludeRedirects ) {
146
166
return false ;
147
167
}
148
168
149
169
$ client = new Client ();
150
170
try {
151
171
$ response = $ client ->request ('HEAD ' , $ url , ['allow_redirects ' => false ]);
152
172
$ statusCode = $ response ->getStatusCode ();
153
-
154
173
return in_array ($ statusCode , [301 , 302 , 307 , 308 ]);
155
174
} catch (GuzzleException $ e ) {
156
- Log::error ('Error checking URL: ' .$ e ->getMessage ());
157
-
175
+ Log::error ('Error checking URL: ' . $ e ->getMessage ());
158
176
return false ;
159
177
}
160
178
}
161
179
180
+ /**
181
+ * Map excluded route names to their URLs, excluding any that are invalid.
182
+ *
183
+ * @return array
184
+ */
185
+ protected function mapExcludedRouteNamesToUrls ()
186
+ {
187
+ $ excludedRouteNames = $ this ->getExcludedRouteNames ();
188
+
189
+ return collect ($ excludedRouteNames )->map (function ($ routeName ) {
190
+ try {
191
+ return route ($ routeName , [], false );
192
+ } catch (\InvalidArgumentException $ e ) {
193
+ return null ;
194
+ }
195
+ })->filter ()->values ()->all ();
196
+ }
197
+
162
198
/**
163
199
* Get route names to exclude from the sitemap.
164
200
*
165
201
* @return array
166
202
*/
167
203
protected function getExcludedRouteNames ()
168
204
{
169
- return Config::get ('fv-sitemap.sitemap_exclude_route_names ' , []);
205
+ return Config::get ('fv-sitemap.exclude_route_names ' , []);
170
206
171
207
}
172
208
@@ -189,40 +225,4 @@ protected function getExcludedUrls()
189
225
{
190
226
return Config::get ('fv-sitemap.exclude_urls ' , []);
191
227
}
192
-
193
- /**
194
- * Map excluded route names to their URLs, excluding any that are invalid.
195
- *
196
- * @return array
197
- */
198
- protected function mapExcludedRoutesToUrls ()
199
- {
200
- $ excludedRouteNames = $ this ->getExcludedRouteNames ();
201
-
202
- return collect ($ excludedRouteNames )->map (function ($ routeName ) {
203
- try {
204
- return route ($ routeName , [], false );
205
- } catch (\InvalidArgumentException $ e ) {
206
- return null ;
207
- }
208
- })->filter ()->values ()->all ();
209
- }
210
-
211
- /**
212
- * Determine if a path matches any of the excluded patterns.
213
- *
214
- * @param string $path
215
- * @param array $excludedPatterns
216
- * @return bool
217
- */
218
- protected function isPathExcluded ($ path , $ excludedPatterns )
219
- {
220
- foreach ($ excludedPatterns as $ pattern ) {
221
- if (preg_match ('#^ ' .preg_quote ($ pattern , '# ' ).'# ' , $ path )) {
222
- return true ;
223
- }
224
- }
225
-
226
- return false ;
227
- }
228
228
}
0 commit comments