parent
43b15ed9ad
commit
133e16b610
28 changed files with 479 additions and 0 deletions
@ -0,0 +1,56 @@ |
||||
<?php |
||||
|
||||
namespace Modules\Api\Http\Controllers; |
||||
|
||||
use App\Http\Controllers\Controller; |
||||
use Illuminate\Http\Request; |
||||
|
||||
class ApiController extends Controller |
||||
{ |
||||
/** |
||||
* Display a listing of the resource. |
||||
*/ |
||||
public function index() |
||||
{ |
||||
return view('api::index'); |
||||
} |
||||
|
||||
/** |
||||
* Show the form for creating a new resource. |
||||
*/ |
||||
public function create() |
||||
{ |
||||
return view('api::create'); |
||||
} |
||||
|
||||
/** |
||||
* Store a newly created resource in storage. |
||||
*/ |
||||
public function store(Request $request) {} |
||||
|
||||
/** |
||||
* Show the specified resource. |
||||
*/ |
||||
public function show($id) |
||||
{ |
||||
return view('api::show'); |
||||
} |
||||
|
||||
/** |
||||
* Show the form for editing the specified resource. |
||||
*/ |
||||
public function edit($id) |
||||
{ |
||||
return view('api::edit'); |
||||
} |
||||
|
||||
/** |
||||
* Update the specified resource in storage. |
||||
*/ |
||||
public function update(Request $request, $id) {} |
||||
|
||||
/** |
||||
* Remove the specified resource from storage. |
||||
*/ |
||||
public function destroy($id) {} |
||||
} |
@ -0,0 +1,154 @@ |
||||
<?php |
||||
|
||||
namespace Modules\Api\Providers; |
||||
|
||||
use Illuminate\Support\Facades\Blade; |
||||
use Illuminate\Support\ServiceProvider; |
||||
use Nwidart\Modules\Traits\PathNamespace; |
||||
use RecursiveDirectoryIterator; |
||||
use RecursiveIteratorIterator; |
||||
|
||||
class ApiServiceProvider extends ServiceProvider |
||||
{ |
||||
use PathNamespace; |
||||
|
||||
protected string $name = 'Api'; |
||||
|
||||
protected string $nameLower = 'api'; |
||||
|
||||
/** |
||||
* 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); |
||||
} |
||||
|
||||
/** |
||||
* 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 |
||||
{ |
||||
$configPath = module_path($this->name, config('modules.paths.generator.config.path')); |
||||
|
||||
if (is_dir($configPath)) { |
||||
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath)); |
||||
|
||||
foreach ($iterator as $file) { |
||||
if ($file->isFile() && $file->getExtension() === 'php') { |
||||
$config = str_replace($configPath.DIRECTORY_SEPARATOR, '', $file->getPathname()); |
||||
$config_key = str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $config); |
||||
$segments = explode('.', $this->nameLower.'.'.$config_key); |
||||
|
||||
// Remove duplicated adjacent segments |
||||
$normalized = []; |
||||
foreach ($segments as $segment) { |
||||
if (end($normalized) !== $segment) { |
||||
$normalized[] = $segment; |
||||
} |
||||
} |
||||
|
||||
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized); |
||||
|
||||
$this->publishes([$file->getPathname() => config_path($config)], 'config'); |
||||
$this->merge_config_from($file->getPathname(), $key); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Merge config from the given path recursively. |
||||
*/ |
||||
protected function merge_config_from(string $path, string $key): void |
||||
{ |
||||
$existing = config($key, []); |
||||
$module_config = require $path; |
||||
|
||||
config([$key => array_replace_recursive($existing, $module_config)]); |
||||
} |
||||
|
||||
/** |
||||
* 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); |
||||
|
||||
Blade::componentNamespace(config('modules.namespace').'\\' . $this->name . '\\View\\Components', $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; |
||||
} |
||||
} |
@ -0,0 +1,27 @@ |
||||
<?php |
||||
|
||||
namespace Modules\Api\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 {} |
||||
} |
@ -0,0 +1,50 @@ |
||||
<?php |
||||
|
||||
namespace Modules\Api\Providers; |
||||
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; |
||||
use Illuminate\Support\Facades\Route; |
||||
|
||||
class RouteServiceProvider extends ServiceProvider |
||||
{ |
||||
protected string $name = 'Api'; |
||||
|
||||
/** |
||||
* 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')); |
||||
} |
||||
} |
@ -0,0 +1,30 @@ |
||||
{ |
||||
"name": "nwidart/api", |
||||
"description": "", |
||||
"authors": [ |
||||
{ |
||||
"name": "Nicolas Widart", |
||||
"email": "n.widart@gmail.com" |
||||
} |
||||
], |
||||
"extra": { |
||||
"laravel": { |
||||
"providers": [], |
||||
"aliases": { |
||||
|
||||
} |
||||
} |
||||
}, |
||||
"autoload": { |
||||
"psr-4": { |
||||
"Modules\\Api\\": "app/", |
||||
"Modules\\Api\\Database\\Factories\\": "database/factories/", |
||||
"Modules\\Api\\Database\\Seeders\\": "database/seeders/" |
||||
} |
||||
}, |
||||
"autoload-dev": { |
||||
"psr-4": { |
||||
"Modules\\Api\\Tests\\": "tests/" |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,5 @@ |
||||
<?php |
||||
|
||||
return [ |
||||
'name' => 'Api', |
||||
]; |
@ -0,0 +1,16 @@ |
||||
<?php |
||||
|
||||
namespace Modules\Api\Database\Seeders; |
||||
|
||||
use Illuminate\Database\Seeder; |
||||
|
||||
class ApiDatabaseSeeder extends Seeder |
||||
{ |
||||
/** |
||||
* Run the database seeds. |
||||
*/ |
||||
public function run(): void |
||||
{ |
||||
// $this->call([]); |
||||
} |
||||
} |
@ -0,0 +1,16 @@ |
||||
{ |
||||
"name": "modules/api", |
||||
"description": "", |
||||
"type": "module", |
||||
"autoload": { |
||||
"psr-4": { |
||||
"Modules\\Api\\": "app/" |
||||
} |
||||
}, |
||||
"extra": { |
||||
"laravel": { |
||||
"providers": [] |
||||
} |
||||
}, |
||||
"require": {} |
||||
} |
@ -0,0 +1,15 @@ |
||||
{ |
||||
"private": true, |
||||
"type": "module", |
||||
"scripts": { |
||||
"dev": "vite", |
||||
"build": "vite build" |
||||
}, |
||||
"devDependencies": { |
||||
"axios": "^1.1.2", |
||||
"laravel-vite-plugin": "^0.7.5", |
||||
"sass": "^1.69.5", |
||||
"postcss": "^8.3.7", |
||||
"vite": "^4.0.0" |
||||
} |
||||
} |
@ -0,0 +1,29 @@ |
||||
<!DOCTYPE html> |
||||
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> |
||||
|
||||
<head> |
||||
<meta charset="utf-8"> |
||||
<meta name="viewport" content="width=device-width, initial-scale=1"> |
||||
<meta name="csrf-token" content="{{ csrf_token() }}"> |
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> |
||||
|
||||
<title>Api Module - {{ config('app.name', 'Laravel') }}</title> |
||||
|
||||
<meta name="description" content="{{ $description ?? '' }}"> |
||||
<meta name="keywords" content="{{ $keywords ?? '' }}"> |
||||
<meta name="author" content="{{ $author ?? '' }}"> |
||||
|
||||
<!-- Fonts --> |
||||
<link rel="preconnect" href="https://fonts.bunny.net"> |
||||
<link href="https://fonts.bunny.net/css?family=figtree:400,500,600&display=swap" rel="stylesheet" /> |
||||
|
||||
{{-- Vite CSS --}} |
||||
{{-- {{ module_vite('build-api', 'resources/assets/sass/app.scss') }} --}} |
||||
</head> |
||||
|
||||
<body> |
||||
{{ $slot }} |
||||
|
||||
{{-- Vite JS --}} |
||||
{{-- {{ module_vite('build-api', 'resources/assets/js/app.js') }} --}} |
||||
</body> |
@ -0,0 +1,5 @@ |
||||
<x-api::layouts.master> |
||||
<h1>Hello World</h1> |
||||
|
||||
<p>Module: {!! config('api.name') !!}</p> |
||||
</x-api::layouts.master> |
@ -0,0 +1,8 @@ |
||||
<?php |
||||
|
||||
use Illuminate\Support\Facades\Route; |
||||
use Modules\Api\Http\Controllers\ApiController; |
||||
|
||||
Route::middleware(['auth:sanctum'])->prefix('v1')->group(function () { |
||||
Route::apiResource('apis', ApiController::class)->names('api'); |
||||
}); |
@ -0,0 +1,8 @@ |
||||
<?php |
||||
|
||||
use Illuminate\Support\Facades\Route; |
||||
use Modules\Api\Http\Controllers\ApiController; |
||||
|
||||
Route::middleware(['auth', 'verified'])->group(function () { |
||||
Route::resource('apis', ApiController::class)->names('api'); |
||||
}); |
@ -0,0 +1,57 @@ |
||||
import { defineConfig } from 'vite'; |
||||
import laravel from 'laravel-vite-plugin'; |
||||
import { readdirSync, statSync } from 'fs'; |
||||
import { join,relative,dirname } from 'path'; |
||||
import { fileURLToPath } from 'url'; |
||||
|
||||
export default defineConfig({ |
||||
build: { |
||||
outDir: '../../public/build-api', |
||||
emptyOutDir: true, |
||||
manifest: true, |
||||
}, |
||||
plugins: [ |
||||
laravel({ |
||||
publicDirectory: '../../public', |
||||
buildDirectory: 'build-api', |
||||
input: [ |
||||
__dirname + '/resources/assets/sass/app.scss', |
||||
__dirname + '/resources/assets/js/app.js' |
||||
], |
||||
refresh: true, |
||||
}), |
||||
], |
||||
}); |
||||
// Scen all resources for assets file. Return array
|
||||
//function getFilePaths(dir) {
|
||||
// const filePaths = [];
|
||||
//
|
||||
// function walkDirectory(currentPath) {
|
||||
// const files = readdirSync(currentPath);
|
||||
// for (const file of files) {
|
||||
// const filePath = join(currentPath, file);
|
||||
// const stats = statSync(filePath);
|
||||
// if (stats.isFile() && !file.startsWith('.')) {
|
||||
// const relativePath = 'Modules/Api/'+relative(__dirname, filePath);
|
||||
// filePaths.push(relativePath);
|
||||
// } else if (stats.isDirectory()) {
|
||||
// walkDirectory(filePath);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// walkDirectory(dir);
|
||||
// return filePaths;
|
||||
//}
|
||||
|
||||
//const __filename = fileURLToPath(import.meta.url);
|
||||
//const __dirname = dirname(__filename);
|
||||
|
||||
//const assetsDir = join(__dirname, 'resources/assets');
|
||||
//export const paths = getFilePaths(assetsDir);
|
||||
|
||||
|
||||
//export const paths = [
|
||||
// 'Modules/Api/resources/assets/sass/app.scss',
|
||||
// 'Modules/Api/resources/assets/js/app.js',
|
||||
//];
|
@ -0,0 +1,3 @@ |
||||
{ |
||||
"Api": true |
||||
} |
Loading…
Reference in new issue