diff --git a/Modules/Api/app/Http/Controllers/.gitkeep b/Modules/Api/app/Http/Controllers/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Modules/Api/app/Http/Controllers/ApiController.php b/Modules/Api/app/Http/Controllers/ApiController.php new file mode 100644 index 0000000..2d7f1e0 --- /dev/null +++ b/Modules/Api/app/Http/Controllers/ApiController.php @@ -0,0 +1,56 @@ +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; + } +} diff --git a/Modules/Api/app/Providers/EventServiceProvider.php b/Modules/Api/app/Providers/EventServiceProvider.php new file mode 100644 index 0000000..7b45106 --- /dev/null +++ b/Modules/Api/app/Providers/EventServiceProvider.php @@ -0,0 +1,27 @@ +> + */ + 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 {} +} diff --git a/Modules/Api/app/Providers/RouteServiceProvider.php b/Modules/Api/app/Providers/RouteServiceProvider.php new file mode 100644 index 0000000..f0d2f27 --- /dev/null +++ b/Modules/Api/app/Providers/RouteServiceProvider.php @@ -0,0 +1,50 @@ +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')); + } +} diff --git a/Modules/Api/composer.json b/Modules/Api/composer.json new file mode 100644 index 0000000..f9bf9a5 --- /dev/null +++ b/Modules/Api/composer.json @@ -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/" + } + } +} diff --git a/Modules/Api/config/.gitkeep b/Modules/Api/config/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Modules/Api/config/config.php b/Modules/Api/config/config.php new file mode 100644 index 0000000..d8a8f1f --- /dev/null +++ b/Modules/Api/config/config.php @@ -0,0 +1,5 @@ + 'Api', +]; diff --git a/Modules/Api/database/factories/.gitkeep b/Modules/Api/database/factories/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Modules/Api/database/migrations/.gitkeep b/Modules/Api/database/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Modules/Api/database/seeders/.gitkeep b/Modules/Api/database/seeders/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Modules/Api/database/seeders/ApiDatabaseSeeder.php b/Modules/Api/database/seeders/ApiDatabaseSeeder.php new file mode 100644 index 0000000..c0085c4 --- /dev/null +++ b/Modules/Api/database/seeders/ApiDatabaseSeeder.php @@ -0,0 +1,16 @@ +call([]); + } +} diff --git a/Modules/Api/module.json b/Modules/Api/module.json new file mode 100644 index 0000000..19b1da3 --- /dev/null +++ b/Modules/Api/module.json @@ -0,0 +1,16 @@ +{ + "name": "modules/api", + "description": "", + "type": "module", + "autoload": { + "psr-4": { + "Modules\\Api\\": "app/" + } + }, + "extra": { + "laravel": { + "providers": [] + } + }, + "require": {} +} diff --git a/Modules/Api/package.json b/Modules/Api/package.json new file mode 100644 index 0000000..d6fbfc8 --- /dev/null +++ b/Modules/Api/package.json @@ -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" + } +} diff --git a/Modules/Api/resources/assets/.gitkeep b/Modules/Api/resources/assets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Modules/Api/resources/assets/js/app.js b/Modules/Api/resources/assets/js/app.js new file mode 100644 index 0000000..e69de29 diff --git a/Modules/Api/resources/assets/sass/app.scss b/Modules/Api/resources/assets/sass/app.scss new file mode 100644 index 0000000..e69de29 diff --git a/Modules/Api/resources/views/.gitkeep b/Modules/Api/resources/views/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Modules/Api/resources/views/components/layouts/master.blade.php b/Modules/Api/resources/views/components/layouts/master.blade.php new file mode 100644 index 0000000..f6cfc4e --- /dev/null +++ b/Modules/Api/resources/views/components/layouts/master.blade.php @@ -0,0 +1,29 @@ + + + + + + + + + + Api Module - {{ config('app.name', 'Laravel') }} + + + + + + + + + + {{-- Vite CSS --}} + {{-- {{ module_vite('build-api', 'resources/assets/sass/app.scss') }} --}} + + + + {{ $slot }} + + {{-- Vite JS --}} + {{-- {{ module_vite('build-api', 'resources/assets/js/app.js') }} --}} + diff --git a/Modules/Api/resources/views/index.blade.php b/Modules/Api/resources/views/index.blade.php new file mode 100644 index 0000000..8b5f487 --- /dev/null +++ b/Modules/Api/resources/views/index.blade.php @@ -0,0 +1,5 @@ + +

Hello World

+ +

Module: {!! config('api.name') !!}

+
diff --git a/Modules/Api/routes/.gitkeep b/Modules/Api/routes/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Modules/Api/routes/api.php b/Modules/Api/routes/api.php new file mode 100644 index 0000000..15ce44d --- /dev/null +++ b/Modules/Api/routes/api.php @@ -0,0 +1,8 @@ +prefix('v1')->group(function () { + Route::apiResource('apis', ApiController::class)->names('api'); +}); diff --git a/Modules/Api/routes/web.php b/Modules/Api/routes/web.php new file mode 100644 index 0000000..80ebc76 --- /dev/null +++ b/Modules/Api/routes/web.php @@ -0,0 +1,8 @@ +group(function () { + Route::resource('apis', ApiController::class)->names('api'); +}); diff --git a/Modules/Api/tests/Feature/.gitkeep b/Modules/Api/tests/Feature/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Modules/Api/tests/Unit/.gitkeep b/Modules/Api/tests/Unit/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Modules/Api/vite.config.js b/Modules/Api/vite.config.js new file mode 100644 index 0000000..38ff765 --- /dev/null +++ b/Modules/Api/vite.config.js @@ -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', +//]; diff --git a/modules_statuses.json b/modules_statuses.json new file mode 100644 index 0000000..4d2898d --- /dev/null +++ b/modules_statuses.json @@ -0,0 +1,3 @@ +{ + "Api": true +} \ No newline at end of file