Skip to content

Commit 8c60357

Browse files
committed
setup oauth2 laravel 11
0 parents  commit 8c60357

File tree

111 files changed

+4222
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+4222
-0
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 4
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml,yaml}]
15+
indent_size = 2
16+
17+
[docker-compose.yml]
18+
indent_size = 4

.env.example

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
APP_NAME=Laravel
2+
APP_ENV=local
3+
APP_KEY=
4+
APP_DEBUG=true
5+
APP_TIMEZONE=UTC
6+
APP_URL=http://localhost
7+
8+
APP_LOCALE=en
9+
APP_FALLBACK_LOCALE=en
10+
APP_FAKER_LOCALE=en_US
11+
12+
APP_MAINTENANCE_DRIVER=file
13+
APP_MAINTENANCE_STORE=database
14+
15+
BCRYPT_ROUNDS=12
16+
17+
LOG_CHANNEL=stack
18+
LOG_STACK=single
19+
LOG_DEPRECATIONS_CHANNEL=null
20+
LOG_LEVEL=debug
21+
22+
DB_CONNECTION=sqlite
23+
# DB_HOST=127.0.0.1
24+
# DB_PORT=3306
25+
# DB_DATABASE=laravel
26+
# DB_USERNAME=root
27+
# DB_PASSWORD=
28+
29+
SESSION_DRIVER=database
30+
SESSION_LIFETIME=120
31+
SESSION_ENCRYPT=false
32+
SESSION_PATH=/
33+
SESSION_DOMAIN=null
34+
35+
BROADCAST_CONNECTION=log
36+
FILESYSTEM_DISK=local
37+
QUEUE_CONNECTION=database
38+
39+
CACHE_STORE=database
40+
CACHE_PREFIX=
41+
42+
MEMCACHED_HOST=127.0.0.1
43+
44+
REDIS_CLIENT=phpredis
45+
REDIS_HOST=127.0.0.1
46+
REDIS_PASSWORD=null
47+
REDIS_PORT=6379
48+
49+
MAIL_MAILER=log
50+
MAIL_HOST=127.0.0.1
51+
MAIL_PORT=2525
52+
MAIL_USERNAME=null
53+
MAIL_PASSWORD=null
54+
MAIL_ENCRYPTION=null
55+
MAIL_FROM_ADDRESS="[email protected]"
56+
MAIL_FROM_NAME="${APP_NAME}"
57+
58+
AWS_ACCESS_KEY_ID=
59+
AWS_SECRET_ACCESS_KEY=
60+
AWS_DEFAULT_REGION=us-east-1
61+
AWS_BUCKET=
62+
AWS_USE_PATH_STYLE_ENDPOINT=false
63+
64+
VITE_APP_NAME="${APP_NAME}"

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
* text=auto eol=lf
2+
3+
*.blade.php diff=html
4+
*.css diff=css
5+
*.html diff=html
6+
*.md diff=markdown
7+
*.php diff=php
8+
9+
/.github export-ignore
10+
CHANGELOG.md export-ignore
11+
.styleci.yml export-ignore

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/.phpunit.cache
2+
/node_modules
3+
/public/build
4+
/public/hot
5+
/public/storage
6+
/storage/*.key
7+
/vendor
8+
.env
9+
.env.backup
10+
.env.production
11+
.phpunit.result.cache
12+
Homestead.json
13+
Homestead.yaml
14+
auth.json
15+
npm-debug.log
16+
yarn-error.log
17+
/.fleet
18+
/.idea
19+
/.vscode
20+
composer.lock

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
### Laravel Setup API OAuth2
2+
3+
This repository serves as a comprehensive guide and boilerplate for setting up OAuth2 authentication in a Laravel API. Streamline the integration of OAuth2 protocols to secure your API endpoints, authenticate users, and authorize access. Whether you're building a new API or enhancing an existing one, this Laravel setup ensures a robust and standardized OAuth2 implementation.
4+
5+
### Key Features:
6+
7+
🚀 Seamless integration with Laravel framework.
8+
🔐 OAuth2 protocols for secure authentication and authorization.
9+
🧩 Modular structure for flexibility and scalability.
10+
⚙️ Configurable options to adapt to specific project requirements.
11+
Getting Started:

app/Console/Commands/a.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class a extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'crawl:test';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Command description';
22+
23+
/**
24+
* Execute the console command.
25+
*/
26+
public function handle()
27+
{
28+
//
29+
}
30+
}

