Skip to content

Commit 48ed303

Browse files
committed
chore: initial setup
0 parents  commit 48ed303

17 files changed

Lines changed: 1222 additions & 0 deletions

.github/FUNDING.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These are supported funding model platforms
2+
3+
open_collective: leaf
4+
github: leafsphp

.github/workflows/lint.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Lint code
2+
3+
on: [push]
4+
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
fail-fast: true
10+
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v2
14+
with:
15+
ref: ${{ github.head_ref }}
16+
17+
- name: Setup PHP
18+
uses: shivammathur/setup-php@v2
19+
with:
20+
php-version: '8.2'
21+
extensions: json, zip, dom, curl, libxml, mbstring
22+
tools: composer:v2
23+
coverage: none
24+
25+
- name: Install PHP dependencies
26+
run: composer update --no-interaction --no-progress
27+
28+
- name: Run Linter
29+
run: composer run lint
30+
31+
- name: Commit changes
32+
uses: stefanzweifel/git-auto-commit-action@v4
33+
with:
34+
commit_message: 'chore: fix styling'

.github/workflows/tests.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Run Tests
2+
3+
on: ["push","pull_request"]
4+
5+
jobs:
6+
tests:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
fail-fast: true
10+
matrix:
11+
os: ["ubuntu-latest"]
12+
php: ["7.4", "8.4"]
13+
14+
name: PHP ${{ matrix.php }} - ${{ matrix.os }}
15+
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v2
19+
20+
- name: Setup PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: ${{ matrix.php }}
24+
extensions: json, zip, dom, curl, libxml, mbstring
25+
tools: composer:v2
26+
coverage: xdebug
27+
28+
- name: Install PHP dependencies
29+
run: composer update --no-interaction --no-progress
30+
31+
- name: Run Tests
32+
run: composer run test -- --flags=coverage

