From db606434bc148d6181397b8beaedeba3d26d8e08 Mon Sep 17 00:00:00 2001 From: molong Date: Sun, 22 Feb 2026 08:59:14 +0800 Subject: [PATCH] =?UTF-8?q?Demo=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/Demo/app/Http/Controllers/.gitkeep | 0 .../Demo/app/Http/Controllers/Admin/Index.php | 8 + .../Demo/app/Http/Controllers/Api/Index.php | 8 + modules/Demo/app/Providers/.gitkeep | 0 .../app/Providers/DemoServiceProvider.php | 154 ++++++++++++++++++ .../app/Providers/EventServiceProvider.php | 27 +++ .../app/Providers/RouteServiceProvider.php | 61 +++++++ modules/Demo/composer.json | 30 ++++ modules/Demo/config/.gitkeep | 0 modules/Demo/config/config.php | 5 + modules/Demo/database/factories/.gitkeep | 0 modules/Demo/database/migrations/.gitkeep | 0 modules/Demo/database/seeders/.gitkeep | 0 .../database/seeders/DemoDatabaseSeeder.php | 16 ++ modules/Demo/module.json | 11 ++ modules/Demo/package.json | 15 ++ modules/Demo/resources/assets/.gitkeep | 0 modules/Demo/resources/assets/js/app.js | 0 modules/Demo/resources/assets/sass/app.scss | 0 modules/Demo/resources/views/.gitkeep | 0 .../views/components/layouts/master.blade.php | 30 ++++ modules/Demo/resources/views/index.blade.php | 5 + modules/Demo/routes/.gitkeep | 0 modules/Demo/routes/admin.php | 4 + modules/Demo/routes/api.php | 4 + modules/Demo/routes/web.php | 8 + modules/Demo/tests/Feature/.gitkeep | 0 modules/Demo/tests/Unit/.gitkeep | 0 modules/Demo/vite.config.js | 57 +++++++ 29 files changed, 443 insertions(+) create mode 100644 modules/Demo/app/Http/Controllers/.gitkeep create mode 100644 modules/Demo/app/Http/Controllers/Admin/Index.php create mode 100644 modules/Demo/app/Http/Controllers/Api/Index.php create mode 100644 modules/Demo/app/Providers/.gitkeep create mode 100644 modules/Demo/app/Providers/DemoServiceProvider.php create mode 100644 modules/Demo/app/Providers/EventServiceProvider.php create mode 100644 modules/Demo/app/Providers/RouteServiceProvider.php create mode 100644 modules/Demo/composer.json create mode 100644 modules/Demo/config/.gitkeep create mode 100644 modules/Demo/config/config.php create mode 100644 modules/Demo/database/factories/.gitkeep create mode 100644 modules/Demo/database/migrations/.gitkeep create mode 100644 modules/Demo/database/seeders/.gitkeep create mode 100644 modules/Demo/database/seeders/DemoDatabaseSeeder.php create mode 100644 modules/Demo/module.json create mode 100644 modules/Demo/package.json create mode 100644 modules/Demo/resources/assets/.gitkeep create mode 100644 modules/Demo/resources/assets/js/app.js create mode 100644 modules/Demo/resources/assets/sass/app.scss create mode 100644 modules/Demo/resources/views/.gitkeep create mode 100644 modules/Demo/resources/views/components/layouts/master.blade.php create mode 100644 modules/Demo/resources/views/index.blade.php create mode 100644 modules/Demo/routes/.gitkeep create mode 100644 modules/Demo/routes/admin.php create mode 100644 modules/Demo/routes/api.php create mode 100644 modules/Demo/routes/web.php create mode 100644 modules/Demo/tests/Feature/.gitkeep create mode 100644 modules/Demo/tests/Unit/.gitkeep create mode 100644 modules/Demo/vite.config.js diff --git a/modules/Demo/app/Http/Controllers/.gitkeep b/modules/Demo/app/Http/Controllers/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/modules/Demo/app/Http/Controllers/Admin/Index.php b/modules/Demo/app/Http/Controllers/Admin/Index.php new file mode 100644 index 0000000..7959525 --- /dev/null +++ b/modules/Demo/app/Http/Controllers/Admin/Index.php @@ -0,0 +1,8 @@ +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/Demo/app/Providers/EventServiceProvider.php b/modules/Demo/app/Providers/EventServiceProvider.php new file mode 100644 index 0000000..1e31c61 --- /dev/null +++ b/modules/Demo/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/Demo/app/Providers/RouteServiceProvider.php b/modules/Demo/app/Providers/RouteServiceProvider.php new file mode 100644 index 0000000..1383315 --- /dev/null +++ b/modules/Demo/app/Providers/RouteServiceProvider.php @@ -0,0 +1,61 @@ +mapApiRoutes(); + $this->mapWebRoutes(); + $this->mapAdminRoutes(); + } + + /** + * 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')); + } + + /** + * Define the "api" routes for the application. + * + * These routes are typically stateless. + */ + protected function mapAdminRoutes(): void + { + Route::middleware('admin')->prefix('admin')->name('admin.')->group(module_path($this->name, '/routes/admin.php')); + } +} diff --git a/modules/Demo/composer.json b/modules/Demo/composer.json new file mode 100644 index 0000000..b711984 --- /dev/null +++ b/modules/Demo/composer.json @@ -0,0 +1,30 @@ +{ + "name": "nwidart/demo", + "description": "", + "authors": [ + { + "name": "Nicolas Widart", + "email": "n.widart@gmail.com" + } + ], + "extra": { + "laravel": { + "providers": [], + "aliases": { + + } + } + }, + "autoload": { + "psr-4": { + "Modules\\Demo\\": "app/", + "Modules\\Demo\\Database\\Factories\\": "database/factories/", + "Modules\\Demo\\Database\\Seeders\\": "database/seeders/" + } + }, + "autoload-dev": { + "psr-4": { + "Modules\\Demo\\Tests\\": "tests/" + } + } +} diff --git a/modules/Demo/config/.gitkeep b/modules/Demo/config/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/modules/Demo/config/config.php b/modules/Demo/config/config.php new file mode 100644 index 0000000..37aee66 --- /dev/null +++ b/modules/Demo/config/config.php @@ -0,0 +1,5 @@ + 'Demo', +]; diff --git a/modules/Demo/database/factories/.gitkeep b/modules/Demo/database/factories/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/modules/Demo/database/migrations/.gitkeep b/modules/Demo/database/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/modules/Demo/database/seeders/.gitkeep b/modules/Demo/database/seeders/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/modules/Demo/database/seeders/DemoDatabaseSeeder.php b/modules/Demo/database/seeders/DemoDatabaseSeeder.php new file mode 100644 index 0000000..e414036 --- /dev/null +++ b/modules/Demo/database/seeders/DemoDatabaseSeeder.php @@ -0,0 +1,16 @@ +call([]); + } +} diff --git a/modules/Demo/module.json b/modules/Demo/module.json new file mode 100644 index 0000000..797d87e --- /dev/null +++ b/modules/Demo/module.json @@ -0,0 +1,11 @@ +{ + "name": "Demo", + "alias": "demo", + "description": "", + "keywords": [], + "priority": 0, + "providers": [ + "Modules\\Demo\\Providers\\DemoServiceProvider" + ], + "files": [] +} diff --git a/modules/Demo/package.json b/modules/Demo/package.json new file mode 100644 index 0000000..d6fbfc8 --- /dev/null +++ b/modules/Demo/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/Demo/resources/assets/.gitkeep b/modules/Demo/resources/assets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/modules/Demo/resources/assets/js/app.js b/modules/Demo/resources/assets/js/app.js new file mode 100644 index 0000000..e69de29 diff --git a/modules/Demo/resources/assets/sass/app.scss b/modules/Demo/resources/assets/sass/app.scss new file mode 100644 index 0000000..e69de29 diff --git a/modules/Demo/resources/views/.gitkeep b/modules/Demo/resources/views/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/modules/Demo/resources/views/components/layouts/master.blade.php b/modules/Demo/resources/views/components/layouts/master.blade.php new file mode 100644 index 0000000..fbfd12b --- /dev/null +++ b/modules/Demo/resources/views/components/layouts/master.blade.php @@ -0,0 +1,30 @@ + + + + + + + + + + Demo Module - {{ config('app.name', 'Laravel') }} + + + + + + + + + + {{-- Vite CSS --}} + {{-- {{ module_vite('build-demo', 'resources/assets/sass/app.scss') }} --}} + + + + {{ $slot }} + + {{-- Vite JS --}} + {{-- {{ module_vite('build-demo', 'resources/assets/js/app.js') }} --}} + + diff --git a/modules/Demo/resources/views/index.blade.php b/modules/Demo/resources/views/index.blade.php new file mode 100644 index 0000000..b6d0623 --- /dev/null +++ b/modules/Demo/resources/views/index.blade.php @@ -0,0 +1,5 @@ + +

Hello World

+ +

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

+
diff --git a/modules/Demo/routes/.gitkeep b/modules/Demo/routes/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/modules/Demo/routes/admin.php b/modules/Demo/routes/admin.php new file mode 100644 index 0000000..ff7eafd --- /dev/null +++ b/modules/Demo/routes/admin.php @@ -0,0 +1,4 @@ +group(function () { + Route::resource('demos', DemoController::class)->names('demo'); +}); diff --git a/modules/Demo/tests/Feature/.gitkeep b/modules/Demo/tests/Feature/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/modules/Demo/tests/Unit/.gitkeep b/modules/Demo/tests/Unit/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/modules/Demo/vite.config.js b/modules/Demo/vite.config.js new file mode 100644 index 0000000..0ccf436 --- /dev/null +++ b/modules/Demo/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-demo', + emptyOutDir: true, + manifest: true, + }, + plugins: [ + laravel({ + publicDirectory: '../../public', + buildDirectory: 'build-demo', + 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/Demo/'+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/Demo/resources/assets/sass/app.scss', +// 'Modules/Demo/resources/assets/js/app.js', +//];