6
6
use GuzzleHttp \Exception \GuzzleException ;
7
7
use Illuminate \Console \Command ;
8
8
use Illuminate \Support \Facades \Config ;
9
- use Illuminate \Support \Facades \Route ;
10
- Use Illuminate \Support \Facades \Storage ;
11
9
use Illuminate \Support \Facades \Log ;
12
- use Spatie \Sitemap \SitemapGenerator ;
13
- use Spatie \Sitemap \Tags \Url ;
10
+ use Illuminate \Support \Facades \Route ;
14
11
use Spatie \Crawler \Crawler ;
12
+ use Spatie \Sitemap \SitemapGenerator ;
15
13
use Spatie \Sitemap \SitemapIndex ;
14
+ use Spatie \Sitemap \Tags \Url ;
16
15
17
16
class SitemapGenerateCommand extends Command
18
17
{
@@ -52,7 +51,7 @@ public function handle()
52
51
$ this ->diskName = Config::get ('fv-sitemap.disk ' , 'public ' );
53
52
54
53
try {
55
- $ SitemapIndex = !Config::get ('fv-sitemap.exclude_index ' );
54
+ $ SitemapIndex = ! Config::get ('fv-sitemap.exclude_index ' );
56
55
57
56
if ($ SitemapIndex ) {
58
57
$ this ->generatePagesSitemap ();
@@ -69,9 +68,9 @@ public function handle()
69
68
70
69
$ this ->info ('Sitemap generated successfully. ' );
71
70
} catch (\Exception $ e ) {
72
- Log::error ('Sitemap generation failed: ' . $ e ->getMessage ());
71
+ Log::error ('Sitemap generation failed: ' . $ e ->getMessage ());
73
72
74
- $ this ->error ('Sitemap generation failed: ' . $ e ->getMessage ());
73
+ $ this ->error ('Sitemap generation failed: ' . $ e ->getMessage ());
75
74
76
75
return 1 ;
77
76
}
@@ -84,12 +83,12 @@ public function handle()
84
83
* It filters out URLs based on various criteria, including predefined excluded routes, paths,
85
84
* and specific URLs. The resulting sitemap is then saved to a specified filename.
86
85
*
87
- * @param string $filename The filename for the generated sitemap, defaulting to 'pages_sitemap.xml'.
86
+ * @param string $filename The filename for the generated sitemap, defaulting to 'pages_sitemap.xml'.
88
87
*/
89
88
protected function generatePagesSitemap ($ filename = 'pages_sitemap.xml ' )
90
89
{
91
- $ filename = 'sitemap/ ' . $ filename ;
92
-
90
+ $ filename = 'sitemap/ ' . $ filename ;
91
+
93
92
$ excludedRouteNameUrls = $ this ->mapExcludedRouteNamesToUrls ();
94
93
$ excludedPaths = $ this ->getExcludedPaths ();
95
94
$ excludedUrls = $ this ->getExcludedUrls ();
@@ -114,7 +113,7 @@ protected function generatePagesSitemap($filename = 'pages_sitemap.xml')
114
113
$ normalizedUrl = rtrim ($ url ->url , '/ ' );
115
114
if ($ normalizedUrl !== $ baseUrlWithoutSlash ) {
116
115
$ url ->setUrl ($ normalizedUrl );
117
- } else if ($ url ->url === $ baseUrlWithoutSlash . '/ ' ) {
116
+ } elseif ($ url ->url === $ baseUrlWithoutSlash. '/ ' ) {
118
117
return null ;
119
118
}
120
119
@@ -138,41 +137,44 @@ protected function generatePostsSitemap()
138
137
/**
139
138
* Determine if a path matches any of the excluded patterns.
140
139
*
141
- * @param string $path
142
- * @param array $excludedPatterns
140
+ * @param string $path
141
+ * @param array $excludedPatterns
143
142
* @return bool
144
143
*/
145
144
protected function isPathExcluded ($ path , $ excludedPatterns )
146
145
{
147
146
foreach ($ excludedPatterns as $ pattern ) {
148
- if (preg_match (" #^ " . preg_quote ($ pattern , '# ' ) . " # " , $ path )) {
147
+ if (preg_match (' #^ ' . preg_quote ($ pattern , '# ' ). ' # ' , $ path )) {
149
148
return true ;
150
149
}
151
150
}
151
+
152
152
return false ;
153
153
}
154
154
155
155
/**
156
156
* Check if a given URL is a redirect.
157
157
*
158
- * @param string $url
158
+ * @param string $url
159
159
* @return bool
160
160
*/
161
161
protected function isRedirect ($ url )
162
162
{
163
163
$ excludeRedirects = Config::get ('fv-sitemap.exclude_redirects ' );
164
164
165
- if (!$ excludeRedirects ) {
165
+ if (! $ excludeRedirects ) {
166
166
return false ;
167
167
}
168
168
169
169
$ client = new Client ();
170
170
try {
171
171
$ response = $ client ->request ('HEAD ' , $ url , ['allow_redirects ' => false ]);
172
172
$ statusCode = $ response ->getStatusCode ();
173
+
173
174
return in_array ($ statusCode , [301 , 302 , 307 , 308 ]);
174
175
} catch (GuzzleException $ e ) {
175
- Log::error ('Error checking URL: ' . $ e ->getMessage ());
176
+ Log::error ('Error checking URL: ' .$ e ->getMessage ());
177
+
176
178
return false ;
177
179
}
178
180
}
0 commit comments