.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Global
2+
.phpunit*
3+
.php-cs-fixer.cache
4+
.composer
5+
composer.lock
6+
package-lock.json
7+
vendor/
8+
test/
9+
10+
# OS Generated
11+
.DS_Store*
12+
ehthumbs.db
13+
Icon?
14+
Thumbs.db
15+
*.swp
16+
17+
# phpstorm
18+
.idea/*
19+
20+
# Alchemy
21+
.alchemy
22+
.phpunit.result.cache

README.md

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<!-- markdownlint-disable no-inline-html -->
2+
<p align="center">
3+
<br>
4+
<img src="https://leafphp.dev/logo-circle.png" height="120"/>
5+
<br>
6+
</p>
7+
8+
<h1 align="center">Sitemap Generator</h1>
9+
10+
<p align="center">
11+
<b>A simple and efficient sitemap generator for Leaf apps</b>
12+
</p>
13+
14+
<p align="center">
15+
<a href="https://packagist.org/packages/leafs/sitemap"><img src="https://poser.pugx.org/leafs/sitemap/v/stable" alt="Latest Stable Version"/></a>
16+
<a href="https://packagist.org/packages/leafs/sitemap"><img src="https://poser.pugx.org/leafs/sitemap/downloads" alt="Total Downloads"/></a>
17+
<a href="https://packagist.org/packages/leafs/sitemap"><img src="https://poser.pugx.org/leafs/sitemap/license" alt="License"/></a>
18+
</p>
19+
20+
<!-- <p align="center">
21+
<a href="#installation">Installation</a> •
22+
<a href="#quick-start">Quick Start</a> •
23+
<a href="#features">Features</a> •
24+
<a href="#api-reference">API Reference</a> •
25+
<!-- <a href="#examples">Examples</a> •
26+
</p> -->
27+
<br>
28+
29+
## Sitemap Generator
30+
31+
**Sitemap Generator** is a simple and efficient tool for generating sitemaps in Leaf PHP. It helps you create XML sitemaps quickly and easily, ensuring your website is properly indexed by search engines. It generates a real sitemap.xml file in the public directory of your Leaf application, which can be accessed by search engines to crawl your website effectively, and has support for custom datasources, allowing you to generate sitemaps from various data sources such as databases, APIs, or static files.
32+
33+
## 🚀 Installation
34+
35+
**Using [Leaf CLI](https://cli.leafphp.dev) (Recommended)**:
36+
37+
```bash
38+
leaf install sitemap
39+
```
40+
41+
**Using [Composer](https://getcomposer.org/)**:
42+
43+
```bash
44+
composer require leafs/sitemap
45+
```
46+
47+
## Quick Start
48+
49+
Once installed, your sitemap will be automatically generated based on the routes defined in your Leaf application. You can customize the sitemap generation by adding a new datasource or by adding sitemap config options directly to your routes.
50+
51+
## Regenerating Your Sitemap
52+
53+
Leaf automatically generates your sitemap when it doesn’t exist.
54+
55+
To regenerate your sitemap, you can:
56+
57+
- delete the existing sitemap.xml file
58+
- or manually generate a new one:
59+
60+
```php
61+
sitemap()->generate();
62+
```
63+
64+
## Auto Sitemaps
65+
66+
Since sitemaps automatically use your Leaf routes, you can add some config options directly to your routes to customize how they appear in the sitemap. For example:
67+
68+
```php
69+
app()->get('/about', [
70+
'sitemap' => [
71+
'changefreq' => 'monthly',
72+
'priority' => 0.5,
73+
],
74+
function() {
75+
echo 'About Us';
76+
}
77+
]);
78+
```
79+
80+
If you have a dynamic route like `/blog/{slug}`, you can also add the sitemap config to the route, you can tell the sitemap generator to replace the `{slug}` parameter with actual values from your model if you have one defined:
81+
82+
```php
83+
app()->get('/blog/{slug}', [
84+
'sitemap' => [
85+
'changefreq' => 'weekly',
86+
'priority' => 0.8,
87+
'model' => \App\Models\Post::class, // or 'posts' if you don't have a model but have a table named 'posts',
88+
'parameter' => 'slug', // the parameter in the route to replace with the model value,
89+
'exclude' => [
90+
'status' => 'draft' // you can also exclude certain items from the sitemap based on model attributes
91+
]
92+
],
93+
'BlogController@show'
94+
]);
95+
```
96+
97+
This is typically a faster way to generate sitemaps for dynamic routes, as you don't have to manually add a datasource and fetch the data yourself, the sitemap generator will handle it for you. So you will only need to manually add a datasource if you want to do more complex link generation like adding multiple URLs for the same route, or if you want to add URLs that are not defined as routes in your application.
98+
99+
## Datasources
100+
101+
Your application may have dynamic routes, eg: `/blog/{slug}`. To include these routes in your sitemap, you can create a custom datasource that fetches the necessary data from your database and adds it to the sitemap. Here's an example of how to create a custom datasource for a blog:
102+
103+
```php
104+
sitemap()->source(function() {
105+
// Fetch blog posts from the database
106+
$posts = db()->select('posts')->get(); // or with a model like Post::all();
107+
108+
// Add each post to the sitemap
109+
foreach ($posts as $post) {
110+
sitemap()->map('/blog/{slug}', "/blog/{$post->slug}", [
111+
'lastmod' => $post->updated_at,
112+
'changefreq' => 'weekly',
113+
'priority' => 0.8,
114+
]);
115+
}
116+
});
117+
118+
// original route
119+
app()->get('/blog/{slug}', 'BlogController@show');
120+
```
121+
122+
You can also add multiple URLs at once:
123+
124+
```php
125+
sitemap()->source(function() {
126+
// Fetch blog posts from the database
127+
$posts = db()->select('posts')->get(); // or with a model like Post::all();
128+
129+
// Add each post to the sitemap
130+
foreach ($posts as $post) {
131+
sitemap()->map('/blog/{slug}', [
132+
"/en/blog/{$post->slug_en}" => [
133+
'lastmod' => $post->updated_at,
134+
'changefreq' => 'weekly',
135+
'priority' => 0.8,
136+
],
137+
"/fr/blog/{$post->slug_fr}" => [
138+
'lastmod' => $post->updated_at,
139+
'changefreq' => 'weekly',
140+
'priority' => 0.8,
141+
],
142+
]);
143+
}
144+
});
145+
146+
// original route
147+
app()->get('/blog/{slug}', 'BlogController@show');
148+
```
149+
150+
This will map the `/blog/{slug}` to the actual URLs of your blog posts, and include additional metadata such as last modification date, change frequency, and priority.
151+
152+
## Add URL
153+
154+
The examples above show how to map existing routes to actual URLs, but you can also add new URLs that are not defined as routes in your application:
155+
156+
```php
157+
sitemap()->source(function() {
158+
// Fetch blog posts from the database
159+
$posts = db()->select('posts')->get(); // or with a model like Post::all();
160+
161+
// Add each post to the sitemap
162+
foreach ($posts as $post) {
163+
sitemap()->add("/blog/{$post->slug}", [
164+
'lastmod' => $post->updated_at,
165+
'changefreq' => 'weekly',
166+
'priority' => 0.8,
167+
]);
168+
}
169+
});
170+
```
171+
172+
This will add the specified URLs to the sitemap without replacing any existing routes.
173+
174+
You can also use `add` without being dynamic:
175+
176+
```php
177+
sitemap()->source(function() {
178+
sitemap()->add('/about', [
179+
'changefreq' => 'monthly',
180+
'priority' => 0.5,
181+
]);
182+
183+
sitemap()->add('/contact', [
184+
'changefreq' => 'monthly',
185+
'priority' => 0.5,
186+
]);
187+
});
188+
```
189+
190+
If you add a URL that already exists in the sitemap, it will be replaced with the new one. For example:
191+
192+
```php
193+
sitemap()->source(function() {
194+
...
195+
196+
sitemap()->add('/about', [
197+
'changefreq' => 'monthly',
198+
'priority' => 0.5,
199+
]);
200+
});
201+
202+
app()->get('/about', function() {
203+
echo 'About Us';
204+
});
205+
```

alchemy.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
app:
2+
- src
3+
4+
tests:
5+
engine: pest
6+
parallel: true
7+
paths:
8+
- tests
9+
files:
10+
- '*.test.php'
11+
coverage:
12+
local: false
13+
actions: true
14+
15+
lint:
16+
single_quote: true
17+
phpdoc_scalar: true
18+
no_unused_imports: true
19+
unary_operator_spaces: true
20+
binary_operator_spaces: true
21+
phpdoc_var_without_name: true
22+
trailing_comma_in_multiline: true
23+
phpdoc_single_line_var_spacing: true
24+
single_trait_insert_per_statement: true
25+
not_operator_with_successor_space: false
26+
array_syntax:
27+
syntax: short
28+
ordered_imports:
29+
sort_algorithm: alpha
30+
method_argument_space:
31+
on_multiline: ensure_fully_multiline
32+
keep_multiple_spaces_after_comma: true
33+
blank_line_before_statement:
34+
statements:
35+
- try
36+
- break
37+
- throw
38+
- return
39+
- declare
40+
- continue
41+
42+
actions:
43+
run:
44+
- lint
45+
- tests
46+
os:
47+
- ubuntu-latest
48+
php:
49+
extensions: json, zip, dom, curl, libxml, mbstring
50+
versions:
51+
- '7.4'
52+
- '8.4'
53+
events:
54+
- push
55+
- pull_request

0 commit comments

Comments
 (0)