app/CrawlBlog/CrawlExample.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
namespace App\CrawlBlog;
4+
5+
use Carbon\Carbon;
6+
use Giauphan\CrawlBlog\Models\CategoryBlog;
7+
use Giauphan\CrawlBlog\Models\Post;
8+
use Giauphan\Goutte\GoutteFacade;
9+
use Illuminate\Console\Command;
10+
use Illuminate\Support\Str;
11+
use Symfony\Component\DomCrawler\Crawler;
12+
13+
class CrawlExample extends Command
14+
{
15+
protected $signature = 'crawl:CrawlExample {url} {category_name} {lang} {limitblog}';
16+
protected $description = 'CrawlExample blog data from a given URL';
17+
18+
public function handle()
19+
{
20+
$pageUrl = $this->argument('url');
21+
$categoryName = $this->argument('category_name');
22+
$lang = $this->argument('lang');
23+
$limit = $this->argument('limitblog');
24+
25+
$category = CategoryBlog::firstOrCreate(['name' => $categoryName], ['slug' => Str::slug($categoryName)]);
26+
$categoryId = $category->id;
27+
28+
$totaltimes = 0;
29+
30+
$category = CategoryBlog::firstOrCreate(['name' => $categoryName], ['slug' => Str::slug($categoryName)]);
31+
$categoryId = $category->id;
32+
do {
33+
$crawler = GoutteFacade::request('GET', $pageUrl);
34+
35+
$crawl_arr = $crawler->filter('.classnameBLog');
36+
if ($crawl_arr->count() === 0) {
37+
$this->error('No matching elements found on the page. Check if the HTML structure has changed.');
38+
break;
39+
}
40+
41+
foreach ($crawl_arr as $node) {
42+
43+
if ($node instanceof \DOMElement) {
44+
$node = new Crawler($node);
45+
}
46+
47+
$title = $node->filter('.Title')->text();
48+
$summary = $node->filter('.summary')->text();
49+
$image = optional($node->filter('.ImagesBlog')->first())->attr('data-lazy-src');
50+
$linkHref = $node->filter('.LinkBlog')->attr('href');
51+
$this->scrapeData($linkHref, $title, $image, $summary, $categoryId, $lang);
52+
53+
$totaltimes++;
54+
55+
if ($totaltimes >= $limit) {
56+
$this->info('Reached the limit.');
57+
break 2;
58+
}
59+
};
60+
61+
$nextLink = $crawler->filter('nav.pagination li a.next')->first();
62+
if ($nextLink->count() <= 0) {
63+
break;
64+
}
65+
$nextPageUrl = $nextLink->attr('href');
66+
$pageUrl = $nextPageUrl;
67+
} while ($pageUrl !== '');
68+
}
69+
70+
public function scrapeData($url, $title, $image, $summary, $categoryId, $lang)
71+
{
72+
$crawler = GoutteFacade::request('GET', $url);
73+
$content = $this->crawlData_html('#main .post', $crawler);
74+
$check = Post::all();
75+
76+
if ($check->isEmpty()) {
77+
$this->createPost($title, $image, $summary, $content, $categoryId, $lang);
78+
} else {
79+
$this->checkAndUpdatePost($title, $image, $summary, $content, $check, $categoryId, $lang);
80+
}
81+
}
82+
83+
protected function createPost($title, $image, $summary, $content, $categoryId, $lang)
84+
{
85+
$cleanedTitle = Str::slug($title, '-');
86+
$slug = preg_replace('/[^A-Za-z0-9\-]/', '', $cleanedTitle);
87+
$dataPost = [
88+
'title' => $title,
89+
'slug' => $slug,
90+
'content' => $content,
91+
'images' => $image,
92+
'lang' => $lang,
93+
'published_at' => Carbon::now(),
94+
'summary' => $summary,
95+
'category_blog_id' => $categoryId,
96+
'SimilarityPercentage' => 0.0,
97+
];
98+
Post::create($dataPost);
99+
}
100+
101+
protected function checkAndUpdatePost($title, $image, $summary, $content, $check, $categoryId, $lang)
102+
{
103+
$checkTile = false;
104+
$similarityPercentage = 0.0;
105+
106+
foreach ($check as $blog) {
107+
if ($blog->title !== $title) {
108+
$blog1Words = explode(' ', $blog->content);
109+
$blog2Words = explode(' ', $content);
110+
$commonWords = array_intersect($blog1Words, $blog2Words);
111+
$similarityPercentage += count($commonWords) / count($blog1Words);
112+
} else {
113+
$checkTile = true;
114+
}
115+
}
116+
117+
if (!$checkTile && $title != null) {
118+
$similarityPercentage = $similarityPercentage / $check->count();
119+
$cleanedTitle = Str::slug($title, '-');
120+
$slug = preg_replace('/[^A-Za-z0-9\-]/', '', $cleanedTitle) . '.html';
121+
$dataPost = [
122+
'title' => $title,
123+
'slug' => $slug,
124+
'content' => $content,
125+
'images' => $image,
126+
'lang' => $lang,
127+
'published_at' => Carbon::now(),
128+
'summary' => $summary,
129+
'category_blog_id' => $categoryId,
130+
'SimilarityPercentage' => round($similarityPercentage, 2),
131+
];
132+
Post::create($dataPost);
133+
}
134+
}
135+
136+
protected function crawlData(string $type, $crawler)
137+
{
138+
$result = $crawler->filter($type)->first();
139+
140+
return $result ? $result->text() : '';
141+
}
142+
143+
protected function crawlData_html(string $type, $crawler)
144+
{
145+
$nodeList = $crawler->filter($type);
146+
if ($nodeList->count() === 0) {
147+
return '';
148+
}
149+
150+
$result = $nodeList->first();
151+
return $result ? $result->html() : '';
152+
}
153+
}

0 commit comments

Comments
 (0)