first change

This commit is contained in:
2025-07-27 17:40:56 +05:45
commit f8b9a6725b
3152 changed files with 229528 additions and 0 deletions

View File

@@ -0,0 +1,150 @@
<?php
namespace Modules\Sitemap\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Sitemap\Interfaces\SitemapConfigInterface;
use Modules\Sitemap\Models\SitemapConfig;
use Yajra\DataTables\Facades\DataTables;
class SitemapConfigController extends Controller
{
public function __construct(private SitemapConfigInterface $sitemapConfig)
{
//
}
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$isEditing = $request->has("id");
$sitemapConfig = $isEditing ? $this->sitemapConfig->findById($request->id) : null;
if (request()->ajax()) {
$model = SitemapConfig::query()->latest();
return DataTables::eloquent($model)
->addIndexColumn()
->setRowClass('tableRow')
->editColumn('status', function (SitemapConfig $sitemapConfig) {
$status = $sitemapConfig->status ? 'Published' : 'Draft';
$color = $sitemapConfig->status ? 'text-success' : 'text-danger';
return "<p class='{$color}'>{$status}</p>";
})
->addColumn('action', 'sitemap::sitemapConfig.datatable.action')
->rawColumns(['action', 'status'])
->toJson();
}
return view('sitemap::sitemapConfig.index', [
'sitemapConfig' => $sitemapConfig,
'title' => $isEditing ? 'Edit Sitemap Config' : 'Add Sitemap Config',
]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$isEditing = $request->has('id');
if ($isEditing) {
$validated = $request->validate([
'title' => ['required', 'string', 'max:255', 'unique:sitemap_configs,title,' . $request->id],
'max_depth' => ['nullable', 'integer'],
'max_crawl_count' => ['nullable', 'integer'],
'sitemap_excludes' => ['nullable', 'string'],
"ignore_robot" => ['required', 'boolean'],
]);
$sitemapConfig = $this->sitemapConfig->update($request->id, $validated);
session()->flash("success", "Sitemap Config successfully updated.");
return to_route('sitemapConfig.index');
}
$maxOrder = SitemapConfig::max('order');
$order = $maxOrder ? ++$maxOrder : 1;
$request->mergeIfMissing([
'order' => $order
]);
$validated = $request->validate([
'title' => ['required', 'string', 'unique:sitemap_configs,title'],
'max_depth' => ['nullable', 'integer'],
'max_crawl_count' => ['nullable', 'integer'],
'sitemap_excludes' => ['nullable', 'string'],
"ignore_robot" => ['required', 'boolean'],
'order' => ['integer'],
]);
$sitemapConfig = $this->sitemapConfig->create($validated);
session()->flash("success", "Sitemap Config successfully created.");
return to_route('sitemapConfig.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
$sitemapConfig = $this->sitemapConfig->delete($id);
return response()->json([
'status' => 200,
'message' => "Sitemap Config successfully deleted."
], 200);
}
public function toggle($id)
{
$sitemapConfig = $this->sitemapConfig->findById($id);
$sitemapConfig->update([
'status' => !$sitemapConfig->status
]);
return response([
'status' => 200,
'message' => 'Sitemap Config successfully toggled.'
], 200);
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace Modules\Sitemap\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Spatie\Crawler\Crawler;
use Spatie\Sitemap\SitemapGenerator;
use Psr\Http\Message\UriInterface;
use Modules\Sitemap\Models\SitemapConfig;
class SitemapController extends Controller
{
public function index()
{
$path = public_path('sitemap.xml');
if (!file_exists($path)) {
return response('Sitemap not found.', 404);
}
return response()->file($path, [
'Content-Type' => 'application/xml',
]);
}
public function generate(Request $request)
{
$path = public_path('sitemap.xml');
$sitemapConfig = SitemapConfig::active()->latest()->first();
$sitemapExcludes = $sitemapConfig->sitemap_excludes;
$ignoreRobots = $sitemapConfig->ignore_robot;
if (is_string($sitemapExcludes)) {
$sitemapExcludes = array_map('trim', explode(',', $sitemapExcludes));
}
$generator = SitemapGenerator::create(config('app.url'))
->configureCrawler(function (Crawler $crawler) use ($ignoreRobots, $sitemapConfig) {
if ($ignoreRobots) {
$crawler->ignoreRobots();
}
if ($sitemapConfig->max_depth) {
$crawler->setMaximumDepth($sitemapConfig->max_depth);
}
})
->shouldCrawl(function (UriInterface $url) use ($sitemapExcludes) {
$path = $url->getPath();
foreach ($sitemapExcludes as $exclude) {
if (str_starts_with($path, $exclude)) {
return false;
}
}
return true;
});
if ($sitemapConfig->max_crawl_count) {
$generator->setMaximumCrawlCount($sitemapConfig->max_crawl_count);
}
$generator->writeToFile($path);
if (!file_exists($path)) {
return response('Failed to generate sitemap.', 404);
}
return response()->file($path, [
'Content-Type' => 'application/xml',
]);
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace Modules\Sitemap\Interfaces;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
interface ModelInterface
{
/**
* Retrieve all records with optional filtering, pagination, and custom query modifications.
*
* @param Request|null $request HTTP request containing filters or parameters
* @param callable|null $query Callback to modify the query (e.g., add joins or where clauses)
* @param bool $paginate Whether to paginate the results
* @param int|null $limit Number of records per page if paginated, null for no limit
* @return Collection|mixed
*/
public function findAll(?Request $request = null, ?callable $query = null, bool $paginate = false, ?int $limit = null);
/**
* Find a record by its ID with optional query callback.
*
* @param int $id
* @param callable|null $query
* @param array $columns
* @return Model|null
*/
public function findById(int $id, ?callable $query = null);
/**
* Create a new record.
*
* @param array $data
* @return Model
*/
public function create(array $data);
/**
* Update a record by ID.
*
* @param int $id
* @param array $data
* @return Model|null
*/
public function update(int $id, array $data);
/**
* Delete a record by ID.
*
* @param int $id
* @return bool
*/
public function delete(int $id);
/**
* Pluck values from a column, optionally keyed by another column.
*
* @param string $column
* @param string|null $key
* @param callable|null $query
* @return Collection
*/
public function pluck(string $column, string $key, ?callable $query = null);
// /**
// * Find the first record matching attributes or create it.
// *
// * @param array $attributes
// * @param array $values
// * @return Model
// */
// public function firstOrCreate(array $attributes, array $values = []);
// /**
// * Check if a record exists by ID.
// *
// * @param int|string $id
// * @return bool
// */
// public function exists(int|string $id): bool;
// /**
// * Count records matching a condition.
// *
// * @param array $conditions
// * @param callable|null $query
// * @return int
// */
// public function count(array $conditions = [], ?callable $query = null);
}

View File

@@ -0,0 +1,8 @@
<?php
namespace Modules\Sitemap\Interfaces;
interface SitemapConfigInterface extends ModelInterface
{
}

View File

@@ -0,0 +1,40 @@
<?php
namespace Modules\Sitemap\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
// use Modules\Sitemap\Database\Factories\SitemapFactory;
class SitemapConfig extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*/
protected $fillable = [
"title",
"sitemap_excludes",
"ignore_robot",
"max_depth",
"max_crawl_count",
"order",
"status"
];
protected function casts(): array {
return [
"ignore_robot" => "boolean",
];
}
public function scopeActive($builder) {
$builder->where("status", 1);
}
// protected static function newFactory(): SitemapFactory
// {
// // return SitemapFactory::new();
// }
}

View File

View File

@@ -0,0 +1,30 @@
<?php
namespace Modules\Sitemap\Providers;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event handler mappings for the application.
*
* @var array<string, array<int, string>>
*/
protected $listen = [];
/**
* Indicates if events should be discovered.
*
* @var bool
*/
protected static $shouldDiscoverEvents = true;
/**
* Configure the proper event listeners for email verification.
*/
protected function configureEmailVerification(): void
{
//
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Modules\Sitemap\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
protected string $name = 'Sitemap';
/**
* Called before routes are registered.
*
* Register any model bindings or pattern based filters.
*/
public function boot(): void
{
parent::boot();
}
/**
* Define the routes for the application.
*/
public function map(): void
{
$this->mapApiRoutes();
$this->mapWebRoutes();
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*/
protected function mapWebRoutes(): void
{
Route::middleware('web')->group(module_path($this->name, '/routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*/
protected function mapApiRoutes(): void
{
Route::middleware('api')->prefix('api')->name('api.')->group(module_path($this->name, '/routes/api.php'));
}
}

View File

@@ -0,0 +1,138 @@
<?php
namespace Modules\Sitemap\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Modules\Sitemap\Interfaces\SitemapConfigInterface;
use Modules\Sitemap\Repositories\SitemapConfigRepository;
use Nwidart\Modules\Traits\PathNamespace;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
class SitemapServiceProvider extends ServiceProvider
{
use PathNamespace;
protected string $name = 'Sitemap';
protected string $nameLower = 'sitemap';
/**
* Boot the application events.
*/
public function boot(): void
{
$this->registerCommands();
$this->registerCommandSchedules();
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->loadMigrationsFrom(module_path($this->name, 'database/migrations'));
}
/**
* Register the service provider.
*/
public function register(): void
{
$this->app->register(EventServiceProvider::class);
$this->app->register(RouteServiceProvider::class);
$this->app->bind(SitemapConfigInterface::class, SitemapConfigRepository::class);
}
/**
* Register commands in the format of Command::class
*/
protected function registerCommands(): void
{
// $this->commands([]);
}
/**
* Register command Schedules.
*/
protected function registerCommandSchedules(): void
{
// $this->app->booted(function () {
// $schedule = $this->app->make(Schedule::class);
// $schedule->command('inspire')->hourly();
// });
}
/**
* Register translations.
*/
public function registerTranslations(): void
{
$langPath = resource_path('lang/modules/'.$this->nameLower);
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, $this->nameLower);
$this->loadJsonTranslationsFrom($langPath);
} else {
$this->loadTranslationsFrom(module_path($this->name, 'lang'), $this->nameLower);
$this->loadJsonTranslationsFrom(module_path($this->name, 'lang'));
}
}
/**
* Register config.
*/
protected function registerConfig(): void
{
$relativeConfigPath = config('modules.paths.generator.config.path');
$configPath = module_path($this->name, $relativeConfigPath);
if (is_dir($configPath)) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$relativePath = str_replace($configPath . DIRECTORY_SEPARATOR, '', $file->getPathname());
$configKey = $this->nameLower . '.' . str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $relativePath);
$key = ($relativePath === 'config.php') ? $this->nameLower : $configKey;
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
$this->mergeConfigFrom($file->getPathname(), $key);
}
}
}
}
/**
* Register views.
*/
public function registerViews(): void
{
$viewPath = resource_path('views/modules/'.$this->nameLower);
$sourcePath = module_path($this->name, 'resources/views');
$this->publishes([$sourcePath => $viewPath], ['views', $this->nameLower.'-module-views']);
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->nameLower);
$componentNamespace = $this->module_namespace($this->name, $this->app_path(config('modules.paths.generator.component-class.path')));
Blade::componentNamespace($componentNamespace, $this->nameLower);
}
/**
* Get the services provided by the provider.
*/
public function provides(): array
{
return [];
}
private function getPublishableViewPaths(): array
{
$paths = [];
foreach (config('view.paths') as $path) {
if (is_dir($path.'/modules/'.$this->nameLower)) {
$paths[] = $path.'/modules/'.$this->nameLower;
}
}
return $paths;
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Modules\Sitemap\Repositories;
use Illuminate\Http\Request;
use Modules\Sitemap\Interfaces\SitemapConfigInterface;
use Modules\Sitemap\Models\SitemapConfig;
class SitemapConfigRepository implements SitemapConfigInterface
{
public function findAll(?Request $request = null, ?callable $query = null, bool $paginate = false, ?int $limit = 10)
{
$baseQuery = SitemapConfig::query();
if ($query) {
$query($baseQuery);
}
if ($paginate) {
return $baseQuery->paginate($limit);
}
return $baseQuery->latest()->get();
}
public function findById(int $id, ?callable $query = null)
{
$baseQuery = SitemapConfig::query();
if (is_callable($query)) {
$query($baseQuery);
}
return $baseQuery->whereId($id)->firstOrFail();
}
public function delete(int $id)
{
$sitemapConfig = $this->findById($id);
$sitemapConfig->delete();
return true;
}
public function create(array $data)
{
return SitemapConfig::create($data);
}
public function update(int $id, array $data)
{
return tap($this->findById($id), function ($sitemapConfig) use ($data) {
$sitemapConfig->update($data);
});
}
public function pluck(string $column, string $key, ?callable $query = null)
{
$baseQuery = SitemapConfig::query();
if (is_callable($query)) {
$query($baseQuery);
}
return $baseQuery->pluck($key, $column);
}
}