├── src ├── Commands │ ├── stubs │ │ ├── assets │ │ │ ├── js │ │ │ │ └── app.stub │ │ │ └── sass │ │ │ │ └── app.stub │ │ ├── scaffold │ │ │ ├── config.stub │ │ │ └── provider.stub │ │ ├── observer-plain.stub │ │ ├── lang │ │ │ └── general.stub │ │ ├── controller-plain.stub │ │ ├── views │ │ │ ├── index.stub │ │ │ └── master.stub │ │ ├── json.stub │ │ ├── composer.stub │ │ ├── webpack.stub │ │ ├── policy.plain.stub │ │ ├── unit-test.stub │ │ ├── resource.stub │ │ ├── seeder.stub │ │ ├── middleware.stub │ │ ├── resource-collection.stub │ │ ├── provider.stub │ │ ├── migration │ │ │ ├── plain.stub │ │ │ ├── add.stub │ │ │ ├── delete.stub │ │ │ ├── drop.stub │ │ │ └── create.stub │ │ ├── event.stub │ │ ├── model.stub │ │ ├── factory.stub │ │ ├── routes │ │ │ ├── web.stub │ │ │ └── api.stub │ │ ├── exception.stub │ │ ├── job.stub │ │ ├── listener-duck.stub │ │ ├── component.stub │ │ ├── listener.stub │ │ ├── request.stub │ │ ├── listener-queued-duck.stub │ │ ├── command.stub │ │ ├── listener-queued.stub │ │ ├── mail.stub │ │ ├── job-queued.stub │ │ ├── rule.stub │ │ ├── cast.stub │ │ ├── package.stub │ │ ├── controller-api.stub │ │ ├── observer.stub │ │ ├── notification.stub │ │ └── controller.stub │ ├── UnUseCommand.php │ ├── DeleteCommand.php │ ├── EnableCommand.php │ ├── DisableCommand.php │ ├── UseCommand.php │ ├── DumpCommand.php │ ├── UpdateCommand.php │ ├── PublishMigrationCommand.php │ ├── ModuleMakeCommand.php │ ├── SetupCommand.php │ ├── TestMakeCommand.php │ ├── PublishCommand.php │ ├── CastMakeCommand.php │ ├── PublishTranslationCommand.php │ ├── ExceptionMakeCommand.php │ ├── EventMakeCommand.php │ ├── MigrateRefreshCommand.php │ ├── ListCommand.php │ ├── PublishConfigurationCommand.php │ ├── MailMakeCommand.php │ ├── RuleMakeCommand.php │ ├── PolicyMakeCommand.php │ ├── RequestMakeCommand.php │ ├── MiddlewareMakeCommand.php │ ├── NotificationMakeCommand.php │ ├── MigrateStatusCommand.php │ ├── GeneratorCommand.php │ ├── ResourceMakeCommand.php │ ├── CommandMakeCommand.php │ ├── SeedMakeCommand.php │ ├── FactoryMakeCommand.php │ ├── JobMakeCommand.php │ ├── MigrateResetCommand.php │ ├── MigrateRollbackCommand.php │ ├── MigrateCommand.php │ ├── ObserverMakeCommand.php │ ├── ListenerMakeCommand.php │ ├── ControllerMakeCommand.php │ ├── ComponentMakeCommand.php │ ├── InstallCommand.php │ ├── ProviderMakeCommand.php │ ├── MigrationMakeCommand.php │ └── ModelMakeCommand.php ├── Generators │ ├── Generator.php │ └── FileGenerator.php ├── Exceptions │ ├── InvalidJsonException.php │ ├── ModuleNotFoundException.php │ ├── FileAlreadyExistException.php │ └── InvalidAssetPath.php ├── Contracts │ ├── PublisherInterface.php │ ├── RunableInterface.php │ ├── ActivatorInterface.php │ └── RepositoryInterface.php ├── Support │ ├── Config │ │ ├── GenerateConfigReader.php │ │ └── GeneratorPath.php │ ├── Stub.php │ └── Migrations │ │ └── NameParser.php ├── Facade.php ├── Lumen │ ├── LumenFileRepository.php │ └── Module.php ├── Traits │ ├── CanClearModulesCache.php │ ├── ModuleCommandTrait.php │ └── MigrationLoaderTrait.php ├── Laravel │ ├── LaravelFileRepository.php │ └── Module.php ├── Routing │ └── Controller.php ├── Providers │ ├── Contracts.php │ ├── Bootstrap.php │ ├── Main.php │ ├── Lumen.php │ ├── Laravel.php │ └── Console.php ├── Process │ ├── Runner.php │ └── Updater.php ├── Publishing │ ├── AssetPublisher.php │ ├── LangPublisher.php │ ├── MigrationPublisher.php │ └── Publisher.php ├── Collection.php ├── helpers.php ├── Activators │ └── File.php └── Json.php ├── .styleci.yml ├── .gitignore ├── .editorconfig ├── .gitattributes ├── phpunit.xml ├── LICENSE.md ├── composer.json └── README.md /src/Commands/stubs/assets/js/app.stub: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/Commands/stubs/assets/sass/app.stub: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: psr2 2 | 3 | enabled: 4 | - concat_with_spaces -------------------------------------------------------------------------------- /src/Commands/stubs/scaffold/config.stub: -------------------------------------------------------------------------------- 1 | '$STUDLY_NAME$' 5 | ]; 6 | -------------------------------------------------------------------------------- /src/Commands/stubs/observer-plain.stub: -------------------------------------------------------------------------------- 1 | '$STUDLY_NAME$', 6 | 'description' => 'This is my awesome module', 7 | 8 | ]; -------------------------------------------------------------------------------- /src/Commands/stubs/controller-plain.stub: -------------------------------------------------------------------------------- 1 | Hello World 5 | 6 |

7 | This view is loaded from module: {!! config('$ALIAS$.name') !!} 8 |

9 | @endsection 10 | -------------------------------------------------------------------------------- /src/Contracts/PublisherInterface.php: -------------------------------------------------------------------------------- 1 | assertTrue(true); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Lumen/LumenFileRepository.php: -------------------------------------------------------------------------------- 1 | forget(config('module.cache.key')); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Laravel/LaravelFileRepository.php: -------------------------------------------------------------------------------- 1 | call("OthersTableSeeder"); 20 | 21 | Model::reguard(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Commands/stubs/middleware.stub: -------------------------------------------------------------------------------- 1 | app->bind(RepositoryInterface::class, LaravelFileRepository::class); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Contracts/ActivatorInterface.php: -------------------------------------------------------------------------------- 1 | group(function() { 15 | Route::get('/', '$STUDLY_NAME$Controller@index'); 16 | }); 17 | -------------------------------------------------------------------------------- /src/Commands/stubs/exception.stub: -------------------------------------------------------------------------------- 1 | app[RepositoryInterface::class]->boot(); 16 | } 17 | 18 | /** 19 | * Register the provider. 20 | */ 21 | public function register() 22 | { 23 | $this->app[RepositoryInterface::class]->register(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Commands/stubs/component.stub: -------------------------------------------------------------------------------- 1 | get('/$ALIAS$', function (Request $request) { 17 | return $request->user(); 18 | }); -------------------------------------------------------------------------------- /src/Commands/stubs/listener.stub: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Module $STUDLY_NAME$ 8 | 9 | {{-- Laravel Mix - CSS File --}} 10 | {{-- --}} 11 | 12 | 13 | 14 | @yield('content') 15 | 16 | {{-- Laravel Mix - JS File --}} 17 | {{-- --}} 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/Commands/stubs/command.stub: -------------------------------------------------------------------------------- 1 | module = $module; 19 | } 20 | 21 | /** 22 | * Run the given command. 23 | * 24 | * @param string $command 25 | */ 26 | public function run($command) 27 | { 28 | passthru($command); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set the default behavior, in case people don't have core.autocrlf set. 2 | * text eol=lf 3 | 4 | # Explicitly declare text files you want to always be normalized and converted 5 | # to native line endings on checkout. 6 | *.c text 7 | *.h text 8 | 9 | # Declare files that will always have CRLF line endings on checkout. 10 | *.sln text eol=crlf 11 | 12 | # Denote all files that are truly binary and should not be modified. 13 | *.png binary 14 | *.jpg binary 15 | *.otf binary 16 | *.eot binary 17 | *.svg binary 18 | *.ttf binary 19 | *.woff binary 20 | *.woff2 binary 21 | 22 | *.css linguist-vendored 23 | *.scss linguist-vendored 24 | *.js linguist-vendored 25 | CHANGELOG.md export-ignore 26 | -------------------------------------------------------------------------------- /src/Commands/stubs/mail.stub: -------------------------------------------------------------------------------- 1 | view('view.name'); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Support/Config/GeneratorPath.php: -------------------------------------------------------------------------------- 1 | path = $config['path']; 14 | $this->generate = $config['generate']; 15 | 16 | return; 17 | } 18 | 19 | $this->path = $config; 20 | $this->generate = (bool) $config; 21 | } 22 | 23 | public function getPath() 24 | { 25 | return $this->path; 26 | } 27 | 28 | public function generate() : bool 29 | { 30 | return $this->generate; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Commands/stubs/migration/add.stub: -------------------------------------------------------------------------------- 1 | laravel['module']->forgetUsed(); 29 | 30 | $this->info('Previous module used successfully forgotten.'); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Commands/stubs/migration/drop.stub: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 28 | $FIELDS$ 29 | $table->timestamps(); 30 | }); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Commands/stubs/migration/create.stub: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 18 | $FIELDS$ 19 | $table->timestamps(); 20 | }); 21 | } 22 | 23 | /** 24 | * Reverse the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function down() 29 | { 30 | Schema::dropIfExists('$TABLE$'); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Commands/stubs/job-queued.stub: -------------------------------------------------------------------------------- 1 | getSnakeName() . '_module.php', $this->app->basePath('storage/app/') . 'services.php'); 16 | } 17 | 18 | /** 19 | * {@inheritdoc} 20 | */ 21 | public function registerProviders() 22 | { 23 | foreach ($this->get('providers', []) as $provider) { 24 | $this->app->register($provider); 25 | } 26 | } 27 | 28 | /** 29 | * {@inheritdoc} 30 | */ 31 | public function registerAliases() 32 | { 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Commands/stubs/rule.stub: -------------------------------------------------------------------------------- 1 | argument('alias') ?: app('module')->getUsedNow(); 15 | 16 | $module = module()->findOrFail($alias); 17 | 18 | return $module; 19 | } 20 | 21 | /** 22 | * Get the module name. 23 | * 24 | * @return string 25 | */ 26 | public function getModuleName() 27 | { 28 | return $this->getModule()->getStudlyName(); 29 | } 30 | 31 | /** 32 | * Get the module name. 33 | * 34 | * @return string 35 | */ 36 | public function getModuleAlias() 37 | { 38 | return $this->getModule()->getAlias(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Traits/MigrationLoaderTrait.php: -------------------------------------------------------------------------------- 1 | laravel['module']->getModulePath($module) . $this->getMigrationGeneratorPath(); 15 | 16 | $files = $this->laravel['files']->glob($path . '/*_*.php'); 17 | 18 | foreach ($files as $file) { 19 | $this->laravel['files']->requireOnce($file); 20 | } 21 | } 22 | 23 | /** 24 | * Get migration generator path. 25 | * 26 | * @return string 27 | */ 28 | protected function getMigrationGeneratorPath() 29 | { 30 | return $this->laravel['module']->config('paths.generator.migration'); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Publishing/AssetPublisher.php: -------------------------------------------------------------------------------- 1 | repository->assetPath($this->module->getLowerName()); 24 | } 25 | 26 | /** 27 | * Get source path. 28 | * 29 | * @return string 30 | */ 31 | public function getSourcePath() 32 | { 33 | return $this->getModule()->getExtraPath( 34 | GenerateConfigReader::read('asset')->getPath() 35 | ); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Publishing/LangPublisher.php: -------------------------------------------------------------------------------- 1 | module->getLowerName(); 24 | 25 | return base_path("resources/lang/{$name}"); 26 | } 27 | 28 | /** 29 | * Get source path. 30 | * 31 | * @return string 32 | */ 33 | public function getSourcePath() 34 | { 35 | return $this->getModule()->getExtraPath( 36 | GenerateConfigReader::read('lang')->getPath() 37 | ); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Commands/stubs/cast.stub: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | ./src 13 | 14 | 15 | 16 | 17 | tests 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/Collection.php: -------------------------------------------------------------------------------- 1 | items; 18 | } 19 | 20 | /** 21 | * Get the collection of items as a plain array. 22 | * 23 | * @return array 24 | */ 25 | public function toArray() 26 | { 27 | return array_map(function ($value) { 28 | if ($value instanceof Module) { 29 | $attributes = $value->json()->getAttributes(); 30 | $attributes["path"] = $value->getPath(); 31 | 32 | return $attributes; 33 | } 34 | 35 | return $value instanceof Arrayable ? $value->toArray() : $value; 36 | }, $this->items); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Publishing/MigrationPublisher.php: -------------------------------------------------------------------------------- 1 | migrator = $migrator; 22 | 23 | parent::__construct($migrator->getModule()); 24 | } 25 | 26 | /** 27 | * Get destination path. 28 | * 29 | * @return string 30 | */ 31 | public function getDestinationPath() 32 | { 33 | return $this->repository->config('paths.migration'); 34 | } 35 | 36 | /** 37 | * Get source path. 38 | * 39 | * @return string 40 | */ 41 | public function getSourcePath() 42 | { 43 | return $this->migrator->getPath(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Commands/stubs/package.stub: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "scripts": { 4 | "dev": "npm run development", 5 | "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js", 6 | "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js", 7 | "watch-poll": "npm run watch -- --watch-poll", 8 | "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js", 9 | "prod": "npm run production", 10 | "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js" 11 | }, 12 | "devDependencies": { 13 | "cross-env": "^7.0", 14 | "laravel-mix": "^5.0.1", 15 | "laravel-mix-merge-manifest": "^0.1.2" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Commands/DeleteCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->findOrFail($this->argument('alias')); 30 | 31 | $module->delete(); 32 | 33 | $this->info("Module [{$module}] deleted successful."); 34 | } 35 | 36 | /** 37 | * Get the console command arguments. 38 | * 39 | * @return array 40 | */ 41 | protected function getArguments() 42 | { 43 | return [ 44 | ['alias', InputArgument::REQUIRED, 'Module alias.'], 45 | ]; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Laravel/Module.php: -------------------------------------------------------------------------------- 1 | getSnakeName() . '_module.php', $this->app->getCachedServicesPath()); 19 | } 20 | 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function registerProviders() 25 | { 26 | (new ProviderRepository($this->app, new Filesystem(), $this->getCachedServicesPath())) 27 | ->load($this->get('providers', [])); 28 | } 29 | 30 | /** 31 | * {@inheritdoc} 32 | */ 33 | public function registerAliases() 34 | { 35 | $loader = AliasLoader::getInstance(); 36 | foreach ($this->get('aliases', []) as $aliasName => $aliasClass) { 37 | $loader->alias($aliasName, $aliasClass); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Pingpong Labs 4 | 5 | Copyright (c) 2016 Nicolas Widart bvba n.widart@gmail.com 6 | 7 | Copyright (c) 2019 Akaunting 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included in all 17 | copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "akaunting/laravel-module", 3 | "description": "Module management package for Laravel", 4 | "keywords": [ 5 | "laravel", 6 | "module", 7 | "rad", 8 | "akaunting" 9 | ], 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Denis Duliçi", 14 | "email": "info@akaunting.com", 15 | "homepage": "https://akaunting.com", 16 | "role": "Developer" 17 | } 18 | ], 19 | "require": { 20 | "php": "^8.0" 21 | }, 22 | "require-dev": { 23 | "laravel/framework": "^9.0|^10.0", 24 | "orchestra/testbench": "^7.4|^8.0", 25 | "phpunit/phpunit": "^9.5|^10.0" 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "Akaunting\\Module\\": "src" 30 | }, 31 | "files": [ 32 | "src/helpers.php" 33 | ] 34 | }, 35 | "extra": { 36 | "laravel": { 37 | "providers": [ 38 | "Akaunting\\Module\\Providers\\Laravel" 39 | ], 40 | "aliases": { 41 | "Module": "Akaunting\\Module\\Facade" 42 | } 43 | } 44 | }, 45 | "minimum-stability": "dev", 46 | "prefer-stable": true 47 | } 48 | -------------------------------------------------------------------------------- /src/Commands/EnableCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->findOrFail($this->argument('alias')); 30 | 31 | if ($module->disabled()) { 32 | $module->enable(); 33 | 34 | $this->info("Module [{$module}] enabled successful."); 35 | } else { 36 | $this->comment("Module [{$module}] has already enabled."); 37 | } 38 | } 39 | 40 | /** 41 | * Get the console command arguments. 42 | * 43 | * @return array 44 | */ 45 | protected function getArguments() 46 | { 47 | return [ 48 | ['alias', InputArgument::REQUIRED, 'Module alias.'], 49 | ]; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Commands/DisableCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->findOrFail($this->argument('alias')); 30 | 31 | if ($module->enabled()) { 32 | $module->disable(); 33 | 34 | $this->info("Module [{$module}] disabled successful."); 35 | } else { 36 | $this->comment("Module [{$module}] has already disabled."); 37 | } 38 | } 39 | 40 | /** 41 | * Get the console command arguments. 42 | * 43 | * @return array 44 | */ 45 | protected function getArguments() 46 | { 47 | return [ 48 | ['alias', InputArgument::REQUIRED, 'Module alias.'], 49 | ]; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Commands/stubs/controller-api.stub: -------------------------------------------------------------------------------- 1 | argument('alias')); 31 | 32 | if (!$this->laravel['module']->has($alias)) { 33 | $this->error("Module [{$alias}] does not exists."); 34 | 35 | return; 36 | } 37 | 38 | $this->laravel['module']->setUsed($alias); 39 | 40 | $this->info("Module [{$alias}] used successfully."); 41 | } 42 | 43 | /** 44 | * Get the console command arguments. 45 | * 46 | * @return array 47 | */ 48 | protected function getArguments() 49 | { 50 | return [ 51 | ['alias', InputArgument::REQUIRED, 'The alias of module will be used.'], 52 | ]; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Commands/stubs/observer.stub: -------------------------------------------------------------------------------- 1 | app->register(Bootstrap::class); 30 | } 31 | 32 | /** 33 | * Register package's namespaces. 34 | */ 35 | protected function registerNamespaces() 36 | { 37 | $this->publishes([ 38 | __DIR__ . '/../Config/module.php' => config_path('module.php'), 39 | ], 'module'); 40 | } 41 | 42 | /** 43 | * Register the service provider. 44 | */ 45 | abstract protected function registerServices(); 46 | 47 | /** 48 | * Get the services provided by the provider. 49 | * 50 | * @return array 51 | */ 52 | public function provides() 53 | { 54 | return [RepositoryInterface::class, 'module']; 55 | } 56 | 57 | /** 58 | * Register providers. 59 | */ 60 | protected function registerProviders() 61 | { 62 | $this->app->register(Console::class); 63 | $this->app->register(Contracts::class); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/Commands/stubs/notification.stub: -------------------------------------------------------------------------------- 1 | line('The introduction to the notification.') 45 | ->action('Notification Action', 'https://laravel.com') 46 | ->line('Thank you for using our application!'); 47 | } 48 | 49 | /** 50 | * Get the array representation of the notification. 51 | * 52 | * @param mixed $notifiable 53 | * @return array 54 | */ 55 | public function toArray($notifiable) 56 | { 57 | return [ 58 | // 59 | ]; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Commands/DumpCommand.php: -------------------------------------------------------------------------------- 1 | info('Generating optimized autoload modules.'); 30 | 31 | if ($module = $this->argument('alias')) { 32 | $this->dump($module); 33 | } else { 34 | foreach ($this->laravel['module']->all() as $module) { 35 | $this->dump($module->getStudlyName()); 36 | } 37 | } 38 | } 39 | 40 | public function dump($module) 41 | { 42 | $module = $this->laravel['module']->findOrFail($module); 43 | 44 | $this->line("Running for module: {$module}"); 45 | 46 | chdir($module->getPath()); 47 | 48 | passthru('composer dump -o -n -q'); 49 | } 50 | 51 | /** 52 | * Get the console command arguments. 53 | * 54 | * @return array 55 | */ 56 | protected function getArguments() 57 | { 58 | return [ 59 | ['alias', InputArgument::OPTIONAL, 'Module alias.'], 60 | ]; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Providers/Lumen.php: -------------------------------------------------------------------------------- 1 | setupStubPath(); 18 | } 19 | 20 | /** 21 | * Register all modules. 22 | */ 23 | public function register() 24 | { 25 | $this->registerNamespaces(); 26 | $this->registerServices(); 27 | $this->registerModules(); 28 | $this->registerProviders(); 29 | } 30 | 31 | /** 32 | * Setup stub path. 33 | */ 34 | public function setupStubPath() 35 | { 36 | Stub::setBasePath(__DIR__ . '/Commands/stubs'); 37 | 38 | if (app('module')->config('stubs.enabled') === true) { 39 | Stub::setBasePath(app('module')->config('stubs.path')); 40 | } 41 | } 42 | 43 | /** 44 | * {@inheritdoc} 45 | */ 46 | protected function registerServices() 47 | { 48 | $this->app->singleton(RepositoryInterface::class, function ($app) { 49 | $path = $app['config']->get('module.paths.modules'); 50 | 51 | return new LumenFileRepository($app, $path); 52 | }); 53 | 54 | $this->app->singleton(ActivatorInterface::class, function ($app) { 55 | $class = $app['config']->get('module.activator'); 56 | 57 | return new $class($app); 58 | }); 59 | 60 | $this->app->alias(RepositoryInterface::class, 'module'); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Commands/UpdateCommand.php: -------------------------------------------------------------------------------- 1 | argument('alias'); 33 | 34 | if ($alias) { 35 | $this->updateModule($alias); 36 | 37 | return; 38 | } 39 | 40 | /** @var \Akaunting\Module\Module $module */ 41 | foreach ($this->laravel['module']->getOrdered() as $module) { 42 | $this->updateModule($module->getAlias()); 43 | } 44 | } 45 | 46 | protected function updateModule($alias) 47 | { 48 | $this->line('Running for module: ' . $alias . ''); 49 | 50 | $this->laravel['module']->update($alias); 51 | 52 | $this->info("Module [{$alias}] updated successfully."); 53 | } 54 | 55 | /** 56 | * Get the console command arguments. 57 | * 58 | * @return array 59 | */ 60 | protected function getArguments() 61 | { 62 | return [ 63 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be updated.'], 64 | ]; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Commands/stubs/controller.stub: -------------------------------------------------------------------------------- 1 | argument('alias')) { 32 | $module = $this->laravel['module']->findOrFail($alias); 33 | 34 | $this->publish($module); 35 | 36 | return; 37 | } 38 | 39 | foreach ($this->laravel['module']->allEnabled() as $module) { 40 | $this->publish($module); 41 | } 42 | } 43 | 44 | /** 45 | * Publish migration for the specified module. 46 | * 47 | * @param \Akaunting\Module\Module $module 48 | */ 49 | public function publish($module) 50 | { 51 | with(new MigrationPublisher(new Migrator($module, $this->getLaravel()))) 52 | ->setRepository($this->laravel['module']) 53 | ->setConsole($this) 54 | ->publish(); 55 | } 56 | 57 | /** 58 | * Get the console command arguments. 59 | * 60 | * @return array 61 | */ 62 | protected function getArguments() 63 | { 64 | return [ 65 | ['alias', InputArgument::OPTIONAL, 'The alias of module being used.'], 66 | ]; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/Commands/ModuleMakeCommand.php: -------------------------------------------------------------------------------- 1 | argument('alias'); 32 | 33 | foreach ($aliases as $alias) { 34 | with(new ModuleGenerator($alias)) 35 | ->setFilesystem($this->laravel['files']) 36 | ->setModule($this->laravel['module']) 37 | ->setConfig($this->laravel['config']) 38 | ->setConsole($this) 39 | ->setForce($this->option('force')) 40 | ->setPlain($this->option('plain')) 41 | ->generate(); 42 | } 43 | } 44 | 45 | /** 46 | * Get the console command arguments. 47 | * 48 | * @return array 49 | */ 50 | protected function getArguments() 51 | { 52 | return [ 53 | ['alias', InputArgument::IS_ARRAY, 'The aliases of modules will be created.'], 54 | ]; 55 | } 56 | 57 | protected function getOptions() 58 | { 59 | return [ 60 | ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain module (without some resources).'], 61 | ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when the module already exists.'], 62 | ]; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Commands/SetupCommand.php: -------------------------------------------------------------------------------- 1 | generateModulesFolder(); 29 | 30 | $this->generateAssetsFolder(); 31 | } 32 | 33 | /** 34 | * Generate the modules folder. 35 | */ 36 | public function generateModulesFolder() 37 | { 38 | $this->generateDirectory( 39 | $this->laravel['module']->config('paths.modules'), 40 | 'Modules directory created successfully', 41 | 'Modules directory already exist' 42 | ); 43 | } 44 | 45 | /** 46 | * Generate the assets folder. 47 | */ 48 | public function generateAssetsFolder() 49 | { 50 | $this->generateDirectory( 51 | $this->laravel['module']->config('paths.assets'), 52 | 'Assets directory created successfully', 53 | 'Assets directory already exist' 54 | ); 55 | } 56 | 57 | /** 58 | * Generate the specified directory by given $dir. 59 | * 60 | * @param $dir 61 | * @param $success 62 | * @param $error 63 | */ 64 | protected function generateDirectory($dir, $success, $error) 65 | { 66 | if (!$this->laravel['files']->isDirectory($dir)) { 67 | $this->laravel['files']->makeDirectory($dir, 0755, true, true); 68 | 69 | $this->info($success); 70 | 71 | return; 72 | } 73 | 74 | $this->error($error); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/Commands/TestMakeCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->config('paths.generator.test.path', 'Tests'); 22 | } 23 | 24 | /** 25 | * Get the console command arguments. 26 | * 27 | * @return array 28 | */ 29 | protected function getArguments() 30 | { 31 | return [ 32 | ['name', InputArgument::REQUIRED, 'The name of the form request class.'], 33 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 34 | ]; 35 | } 36 | 37 | /** 38 | * @return mixed 39 | */ 40 | protected function getTemplateContents() 41 | { 42 | $module = $this->getModule(); 43 | 44 | return (new Stub('/unit-test.stub', [ 45 | 'ALIAS' => $module->getAlias(), 46 | 'NAMESPACE' => $this->getClassNamespace($module), 47 | 'CLASS' => $this->getClass(), 48 | ]))->render(); 49 | } 50 | 51 | /** 52 | * @return mixed 53 | */ 54 | protected function getDestinationFilePath() 55 | { 56 | $path = module()->getModulePath($this->getModuleAlias()); 57 | 58 | $testPath = GenerateConfigReader::read('test'); 59 | 60 | return $path . $testPath->getPath() . '/' . $this->getFileName() . '.php'; 61 | } 62 | 63 | /** 64 | * @return string 65 | */ 66 | private function getFileName() 67 | { 68 | return Str::studly($this->argument('name')); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Commands/PublishCommand.php: -------------------------------------------------------------------------------- 1 | argument('alias')) { 32 | $this->publish($alias); 33 | 34 | return; 35 | } 36 | 37 | $this->publishAll(); 38 | } 39 | 40 | /** 41 | * Publish assets from all modules. 42 | */ 43 | public function publishAll() 44 | { 45 | foreach ($this->laravel['module']->allEnabled() as $module) { 46 | $this->publish($module); 47 | } 48 | } 49 | 50 | /** 51 | * Publish assets from the specified module. 52 | * 53 | * @param string $alias 54 | */ 55 | public function publish($alias) 56 | { 57 | if ($alias instanceof Module) { 58 | $module = $alias; 59 | } else { 60 | $module = $this->laravel['module']->findOrFail($alias); 61 | } 62 | 63 | with(new AssetPublisher($module)) 64 | ->setRepository($this->laravel['module']) 65 | ->setConsole($this) 66 | ->publish(); 67 | 68 | $this->line("Published: {$alias}"); 69 | } 70 | 71 | /** 72 | * Get the console command arguments. 73 | * 74 | * @return array 75 | */ 76 | protected function getArguments() 77 | { 78 | return [ 79 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 80 | ]; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/Commands/CastMakeCommand.php: -------------------------------------------------------------------------------- 1 | getModule(); 34 | 35 | return (new Stub('/cast.stub', [ 36 | 'NAMESPACE' => $this->getClassNamespace($module), 37 | 'CLASS' => $this->getClass(), 38 | ]))->render(); 39 | } 40 | 41 | public function getDestinationFilePath() 42 | { 43 | $path = $this->laravel['module']->getModulePath($this->getModuleAlias()); 44 | 45 | $castPath = GenerateConfigReader::read('cast'); 46 | 47 | return $path . $castPath->getPath() . '/' . $this->getFileName() . '.php'; 48 | } 49 | 50 | /** 51 | * @return string 52 | */ 53 | protected function getFileName() 54 | { 55 | return Str::studly($this->argument('name')); 56 | } 57 | 58 | public function getDefaultNamespace(): string 59 | { 60 | return $this->laravel['module']->config('paths.generator.cast.path', 'Casts'); 61 | } 62 | 63 | /** 64 | * Get the console command arguments. 65 | * 66 | * @return array 67 | */ 68 | protected function getArguments() 69 | { 70 | return [ 71 | ['name', InputArgument::REQUIRED, 'The name of the cast.'], 72 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 73 | ]; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Module management package for Laravel 2 | 3 | ![Downloads](https://img.shields.io/packagist/dt/akaunting/laravel-module) 4 | [![StyleCI](https://github.styleci.io/repos/180859866/shield?style=flat&branch=master)](https://styleci.io/repos/180859866) 5 | [![License](https://img.shields.io/github/license/akaunting/laravel-module)](LICENSE.md) 6 | 7 | This package intends to make your Laravel app extensible via modules. A module is a kinda small Laravel app, shipping with its own views, controllers, models, etc. 8 | 9 | ## Getting Started 10 | 11 | ### 1. Install 12 | 13 | Run the following command: 14 | 15 | ```bash 16 | composer require akaunting/laravel-module 17 | ``` 18 | 19 | ### 2. Register 20 | 21 | Service provider and facade will be registered automatically. If you want to register them manually in `config/app.php`: 22 | 23 | ```php 24 | Akaunting\Module\Facade::class, 25 | Akaunting\Module\Providers\Laravel::class, 26 | ``` 27 | 28 | ### 3. Publish 29 | 30 | Publish config file. 31 | 32 | ```bash 33 | php artisan vendor:publish --tag=module 34 | ``` 35 | 36 | ### 4. Configure 37 | 38 | You can change the configuration from `config/module.php` file 39 | 40 | ### 5. Autoloading 41 | 42 | By default, the module classes are not loaded automatically. You can autoload your modules using `psr-4`. For example: 43 | 44 | ``` json 45 | { 46 | "autoload": { 47 | "psr-4": { 48 | "App\\": "app/", 49 | "Modules\\": "modules/" 50 | } 51 | } 52 | } 53 | ``` 54 | 55 | **Tip: don't forget to run `composer dump-autoload` afterwards.** 56 | 57 | ## Usage 58 | 59 | Check out the [wiki](../../wiki) about the usage and further documentation. 60 | 61 | ## Changelog 62 | 63 | Please see [Releases](../../releases) for more information what has changed recently. 64 | 65 | ## Contributing 66 | 67 | Pull requests are more than welcome. You must follow the PSR coding standards. 68 | 69 | ## Credits 70 | 71 | - [Denis Duliçi](https://github.com/denisdulici) 72 | - [Nicolas Widart](https://github.com/nwidart) 73 | - [All Contributors](../../contributors) 74 | 75 | ## License 76 | 77 | The MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information. 78 | -------------------------------------------------------------------------------- /src/Commands/PublishTranslationCommand.php: -------------------------------------------------------------------------------- 1 | argument('alias')) { 32 | $this->publish($alias); 33 | 34 | return; 35 | } 36 | 37 | $this->publishAll(); 38 | } 39 | 40 | /** 41 | * Publish assets from all modules. 42 | */ 43 | public function publishAll() 44 | { 45 | foreach ($this->laravel['module']->allEnabled() as $module) { 46 | $this->publish($module); 47 | } 48 | } 49 | 50 | /** 51 | * Publish assets from the specified module. 52 | * 53 | * @param string $alias 54 | */ 55 | public function publish($alias) 56 | { 57 | if ($alias instanceof Module) { 58 | $module = $alias; 59 | } else { 60 | $module = $this->laravel['module']->findOrFail($alias); 61 | } 62 | 63 | with(new LangPublisher($module)) 64 | ->setRepository($this->laravel['module']) 65 | ->setConsole($this) 66 | ->publish(); 67 | 68 | $this->line("Published: {$module->getStudlyName()}"); 69 | } 70 | 71 | /** 72 | * Get the console command arguments. 73 | * 74 | * @return array 75 | */ 76 | protected function getArguments() 77 | { 78 | return [ 79 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 80 | ]; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | get($alias); 20 | } 21 | } 22 | 23 | if (!function_exists('module_attribute')) { 24 | /** 25 | * Get an attribute from module.json file 26 | * 27 | * @param string $alias 28 | * @param string $attribute 29 | * 30 | * @return mixed 31 | */ 32 | function module_attribute($alias, $attribute) 33 | { 34 | return app('module')->get($alias)->get($attribute); 35 | } 36 | } 37 | 38 | if (!function_exists('module_version')) { 39 | /** 40 | * Get the module version 41 | * 42 | * @param string $alias 43 | * 44 | * @return mixed 45 | */ 46 | function module_version($alias) 47 | { 48 | return app('module')->get($alias)->get('version'); 49 | } 50 | } 51 | 52 | if (!function_exists('module_path')) { 53 | /** 54 | * Get the module path 55 | * 56 | * @param string $alias 57 | * 58 | * @return mixed 59 | */ 60 | function module_path($alias, $path = '') 61 | { 62 | return app('module')->get($alias)->getPath() . ($path ? '/' . $path : $path); 63 | } 64 | } 65 | 66 | if (!function_exists('config_path')) { 67 | /** 68 | * Get the configuration path. 69 | * 70 | * @param string $path 71 | * 72 | * @return string 73 | */ 74 | function config_path($path = '') 75 | { 76 | return app()->basePath() . '/config' . ($path ? '/' . $path : $path); 77 | } 78 | } 79 | 80 | if (!function_exists('public_path')) { 81 | /** 82 | * Get the path to the public folder. 83 | * 84 | * @param string $path 85 | * 86 | * @return string 87 | */ 88 | function public_path($path = '') 89 | { 90 | return app()->make('path.public') . ($path ? '/' . $path : $path); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/Providers/Laravel.php: -------------------------------------------------------------------------------- 1 | registerNamespaces(); 18 | $this->registerModules(); 19 | } 20 | 21 | /** 22 | * Register the service provider. 23 | */ 24 | public function register() 25 | { 26 | $this->registerServices(); 27 | $this->setupStubPath(); 28 | $this->registerProviders(); 29 | $this->registerConfig(); 30 | } 31 | 32 | /** 33 | * Setup stub path. 34 | */ 35 | public function setupStubPath() 36 | { 37 | $path = $this->app['config']->get('module.stubs.path') ?? __DIR__ . '/Commands/stubs'; 38 | 39 | Stub::setBasePath($path); 40 | 41 | $this->app->booted(function ($app) { 42 | $repository = $app[RepositoryInterface::class]; 43 | 44 | if ($repository->config('stubs.enabled') === true) { 45 | Stub::setBasePath($repository->config('stubs.path')); 46 | } 47 | }); 48 | } 49 | 50 | /** 51 | * {@inheritdoc} 52 | */ 53 | protected function registerServices() 54 | { 55 | $this->app->singleton(RepositoryInterface::class, function ($app) { 56 | $path = $app['config']->get('module.paths.modules'); 57 | 58 | return new LaravelFileRepository($app, $path); 59 | }); 60 | 61 | $this->app->singleton(ActivatorInterface::class, function ($app) { 62 | $class = $app['config']->get('module.activator'); 63 | 64 | return new $class($app); 65 | }); 66 | 67 | $this->app->alias(RepositoryInterface::class, 'module'); 68 | } 69 | 70 | /** 71 | * Register module config. 72 | */ 73 | public function registerConfig() 74 | { 75 | $this->mergeConfigFrom(__DIR__ . '/../Config/module.php', 'module'); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Commands/ExceptionMakeCommand.php: -------------------------------------------------------------------------------- 1 | getModule(); 34 | 35 | return (new Stub('/exception.stub', [ 36 | 'NAMESPACE' => $this->getClassNamespace($module), 37 | 'CLASS' => $this->getClass(), 38 | ]))->render(); 39 | } 40 | 41 | public function getDestinationFilePath() 42 | { 43 | $path = $this->laravel['module']->getModulePath($this->getModuleAlias()); 44 | 45 | $exceptionPath = GenerateConfigReader::read('exception'); 46 | 47 | return $path . $exceptionPath->getPath() . '/' . $this->getFileName() . '.php'; 48 | } 49 | 50 | /** 51 | * @return string 52 | */ 53 | protected function getFileName() 54 | { 55 | return Str::studly($this->argument('name')); 56 | } 57 | 58 | public function getDefaultNamespace(): string 59 | { 60 | return $this->laravel['module']->config('paths.generator.exception.path', 'Exceptions'); 61 | } 62 | 63 | /** 64 | * Get the console command arguments. 65 | * 66 | * @return array 67 | */ 68 | protected function getArguments() 69 | { 70 | return [ 71 | ['name', InputArgument::REQUIRED, 'The name of the exception.'], 72 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 73 | ]; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/Commands/EventMakeCommand.php: -------------------------------------------------------------------------------- 1 | getModule(); 34 | 35 | return (new Stub('/event.stub', [ 36 | 'ALIAS' => $module->getAlias(), 37 | 'NAMESPACE' => $this->getClassNamespace($module), 38 | 'CLASS' => $this->getClass(), 39 | ]))->render(); 40 | } 41 | 42 | public function getDestinationFilePath() 43 | { 44 | $path = $this->laravel['module']->getModulePath($this->getModuleAlias()); 45 | 46 | $eventPath = GenerateConfigReader::read('event'); 47 | 48 | return $path . $eventPath->getPath() . '/' . $this->getFileName() . '.php'; 49 | } 50 | 51 | /** 52 | * @return string 53 | */ 54 | protected function getFileName() 55 | { 56 | return Str::studly($this->argument('name')); 57 | } 58 | 59 | public function getDefaultNamespace() : string 60 | { 61 | return $this->laravel['module']->config('paths.generator.event.path', 'Events'); 62 | } 63 | 64 | /** 65 | * Get the console command arguments. 66 | * 67 | * @return array 68 | */ 69 | protected function getArguments() 70 | { 71 | return [ 72 | ['name', InputArgument::REQUIRED, 'The name of the event.'], 73 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 74 | ]; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/Commands/MigrateRefreshCommand.php: -------------------------------------------------------------------------------- 1 | call('module:migrate-reset', [ 34 | 'alias' => $this->getModuleAlias(), 35 | '--database' => $this->option('database'), 36 | '--force' => $this->option('force'), 37 | ]); 38 | 39 | $this->call('module:migrate', [ 40 | 'alias' => $this->getModuleAlias(), 41 | '--database' => $this->option('database'), 42 | '--force' => $this->option('force'), 43 | ]); 44 | 45 | if ($this->option('seed')) { 46 | $this->call('module:seed', [ 47 | 'alias' => $this->getModuleAlias(), 48 | ]); 49 | } 50 | } 51 | 52 | /** 53 | * Get the console command arguments. 54 | * 55 | * @return array 56 | */ 57 | protected function getArguments() 58 | { 59 | return [ 60 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 61 | ]; 62 | } 63 | 64 | /** 65 | * Get the console command options. 66 | * 67 | * @return array 68 | */ 69 | protected function getOptions() 70 | { 71 | return [ 72 | ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'], 73 | ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'], 74 | ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'], 75 | ]; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Commands/ListCommand.php: -------------------------------------------------------------------------------- 1 | table(['Alias', 'Status', 'Order', 'Path'], $this->getRows()); 30 | } 31 | 32 | /** 33 | * Get table rows. 34 | * 35 | * @return array 36 | */ 37 | public function getRows() 38 | { 39 | $rows = []; 40 | 41 | foreach ($this->getModules() as $module) { 42 | $rows[] = [ 43 | $module->getAlias(), 44 | $module->enabled() ? 'Enabled' : 'Disabled', 45 | $module->get('order'), 46 | $module->getPath(), 47 | ]; 48 | } 49 | 50 | return $rows; 51 | } 52 | 53 | public function getModules() 54 | { 55 | switch ($this->option('only')) { 56 | case 'enabled': 57 | return $this->laravel['module']->getByStatus(1); 58 | break; 59 | 60 | case 'disabled': 61 | return $this->laravel['module']->getByStatus(0); 62 | break; 63 | 64 | case 'ordered': 65 | return $this->laravel['module']->getOrdered($this->option('direction')); 66 | break; 67 | 68 | default: 69 | return $this->laravel['module']->all(); 70 | break; 71 | } 72 | } 73 | 74 | /** 75 | * Get the console command options. 76 | * 77 | * @return array 78 | */ 79 | protected function getOptions() 80 | { 81 | return [ 82 | ['only', null, InputOption::VALUE_OPTIONAL, 'Types of modules will be displayed.', null], 83 | ['direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'asc'], 84 | ]; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/Commands/PublishConfigurationCommand.php: -------------------------------------------------------------------------------- 1 | argument('alias')) { 32 | $this->publishConfiguration($alias); 33 | 34 | return; 35 | } 36 | 37 | foreach ($this->laravel['module']->allEnabled() as $module) { 38 | $this->publishConfiguration($module->getAlias()); 39 | } 40 | } 41 | 42 | /** 43 | * @param string $alias 44 | * @return string 45 | */ 46 | private function getServiceProviderForModule($alias) 47 | { 48 | $namespace = $this->laravel['config']->get('module.namespace'); 49 | $studlyName = Str::studly($alias); 50 | 51 | return "$namespace\\$studlyName\\Providers\\Main"; 52 | } 53 | 54 | /** 55 | * @param string $alias 56 | */ 57 | private function publishConfiguration($alias) 58 | { 59 | $this->call('vendor:publish', [ 60 | '--provider' => $this->getServiceProviderForModule($alias), 61 | '--force' => $this->option('force'), 62 | '--tag' => ['config'], 63 | ]); 64 | } 65 | 66 | /** 67 | * Get the console command arguments. 68 | * 69 | * @return array 70 | */ 71 | protected function getArguments() 72 | { 73 | return [ 74 | ['alias', InputArgument::OPTIONAL, 'The alias of module being used.'], 75 | ]; 76 | } 77 | 78 | /** 79 | * @return array 80 | */ 81 | protected function getOptions() 82 | { 83 | return [ 84 | ['--force', '-f', InputOption::VALUE_NONE, 'Force the publishing of config files'], 85 | ]; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/Commands/MailMakeCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->config('paths.generator.emails.path', 'Emails'); 34 | } 35 | 36 | /** 37 | * Get the console command arguments. 38 | * 39 | * @return array 40 | */ 41 | protected function getArguments() 42 | { 43 | return [ 44 | ['name', InputArgument::REQUIRED, 'The name of the mailable.'], 45 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 46 | ]; 47 | } 48 | 49 | /** 50 | * Get template contents. 51 | * 52 | * @return string 53 | */ 54 | protected function getTemplateContents() 55 | { 56 | $module = $this->getModule(); 57 | 58 | return (new Stub('/mail.stub', [ 59 | 'ALIAS' => $module->getAlias(), 60 | 'NAMESPACE' => $this->getClassNamespace($module), 61 | 'CLASS' => $this->getClass(), 62 | ]))->render(); 63 | } 64 | 65 | /** 66 | * Get the destination file path. 67 | * 68 | * @return string 69 | */ 70 | protected function getDestinationFilePath() 71 | { 72 | $path = module()->getModulePath($this->getModuleAlias()); 73 | 74 | $mailPath = GenerateConfigReader::read('email'); 75 | 76 | return $path . $mailPath->getPath() . '/' . $this->getFileName() . '.php'; 77 | } 78 | 79 | /** 80 | * @return string 81 | */ 82 | private function getFileName() 83 | { 84 | return Str::studly($this->argument('name')); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/Commands/RuleMakeCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->config('paths.generator.rules.path', 'Rules'); 39 | } 40 | 41 | /** 42 | * Get the console command arguments. 43 | * 44 | * @return array 45 | */ 46 | protected function getArguments() 47 | { 48 | return [ 49 | ['name', InputArgument::REQUIRED, 'The name of the rule class.'], 50 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 51 | ]; 52 | } 53 | 54 | /** 55 | * @return mixed 56 | */ 57 | protected function getTemplateContents() 58 | { 59 | $module = $this->getModule(); 60 | 61 | return (new Stub('/rule.stub', [ 62 | 'ALIAS' => $module->getAlias(), 63 | 'NAMESPACE' => $this->getClassNamespace($module), 64 | 'CLASS' => $this->getFileName(), 65 | ]))->render(); 66 | } 67 | 68 | /** 69 | * @return mixed 70 | */ 71 | protected function getDestinationFilePath() 72 | { 73 | $path = module()->getModulePath($this->getModuleAlias()); 74 | 75 | $rulePath = GenerateConfigReader::read('rules'); 76 | 77 | return $path . $rulePath->getPath() . '/' . $this->getFileName() . '.php'; 78 | } 79 | 80 | /** 81 | * @return string 82 | */ 83 | private function getFileName() 84 | { 85 | return Str::studly($this->argument('name')); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/Commands/PolicyMakeCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->config('paths.generator.policies.path', 'Policies'); 39 | } 40 | 41 | /** 42 | * Get the console command arguments. 43 | * 44 | * @return array 45 | */ 46 | protected function getArguments() 47 | { 48 | return [ 49 | ['name', InputArgument::REQUIRED, 'The name of the policy class.'], 50 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 51 | ]; 52 | } 53 | 54 | /** 55 | * @return mixed 56 | */ 57 | protected function getTemplateContents() 58 | { 59 | $module = $this->getModule(); 60 | 61 | return (new Stub('/policy.plain.stub', [ 62 | 'ALIAS' => $module->getAlias(), 63 | 'NAMESPACE' => $this->getClassNamespace($module), 64 | 'CLASS' => $this->getClass(), 65 | ]))->render(); 66 | } 67 | 68 | /** 69 | * @return mixed 70 | */ 71 | protected function getDestinationFilePath() 72 | { 73 | $path = module()->getModulePath($this->getModuleAlias()); 74 | 75 | $policyPath = GenerateConfigReader::read('policy'); 76 | 77 | return $path . $policyPath->getPath() . '/' . $this->getFileName() . '.php'; 78 | } 79 | 80 | /** 81 | * @return string 82 | */ 83 | private function getFileName() 84 | { 85 | return Str::studly($this->argument('name')); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/Commands/RequestMakeCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->config('paths.generator.request.path', 'Http/Requests'); 39 | } 40 | 41 | /** 42 | * Get the console command arguments. 43 | * 44 | * @return array 45 | */ 46 | protected function getArguments() 47 | { 48 | return [ 49 | ['name', InputArgument::REQUIRED, 'The name of the form request class.'], 50 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 51 | ]; 52 | } 53 | 54 | /** 55 | * @return mixed 56 | */ 57 | protected function getTemplateContents() 58 | { 59 | $module = $this->getModule(); 60 | 61 | return (new Stub('/request.stub', [ 62 | 'ALIAS' => $module->getAlias(), 63 | 'NAMESPACE' => $this->getClassNamespace($module), 64 | 'CLASS' => $this->getClass(), 65 | ]))->render(); 66 | } 67 | 68 | /** 69 | * @return mixed 70 | */ 71 | protected function getDestinationFilePath() 72 | { 73 | $path = module()->getModulePath($this->getModuleAlias()); 74 | 75 | $requestPath = GenerateConfigReader::read('request'); 76 | 77 | return $path . $requestPath->getPath() . '/' . $this->getFileName() . '.php'; 78 | } 79 | 80 | /** 81 | * @return string 82 | */ 83 | private function getFileName() 84 | { 85 | return Str::studly($this->argument('name')); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/Commands/MiddlewareMakeCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->config('paths.generator.filter.path', 'Http/Middleware'); 39 | } 40 | 41 | /** 42 | * Get the console command arguments. 43 | * 44 | * @return array 45 | */ 46 | protected function getArguments() 47 | { 48 | return [ 49 | ['name', InputArgument::REQUIRED, 'The name of the command.'], 50 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 51 | ]; 52 | } 53 | 54 | /** 55 | * @return mixed 56 | */ 57 | protected function getTemplateContents() 58 | { 59 | $module = $this->getModule(); 60 | 61 | return (new Stub('/middleware.stub', [ 62 | 'ALIAS' => $module->getAlias(), 63 | 'NAMESPACE' => $this->getClassNamespace($module), 64 | 'CLASS' => $this->getClass(), 65 | ]))->render(); 66 | } 67 | 68 | /** 69 | * @return mixed 70 | */ 71 | protected function getDestinationFilePath() 72 | { 73 | $path = module()->getModulePath($this->getModuleAlias()); 74 | 75 | $middlewarePath = GenerateConfigReader::read('middleware'); 76 | 77 | return $path . $middlewarePath->getPath() . '/' . $this->getFileName() . '.php'; 78 | } 79 | 80 | /** 81 | * @return string 82 | */ 83 | private function getFileName() 84 | { 85 | return Str::studly($this->argument('name')); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/Commands/NotificationMakeCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->config('paths.generator.notifications.path', 'Notifications'); 34 | } 35 | 36 | /** 37 | * Get template contents. 38 | * 39 | * @return string 40 | */ 41 | protected function getTemplateContents() 42 | { 43 | $module = $this->getModule(); 44 | 45 | return (new Stub('/notification.stub', [ 46 | 'ALIAS' => $module->getAlias(), 47 | 'NAMESPACE' => $this->getClassNamespace($module), 48 | 'CLASS' => $this->getClass(), 49 | ]))->render(); 50 | } 51 | 52 | /** 53 | * Get the destination file path. 54 | * 55 | * @return string 56 | */ 57 | protected function getDestinationFilePath() 58 | { 59 | $path = module()->getModulePath($this->getModuleAlias()); 60 | 61 | $notificationPath = GenerateConfigReader::read('notification'); 62 | 63 | return $path . $notificationPath->getPath() . '/' . $this->getFileName() . '.php'; 64 | } 65 | 66 | /** 67 | * @return string 68 | */ 69 | private function getFileName() 70 | { 71 | return Str::studly($this->argument('name')); 72 | } 73 | 74 | /** 75 | * Get the console command arguments. 76 | * 77 | * @return array 78 | */ 79 | protected function getArguments() 80 | { 81 | return [ 82 | ['name', InputArgument::REQUIRED, 'The name of the notification class.'], 83 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 84 | ]; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/Process/Updater.php: -------------------------------------------------------------------------------- 1 | module->findOrFail($module); 17 | 18 | chdir(base_path()); 19 | 20 | $this->installRequires($module); 21 | $this->installDevRequires($module); 22 | $this->copyScriptsToMainComposerJson($module); 23 | } 24 | 25 | /** 26 | * @param Module $module 27 | */ 28 | private function installRequires(Module $module) 29 | { 30 | $packages = $module->getComposerAttr('require', []); 31 | 32 | $concatenatedPackages = ''; 33 | foreach ($packages as $name => $version) { 34 | $concatenatedPackages .= "\"{$name}:{$version}\" "; 35 | } 36 | 37 | if (!empty($concatenatedPackages)) { 38 | $this->run("composer require {$concatenatedPackages}"); 39 | } 40 | } 41 | 42 | /** 43 | * @param Module $module 44 | */ 45 | private function installDevRequires(Module $module) 46 | { 47 | $devPackages = $module->getComposerAttr('require-dev', []); 48 | 49 | $concatenatedPackages = ''; 50 | foreach ($devPackages as $name => $version) { 51 | $concatenatedPackages .= "\"{$name}:{$version}\" "; 52 | } 53 | 54 | if (!empty($concatenatedPackages)) { 55 | $this->run("composer require --dev {$concatenatedPackages}"); 56 | } 57 | } 58 | 59 | /** 60 | * @param Module $module 61 | */ 62 | private function copyScriptsToMainComposerJson(Module $module) 63 | { 64 | $scripts = $module->getComposerAttr('scripts', []); 65 | 66 | $composer = json_decode(file_get_contents(base_path('composer.json')), true); 67 | 68 | foreach ($scripts as $key => $script) { 69 | if (array_key_exists($key, $composer['scripts'])) { 70 | $composer['scripts'][$key] = array_unique(array_merge($composer['scripts'][$key], $script)); 71 | continue; 72 | } 73 | 74 | $composer['scripts'] = array_merge($composer['scripts'], [$key => $script]); 75 | } 76 | 77 | file_put_contents(base_path('composer.json'), json_encode($composer, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/Contracts/RepositoryInterface.php: -------------------------------------------------------------------------------- 1 | module = $this->laravel['module']; 40 | 41 | $alias = $this->argument('alias'); 42 | 43 | if ($alias) { 44 | $module = $this->module->findOrFail($alias); 45 | 46 | return $this->migrateStatus($module); 47 | } 48 | 49 | foreach ($this->module->getOrdered($this->option('direction')) as $module) { 50 | $this->line('Running for module: ' . $module->getAlias() . ''); 51 | $this->migrateStatus($module); 52 | } 53 | } 54 | 55 | /** 56 | * Run the migration from the specified module. 57 | * 58 | * @param Module $module 59 | */ 60 | protected function migrateStatus(Module $module) 61 | { 62 | $path = str_replace(base_path(), '', (new Migrator($module, $this->getLaravel()))->getPath()); 63 | 64 | $this->call('migrate:status', [ 65 | '--path' => $path, 66 | '--database' => $this->option('database'), 67 | ]); 68 | } 69 | 70 | /** 71 | * Get the console command arguments. 72 | * 73 | * @return array 74 | */ 75 | protected function getArguments() 76 | { 77 | return [ 78 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 79 | ]; 80 | } 81 | 82 | /** 83 | * Get the console command options. 84 | * 85 | * @return array 86 | */ 87 | protected function getOptions() 88 | { 89 | return [ 90 | ['direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'asc'], 91 | ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'], 92 | ]; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/Providers/Console.php: -------------------------------------------------------------------------------- 1 | commands($this->commands); 67 | } 68 | 69 | /** 70 | * @return array 71 | */ 72 | public function provides() 73 | { 74 | $provides = $this->commands; 75 | 76 | return $provides; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/Commands/GeneratorCommand.php: -------------------------------------------------------------------------------- 1 | getDestinationFilePath()); 38 | 39 | if (!$this->laravel['files']->isDirectory($dir = dirname($path))) { 40 | $this->laravel['files']->makeDirectory($dir, 0777, true); 41 | } 42 | 43 | $contents = $this->getTemplateContents(); 44 | 45 | try { 46 | $overwrite_file = $this->hasOption('force') ? $this->option('force') : false; 47 | (new FileGenerator($path, $contents))->withFileOverwrite($overwrite_file)->generate(); 48 | 49 | $this->info("Created : {$path}"); 50 | } catch (FileAlreadyExistException $e) { 51 | $this->error("File : {$path} already exists."); 52 | } 53 | } 54 | 55 | /** 56 | * Get class name. 57 | * 58 | * @return string 59 | */ 60 | public function getClass() 61 | { 62 | return class_basename($this->argument($this->argumentName)); 63 | } 64 | 65 | /** 66 | * Get default namespace. 67 | * 68 | * @return string 69 | */ 70 | public function getDefaultNamespace() : string 71 | { 72 | return ''; 73 | } 74 | 75 | /** 76 | * Get class namespace. 77 | * 78 | * @param \Akaunting\Module\Module $module 79 | * 80 | * @return string 81 | */ 82 | public function getClassNamespace($module) 83 | { 84 | $extra = str_replace($this->getClass(), '', $this->argument($this->argumentName)); 85 | 86 | $extra = str_replace('/', '\\', $extra); 87 | 88 | $namespace = $this->laravel['module']->config('namespace'); 89 | 90 | $namespace .= '\\' . $module->getStudlyName(); 91 | 92 | $namespace .= '\\' . $this->getDefaultNamespace(); 93 | 94 | $namespace .= '\\' . $extra; 95 | 96 | $namespace = str_replace('/', '\\', $namespace); 97 | 98 | return trim($namespace, '\\'); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/Commands/ResourceMakeCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->config('paths.generator.resource.path', 'Transformers'); 23 | } 24 | 25 | /** 26 | * Get the console command arguments. 27 | * 28 | * @return array 29 | */ 30 | protected function getArguments() 31 | { 32 | return [ 33 | ['name', InputArgument::REQUIRED, 'The name of the resource class.'], 34 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 35 | ]; 36 | } 37 | 38 | protected function getOptions() 39 | { 40 | return [ 41 | ['collection', 'c', InputOption::VALUE_NONE, 'Create a resource collection.'], 42 | ]; 43 | } 44 | 45 | /** 46 | * @return mixed 47 | */ 48 | protected function getTemplateContents() 49 | { 50 | $module = $this->getModule(); 51 | 52 | return (new Stub($this->getStubName(), [ 53 | 'ALIAS' => $module->getAlias(), 54 | 'NAMESPACE' => $this->getClassNamespace($module), 55 | 'CLASS' => $this->getClass(), 56 | ]))->render(); 57 | } 58 | 59 | /** 60 | * @return mixed 61 | */ 62 | protected function getDestinationFilePath() 63 | { 64 | $path = module()->getModulePath($this->getModuleAlias()); 65 | 66 | $resourcePath = GenerateConfigReader::read('resource'); 67 | 68 | return $path . $resourcePath->getPath() . '/' . $this->getFileName() . '.php'; 69 | } 70 | 71 | /** 72 | * @return string 73 | */ 74 | private function getFileName() 75 | { 76 | return Str::studly($this->argument('name')); 77 | } 78 | 79 | /** 80 | * Determine if the command is generating a resource collection. 81 | * 82 | * @return bool 83 | */ 84 | protected function collection() : bool 85 | { 86 | return $this->option('collection') || 87 | Str::endsWith($this->argument('name'), 'Collection'); 88 | } 89 | 90 | /** 91 | * @return string 92 | */ 93 | protected function getStubName(): string 94 | { 95 | if ($this->collection()) { 96 | return '/resource-collection.stub'; 97 | } 98 | 99 | return '/resource.stub'; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/Commands/CommandMakeCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->config('paths.generator.command.path', 'Console'); 40 | } 41 | 42 | /** 43 | * Get the console command arguments. 44 | * 45 | * @return array 46 | */ 47 | protected function getArguments() 48 | { 49 | return [ 50 | ['name', InputArgument::REQUIRED, 'The name of the command.'], 51 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 52 | ]; 53 | } 54 | 55 | /** 56 | * Get the console command options. 57 | * 58 | * @return array 59 | */ 60 | protected function getOptions() 61 | { 62 | return [ 63 | ['command', null, InputOption::VALUE_OPTIONAL, 'The terminal command that should be assigned.', null], 64 | ]; 65 | } 66 | 67 | /** 68 | * @return mixed 69 | */ 70 | protected function getTemplateContents() 71 | { 72 | $module = $this->getModule(); 73 | 74 | return (new Stub('/command.stub', [ 75 | 'ALIAS' => $this->getModuleAlias(), 76 | 'COMMAND_NAME' => $this->getCommandName(), 77 | 'NAMESPACE' => $this->getClassNamespace($module), 78 | 'CLASS' => $this->getClass(), 79 | ]))->render(); 80 | } 81 | 82 | /** 83 | * @return string 84 | */ 85 | private function getCommandName() 86 | { 87 | return $this->option('command') ?: 'command:name'; 88 | } 89 | 90 | /** 91 | * @return mixed 92 | */ 93 | protected function getDestinationFilePath() 94 | { 95 | $path = module()->getModulePath($this->getModuleAlias()); 96 | 97 | $commandPath = GenerateConfigReader::read('command'); 98 | 99 | return $path . $commandPath->getPath() . '/' . $this->getFileName() . '.php'; 100 | } 101 | 102 | /** 103 | * @return string 104 | */ 105 | private function getFileName() 106 | { 107 | return Str::studly($this->argument('name')); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/Commands/SeedMakeCommand.php: -------------------------------------------------------------------------------- 1 | getModule(); 69 | 70 | return (new Stub('/seeder.stub', [ 71 | 'NAME' => $this->getSeederName(), 72 | 'ALIAS' => $module->getAlias(), 73 | 'MODULE' => $this->getModuleName(), 74 | 'NAMESPACE' => $this->getClassNamespace($module), 75 | ]))->render(); 76 | } 77 | 78 | /** 79 | * @return mixed 80 | */ 81 | protected function getDestinationFilePath() 82 | { 83 | $this->clearCache(); 84 | 85 | $path = module()->getModulePath($this->getModuleAlias()); 86 | 87 | $seederPath = GenerateConfigReader::read('seeder'); 88 | 89 | return $path . $seederPath->getPath() . '/' . $this->getSeederName() . '.php'; 90 | } 91 | 92 | /** 93 | * Get seeder name. 94 | * 95 | * @return string 96 | */ 97 | private function getSeederName() 98 | { 99 | $end = $this->option('master') ? 'DatabaseSeeder' : ''; 100 | 101 | return Str::studly($this->argument('name')) . $end; 102 | } 103 | 104 | /** 105 | * Get default namespace. 106 | * 107 | * @return string 108 | */ 109 | public function getDefaultNamespace() : string 110 | { 111 | return module()->config('paths.generator.seeder.path', 'Database/Seeds'); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/Generators/FileGenerator.php: -------------------------------------------------------------------------------- 1 | path = $path; 41 | $this->contents = $contents; 42 | $this->filesystem = $filesystem ?: new Filesystem(); 43 | } 44 | 45 | /** 46 | * Get contents. 47 | * 48 | * @return mixed 49 | */ 50 | public function getContents() 51 | { 52 | return $this->contents; 53 | } 54 | 55 | /** 56 | * Set contents. 57 | * 58 | * @param mixed $contents 59 | * 60 | * @return $this 61 | */ 62 | public function setContents($contents) 63 | { 64 | $this->contents = $contents; 65 | 66 | return $this; 67 | } 68 | 69 | /** 70 | * Get filesystem. 71 | * 72 | * @return mixed 73 | */ 74 | public function getFilesystem() 75 | { 76 | return $this->filesystem; 77 | } 78 | 79 | /** 80 | * Set filesystem. 81 | * 82 | * @param Filesystem $filesystem 83 | * 84 | * @return $this 85 | */ 86 | public function setFilesystem(Filesystem $filesystem) 87 | { 88 | $this->filesystem = $filesystem; 89 | 90 | return $this; 91 | } 92 | 93 | /** 94 | * Get path. 95 | * 96 | * @return mixed 97 | */ 98 | public function getPath() 99 | { 100 | return $this->path; 101 | } 102 | 103 | /** 104 | * Set path. 105 | * 106 | * @param mixed $path 107 | * 108 | * @return $this 109 | */ 110 | public function setPath($path) 111 | { 112 | $this->path = $path; 113 | 114 | return $this; 115 | } 116 | 117 | public function withFileOverwrite(bool $overwrite): FileGenerator 118 | { 119 | $this->overwriteFile = $overwrite; 120 | 121 | return $this; 122 | } 123 | 124 | /** 125 | * Generate the file. 126 | */ 127 | public function generate() 128 | { 129 | $path = $this->getPath(); 130 | 131 | if (!$this->filesystem->exists($path)) { 132 | return $this->filesystem->put($path, $this->getContents()); 133 | } 134 | 135 | if ($this->overwriteFile === true) { 136 | return $this->filesystem->put($path, $this->getContents()); 137 | } 138 | 139 | throw new FileAlreadyExistException('File already exists!'); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/Commands/FactoryMakeCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->findOrFail($this->getModuleAlias()); 55 | 56 | return (new Stub('/factory.stub', [ 57 | 'NAMESPACE' => $this->getClassNamespace($module), 58 | 'NAME' => $this->getModelName(), 59 | 'MODEL_NAMESPACE' => $this->getModelNamespace(), 60 | ]))->render(); 61 | } 62 | 63 | /** 64 | * @return mixed 65 | */ 66 | protected function getDestinationFilePath() 67 | { 68 | $path = module()->getModulePath($this->getModuleAlias()); 69 | 70 | $factoryPath = GenerateConfigReader::read('factory'); 71 | 72 | return $path . $factoryPath->getPath() . '/' . $this->getFileName(); 73 | } 74 | 75 | /** 76 | * @return string 77 | */ 78 | private function getFileName() 79 | { 80 | return Str::studly($this->argument('name')) . '.php'; 81 | } 82 | 83 | /** 84 | * @return mixed|string 85 | */ 86 | private function getModelName() 87 | { 88 | return Str::studly($this->argument('name')); 89 | } 90 | 91 | /** 92 | * Get default namespace. 93 | * 94 | * @return string 95 | */ 96 | public function getDefaultNamespace(): string 97 | { 98 | $module = $this->laravel['module']; 99 | 100 | return $module->config('paths.generator.factory.namespace') ?: $module->config('paths.generator.factory.path'); 101 | } 102 | 103 | /** 104 | * Get model namespace. 105 | * 106 | * @return string 107 | */ 108 | public function getModelNamespace(): string 109 | { 110 | $module = $this->laravel['module']; 111 | 112 | return $module->config('namespace') . '\\' . $this->getModuleName() . '\\' . $module->config('paths.generator.model.path'); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/Commands/JobMakeCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->config('paths.generator.jobs.path', 'Jobs'); 35 | } 36 | 37 | /** 38 | * Get the console command arguments. 39 | * 40 | * @return array 41 | */ 42 | protected function getArguments() 43 | { 44 | return [ 45 | ['name', InputArgument::REQUIRED, 'The name of the job.'], 46 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 47 | ]; 48 | } 49 | 50 | /** 51 | * Get the console command options. 52 | * 53 | * @return array 54 | */ 55 | protected function getOptions() 56 | { 57 | return [ 58 | ['sync', null, InputOption::VALUE_NONE, 'Indicates that job should be synchronous.'], 59 | ]; 60 | } 61 | 62 | /** 63 | * Get template contents. 64 | * 65 | * @return string 66 | */ 67 | protected function getTemplateContents() 68 | { 69 | $module = $this->getModule(); 70 | 71 | return (new Stub($this->getStubName(), [ 72 | 'ALIAS' => $module->getAlias(), 73 | 'NAMESPACE' => $this->getClassNamespace($module), 74 | 'CLASS' => $this->getClass(), 75 | 'MODULE' => $this->getModuleName(), 76 | 'NAME' => $this->getFileName(), 77 | 'STUDLY_NAME' => $module->getStudlyName(), 78 | ]))->render(); 79 | } 80 | 81 | /** 82 | * Get the destination file path. 83 | * 84 | * @return string 85 | */ 86 | protected function getDestinationFilePath() 87 | { 88 | $path = module()->getModulePath($this->getModuleAlias()); 89 | 90 | $jobPath = GenerateConfigReader::read('job'); 91 | 92 | return $path . $jobPath->getPath() . '/' . $this->getFileName() . '.php'; 93 | } 94 | 95 | /** 96 | * @return string 97 | */ 98 | private function getFileName() 99 | { 100 | return Str::studly($this->argument('name')); 101 | } 102 | 103 | /** 104 | * @return string 105 | */ 106 | protected function getStubName(): string 107 | { 108 | if ($this->option('sync')) { 109 | return '/job.stub'; 110 | } 111 | 112 | return '/job-queued.stub'; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/Commands/MigrateResetCommand.php: -------------------------------------------------------------------------------- 1 | module = $this->laravel['module']; 40 | 41 | $alias = $this->argument('alias'); 42 | 43 | if (!empty($alias)) { 44 | $this->reset($alias); 45 | 46 | return; 47 | } 48 | 49 | foreach ($this->module->getOrdered($this->option('direction')) as $module) { 50 | $this->line('Running for module: ' . $module->getAlias() . ''); 51 | 52 | $this->reset($module); 53 | } 54 | } 55 | 56 | /** 57 | * Rollback migration from the specified module. 58 | * 59 | * @param $module 60 | */ 61 | public function reset($module) 62 | { 63 | if (is_string($module)) { 64 | $module = $this->module->findOrFail($module); 65 | } 66 | 67 | $migrator = new Migrator($module, $this->getLaravel()); 68 | 69 | $database = $this->option('database'); 70 | 71 | if (!empty($database)) { 72 | $migrator->setDatabase($database); 73 | } 74 | 75 | $migrated = $migrator->reset(); 76 | 77 | if (count($migrated)) { 78 | foreach ($migrated as $migration) { 79 | $this->line("Rollback: {$migration}"); 80 | } 81 | 82 | return; 83 | } 84 | 85 | $this->comment('Nothing to rollback.'); 86 | } 87 | 88 | /** 89 | * Get the console command arguments. 90 | * 91 | * @return array 92 | */ 93 | protected function getArguments() 94 | { 95 | return [ 96 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 97 | ]; 98 | } 99 | 100 | /** 101 | * Get the console command options. 102 | * 103 | * @return array 104 | */ 105 | protected function getOptions() 106 | { 107 | return [ 108 | ['direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'desc'], 109 | ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'], 110 | ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'], 111 | ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'], 112 | ]; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/Commands/MigrateRollbackCommand.php: -------------------------------------------------------------------------------- 1 | module = $this->laravel['module']; 40 | 41 | $alias = $this->argument('alias'); 42 | 43 | if (!empty($alias)) { 44 | $this->rollback($alias); 45 | 46 | return; 47 | } 48 | 49 | foreach ($this->module->getOrdered($this->option('direction')) as $module) { 50 | $this->line('Running for module: ' . $module->getAlias() . ''); 51 | 52 | $this->rollback($module); 53 | } 54 | } 55 | 56 | /** 57 | * Rollback migration from the specified module. 58 | * 59 | * @param $module 60 | */ 61 | public function rollback($module) 62 | { 63 | if (is_string($module)) { 64 | $module = $this->module->findOrFail($module); 65 | } 66 | 67 | $migrator = new Migrator($module, $this->getLaravel()); 68 | 69 | $database = $this->option('database'); 70 | 71 | if (!empty($database)) { 72 | $migrator->setDatabase($database); 73 | } 74 | 75 | $migrated = $migrator->rollback(); 76 | 77 | if (count($migrated)) { 78 | foreach ($migrated as $migration) { 79 | $this->line("Rollback: {$migration}"); 80 | } 81 | 82 | return; 83 | } 84 | 85 | $this->comment('Nothing to rollback.'); 86 | } 87 | 88 | /** 89 | * Get the console command arguments. 90 | * 91 | * @return array 92 | */ 93 | protected function getArguments() 94 | { 95 | return [ 96 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 97 | ]; 98 | } 99 | 100 | /** 101 | * Get the console command options. 102 | * 103 | * @return array 104 | */ 105 | protected function getOptions() 106 | { 107 | return [ 108 | ['direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'desc'], 109 | ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'], 110 | ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'], 111 | ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'], 112 | ]; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/Commands/MigrateCommand.php: -------------------------------------------------------------------------------- 1 | module = $this->laravel['module']; 40 | 41 | $alias = $this->argument('alias'); 42 | 43 | if ($alias) { 44 | $module = $this->module->findOrFail($alias); 45 | 46 | return $this->migrate($module); 47 | } 48 | 49 | foreach ($this->module->getOrdered($this->option('direction')) as $module) { 50 | $this->line('Running for module: ' . $module->getName() . ''); 51 | 52 | $this->migrate($module); 53 | } 54 | } 55 | 56 | /** 57 | * Run the migration from the specified module. 58 | * 59 | * @param Module $module 60 | */ 61 | protected function migrate(Module $module) 62 | { 63 | $path = str_replace(base_path(), '', (new Migrator($module, $this->getLaravel()))->getPath()); 64 | 65 | if ($this->option('subpath')) { 66 | $path = $path . "/" . $this->option("subpath"); 67 | } 68 | 69 | $this->call('migrate', [ 70 | '--path' => $path, 71 | '--database' => $this->option('database'), 72 | '--pretend' => $this->option('pretend'), 73 | '--force' => $this->option('force'), 74 | ]); 75 | 76 | if ($this->option('seed')) { 77 | $this->call('module:seed', ['alias' => $module->getAlias()]); 78 | } 79 | } 80 | 81 | /** 82 | * Get the console command arguments. 83 | * 84 | * @return array 85 | */ 86 | protected function getArguments() 87 | { 88 | return [ 89 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 90 | ]; 91 | } 92 | 93 | /** 94 | * Get the console command options. 95 | * 96 | * @return array 97 | */ 98 | protected function getOptions() 99 | { 100 | return [ 101 | ['direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'asc'], 102 | ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'], 103 | ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'], 104 | ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'], 105 | ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'], 106 | ['subpath', null, InputOption::VALUE_OPTIONAL, 'Indicate a subpath to run your migrations from'], 107 | ]; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/Activators/File.php: -------------------------------------------------------------------------------- 1 | cache = $app['cache']; 25 | $this->files = $app['files']; 26 | $this->config = $app['config']; 27 | $this->statuses = $this->getStatuses(); 28 | } 29 | 30 | public function is(Module $module, bool $active): bool 31 | { 32 | if (! isset($this->statuses[$module->getAlias()])) { 33 | $this->setActive($module, $module->get('active', false)); 34 | } 35 | 36 | return $this->statuses[$module->getAlias()] === $active; 37 | } 38 | 39 | public function enable(Module $module): void 40 | { 41 | $this->setActive($module, true); 42 | } 43 | 44 | public function disable(Module $module): void 45 | { 46 | $this->setActive($module, false); 47 | } 48 | 49 | public function setActive(Module $module, bool $active): void 50 | { 51 | $this->statuses[$module->getAlias()] = $active; 52 | 53 | $module->json()->set('active', $active)->save(); 54 | 55 | $this->writeJson(); 56 | 57 | $this->flushCache(); 58 | } 59 | 60 | public function delete(Module $module): void 61 | { 62 | if (! isset($this->statuses[$module->getAlias()])) { 63 | return; 64 | } 65 | 66 | unset($this->statuses[$module->getAlias()]); 67 | 68 | $this->writeJson(); 69 | 70 | $this->flushCache(); 71 | } 72 | 73 | public function reset(): void 74 | { 75 | $path = $this->getFilePath(); 76 | 77 | if ($this->files->exists($path)) { 78 | $this->files->delete($path); 79 | } 80 | 81 | $this->statuses = []; 82 | 83 | $this->flushCache(); 84 | } 85 | 86 | public function getStatuses(): array 87 | { 88 | if (! $this->config->get('module.cache.enabled')) { 89 | return $this->readJson(); 90 | } 91 | 92 | $key = $this->config->get('module.cache.key') . '.statuses'; 93 | $lifetime = $this->config->get('module.cache.lifetime'); 94 | 95 | return $this->cache->remember($key, $lifetime, function () { 96 | return $this->readJson(); 97 | }); 98 | } 99 | 100 | public function readJson(): array 101 | { 102 | $path = $this->getFilePath(); 103 | 104 | if (! $this->files->exists($path)) { 105 | return []; 106 | } 107 | 108 | return json_decode($this->files->get($path), true); 109 | } 110 | 111 | public function writeJson(): void 112 | { 113 | $this->files->put($this->getFilePath(), json_encode($this->statuses, JSON_PRETTY_PRINT)); 114 | } 115 | 116 | public function flushCache(): void 117 | { 118 | $key = $this->config->get('module.cache.key') . '.statuses'; 119 | 120 | $this->cache->forget($key); 121 | } 122 | 123 | public function getFilePath() 124 | { 125 | return storage_path('module_statuses.json'); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/Commands/stubs/scaffold/provider.stub: -------------------------------------------------------------------------------- 1 | loadConfig(); 18 | $this->loadRoutes(); 19 | } 20 | 21 | /** 22 | * Boot the application events. 23 | * 24 | * @return void 25 | */ 26 | public function boot() 27 | { 28 | $this->publishConfig(); 29 | $this->loadViews(); 30 | $this->loadViewComponents(); 31 | $this->loadTranslations(); 32 | $this->loadMigrations(); 33 | } 34 | 35 | /** 36 | * Load config. 37 | * 38 | * @return void 39 | */ 40 | protected function loadConfig() 41 | { 42 | $this->mergeConfigFrom( 43 | __DIR__ . '/../$PATH_CONFIG$/config.php', '$ALIAS$' 44 | ); 45 | } 46 | 47 | /** 48 | * Publish config. 49 | * 50 | * @return void 51 | */ 52 | protected function publishConfig() 53 | { 54 | $this->publishes([ 55 | __DIR__ . '/../$PATH_CONFIG$/config.php' => config_path('$ALIAS$.php'), 56 | ], 'config'); 57 | } 58 | 59 | /** 60 | * Load views. 61 | * 62 | * @return void 63 | */ 64 | public function loadViews() 65 | { 66 | $viewPath = resource_path('views/modules/$ALIAS$'); 67 | 68 | $sourcePath = __DIR__ . '/../$PATH_VIEWS$'; 69 | 70 | $this->publishes([ 71 | $sourcePath => $viewPath 72 | ],'views'); 73 | 74 | $this->loadViewsFrom(array_merge(array_map(function ($path) { 75 | return $path . '/modules/$ALIAS$'; 76 | }, \Config::get('view.paths')), [$sourcePath]), '$ALIAS$'); 77 | } 78 | 79 | /** 80 | * Load view components. 81 | * 82 | * @return void 83 | */ 84 | public function loadViewComponents() 85 | { 86 | Blade::componentNamespace('$COMPONENT_NAMESPACE$', '$ALIAS$'); 87 | } 88 | 89 | /** 90 | * Load translations. 91 | * 92 | * @return void 93 | */ 94 | public function loadTranslations() 95 | { 96 | $langPath = resource_path('lang/modules/$ALIAS$'); 97 | 98 | if (is_dir($langPath)) { 99 | $this->loadTranslationsFrom($langPath, '$ALIAS$'); 100 | } else { 101 | $this->loadTranslationsFrom(__DIR__ . '/../$PATH_LANG$', '$ALIAS$'); 102 | } 103 | } 104 | 105 | /** 106 | * Load migrations. 107 | * 108 | * @return void 109 | */ 110 | public function loadMigrations() 111 | { 112 | $this->loadMigrationsFrom(__DIR__ . '/../$MIGRATIONS_PATH$'); 113 | } 114 | 115 | /** 116 | * Load routes. 117 | * 118 | * @return void 119 | */ 120 | public function loadRoutes() 121 | { 122 | if (app()->routesAreCached()) { 123 | return; 124 | } 125 | 126 | $routes = [ 127 | 'web.php', 128 | 'api.php', 129 | ]; 130 | 131 | foreach ($routes as $route) { 132 | $this->loadRoutesFrom(__DIR__ . '/../$ROUTES_PATH$/' . $route); 133 | } 134 | } 135 | 136 | /** 137 | * Get the services provided by the provider. 138 | * 139 | * @return array 140 | */ 141 | public function provides() 142 | { 143 | return []; 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /src/Commands/ObserverMakeCommand.php: -------------------------------------------------------------------------------- 1 | getModule(); 36 | 37 | return (new Stub($this->getStubName(), [ 38 | 'NAMESPACE' => $this->getClassNamespace($module), 39 | 'CLASS' => $this->getClass(), 40 | 'MODELNAME' => $this->getModelName($module), 41 | 'SHORTMODELNAME' => $this->getShortModelName(), 42 | 'MODELVARIABLE' => $this->getModelVariable(), 43 | ]))->render(); 44 | } 45 | 46 | public function getDestinationFilePath() 47 | { 48 | $path = $this->laravel['module']->getModulePath($this->getModuleAlias()); 49 | 50 | $observerPath = GenerateConfigReader::read('observer'); 51 | 52 | return $path . $observerPath->getPath() . '/' . $this->getFileName() . '.php'; 53 | } 54 | 55 | /** 56 | * @return string 57 | */ 58 | protected function getFileName() 59 | { 60 | return Str::studly($this->argument('name')); 61 | } 62 | 63 | public function getDefaultNamespace(): string 64 | { 65 | return $this->laravel['module']->config('paths.generator.observer.path', 'Observers'); 66 | } 67 | 68 | protected function getModelName(Module $module) 69 | { 70 | $config = GenerateConfigReader::read('model'); 71 | 72 | $name = $this->laravel['module']->config('namespace') . "\\" . $module->getStudlyName() . "\\" . $config->getPath() . "\\" . $this->option('model'); 73 | 74 | return str_replace('/', '\\', $name); 75 | } 76 | 77 | protected function getShortModelName() 78 | { 79 | return class_basename($this->option('model')); 80 | } 81 | 82 | protected function getModelVariable() 83 | { 84 | return strtolower(class_basename($this->option('model'))); 85 | } 86 | 87 | /** 88 | * Get the console command arguments. 89 | * 90 | * @return array 91 | */ 92 | protected function getArguments() 93 | { 94 | return [ 95 | ['name', InputArgument::REQUIRED, 'The name of the observer.'], 96 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 97 | ]; 98 | } 99 | 100 | /** 101 | * @return array 102 | */ 103 | protected function getOptions() 104 | { 105 | return [ 106 | ['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the observer applies to.'], 107 | ]; 108 | } 109 | 110 | /** 111 | * Get the stub file name based on the options 112 | * @return string 113 | */ 114 | protected function getStubName() 115 | { 116 | $stub = '/observer-plain.stub'; 117 | 118 | if ($this->option('model')) { 119 | $stub = '/observer.stub'; 120 | } 121 | 122 | return $stub; 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/Support/Stub.php: -------------------------------------------------------------------------------- 1 | path = $path; 37 | $this->replaces = $replaces; 38 | } 39 | 40 | /** 41 | * Create new self instance. 42 | * 43 | * @param string $path 44 | * @param array $replaces 45 | * 46 | * @return self 47 | */ 48 | public static function create($path, array $replaces = []) 49 | { 50 | return new static($path, $replaces); 51 | } 52 | 53 | /** 54 | * Set stub path. 55 | * 56 | * @param string $path 57 | * 58 | * @return self 59 | */ 60 | public function setPath($path) 61 | { 62 | $this->path = $path; 63 | 64 | return $this; 65 | } 66 | 67 | /** 68 | * Get stub path. 69 | * 70 | * @return string 71 | */ 72 | public function getPath() 73 | { 74 | $path = static::getBasePath() . $this->path; 75 | 76 | return file_exists($path) ? $path : __DIR__ . '/../Commands/stubs' . $this->path; 77 | } 78 | 79 | /** 80 | * Set base path. 81 | * 82 | * @param string $path 83 | */ 84 | public static function setBasePath($path) 85 | { 86 | static::$basePath = $path; 87 | } 88 | 89 | /** 90 | * Get base path. 91 | * 92 | * @return string|null 93 | */ 94 | public static function getBasePath() 95 | { 96 | return static::$basePath; 97 | } 98 | 99 | /** 100 | * Get stub contents. 101 | * 102 | * @return mixed|string 103 | */ 104 | public function getContents() 105 | { 106 | $contents = file_get_contents($this->getPath()); 107 | 108 | foreach ($this->replaces as $search => $replace) { 109 | $contents = str_replace('$' . strtoupper($search) . '$', $replace, $contents); 110 | } 111 | 112 | return $contents; 113 | } 114 | 115 | /** 116 | * Get stub contents. 117 | * 118 | * @return string 119 | */ 120 | public function render() 121 | { 122 | return $this->getContents(); 123 | } 124 | 125 | /** 126 | * Save stub to specific path. 127 | * 128 | * @param string $path 129 | * @param string $filename 130 | * 131 | * @return bool 132 | */ 133 | public function saveTo($path, $filename) 134 | { 135 | return file_put_contents($path . '/' . $filename, $this->getContents()); 136 | } 137 | 138 | /** 139 | * Set replacements array. 140 | * 141 | * @param array $replaces 142 | * 143 | * @return $this 144 | */ 145 | public function replace(array $replaces = []) 146 | { 147 | $this->replaces = $replaces; 148 | 149 | return $this; 150 | } 151 | 152 | /** 153 | * Get replacements. 154 | * 155 | * @return array 156 | */ 157 | public function getReplaces() 158 | { 159 | return $this->replaces; 160 | } 161 | 162 | /** 163 | * Handle magic method __toString. 164 | * 165 | * @return string 166 | */ 167 | public function __toString() 168 | { 169 | return $this->render(); 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /src/Commands/ListenerMakeCommand.php: -------------------------------------------------------------------------------- 1 | getModule(); 62 | 63 | return (new Stub($this->getStubName(), [ 64 | 'ALIAS' => $module->getAlias(), 65 | 'NAMESPACE' => $this->getClassNamespace($module), 66 | 'EVENTNAME' => $this->getEventName($module), 67 | 'SHORTEVENTNAME' => $this->getShortEventName(), 68 | 'CLASS' => $this->getClass(), 69 | ]))->render(); 70 | } 71 | 72 | public function getDefaultNamespace() : string 73 | { 74 | return $this->laravel['module']->config('paths.generator.listener.path', 'Listeners'); 75 | } 76 | 77 | protected function getEventName(Module $module) 78 | { 79 | $config = GenerateConfigReader::read('event'); 80 | 81 | $name = $this->laravel['module']->config('namespace') . "\\" . $module->getStudlyName() . "\\" . $config->getPath() . "\\" . $this->option('event'); 82 | 83 | return str_replace('/', '\\', $name); 84 | } 85 | 86 | protected function getShortEventName() 87 | { 88 | return class_basename($this->option('event')); 89 | } 90 | 91 | protected function getDestinationFilePath() 92 | { 93 | $path = module()->getModulePath($this->getModuleAlias()); 94 | 95 | $config = GenerateConfigReader::read('listener'); 96 | 97 | return $path . $config->getPath() . '/' . $this->getFileName() . '.php'; 98 | } 99 | 100 | /** 101 | * @return string 102 | */ 103 | protected function getFileName() 104 | { 105 | return Str::studly($this->argument('name')); 106 | } 107 | 108 | /** 109 | * @return string 110 | */ 111 | protected function getStubName(): string 112 | { 113 | if ($this->option('queued')) { 114 | if ($this->option('event')) { 115 | return '/listener-queued.stub'; 116 | } 117 | 118 | return '/listener-queued-duck.stub'; 119 | } 120 | 121 | if ($this->option('event')) { 122 | return '/listener.stub'; 123 | } 124 | 125 | return '/listener-duck.stub'; 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/Commands/ControllerMakeCommand.php: -------------------------------------------------------------------------------- 1 | getModulePath($this->getModuleAlias()); 45 | 46 | $controllerPath = GenerateConfigReader::read('controller'); 47 | 48 | return $path . $controllerPath->getPath() . '/' . $this->getControllerName() . '.php'; 49 | } 50 | 51 | /** 52 | * @return string 53 | */ 54 | protected function getTemplateContents() 55 | { 56 | $module = $this->getModule(); 57 | 58 | return (new Stub($this->getStubName(), [ 59 | 'ALIAS' => $module->getAlias(), 60 | 'MODULENAME' => $module->getStudlyName(), 61 | 'CONTROLLERNAME' => $this->getControllerName(), 62 | 'NAMESPACE' => $module->getStudlyName(), 63 | 'CLASS_NAMESPACE' => $this->getClassNamespace($module), 64 | 'CLASS' => $this->getControllerNameWithoutNamespace(), 65 | 'MODULE' => $this->getModuleName(), 66 | 'NAME' => $this->getModuleName(), 67 | 'STUDLY_NAME' => $module->getStudlyName(), 68 | 'MODULE_NAMESPACE' => $this->laravel['module']->config('namespace'), 69 | ]))->render(); 70 | } 71 | 72 | /** 73 | * Get the console command arguments. 74 | * 75 | * @return array 76 | */ 77 | protected function getArguments() 78 | { 79 | return [ 80 | ['controller', InputArgument::REQUIRED, 'The name of the controller class.'], 81 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 82 | ]; 83 | } 84 | 85 | /** 86 | * @return array 87 | */ 88 | protected function getOptions() 89 | { 90 | return [ 91 | ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain controller', null], 92 | ['api', null, InputOption::VALUE_NONE, 'Exclude the create and edit methods from the controller.'], 93 | ]; 94 | } 95 | 96 | /** 97 | * @return array|string 98 | */ 99 | protected function getControllerName() 100 | { 101 | $controller = Str::studly($this->argument('controller')); 102 | 103 | return $controller; 104 | } 105 | 106 | /** 107 | * @return array|string 108 | */ 109 | protected function getControllerNameWithoutNamespace() 110 | { 111 | return class_basename($this->getControllerName()); 112 | } 113 | 114 | public function getDefaultNamespace() : string 115 | { 116 | return $this->laravel['module']->config('paths.generator.controller.path', 'Http/Controllers'); 117 | } 118 | 119 | /** 120 | * Get the stub file name based on the options 121 | * @return string 122 | */ 123 | protected function getStubName() 124 | { 125 | if ($this->option('plain') === true) { 126 | $stub = '/controller-plain.stub'; 127 | } elseif ($this->option('api') === true) { 128 | $stub = '/controller-api.stub'; 129 | } else { 130 | $stub = '/controller.stub'; 131 | } 132 | 133 | return $stub; 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /src/Commands/ComponentMakeCommand.php: -------------------------------------------------------------------------------- 1 | createViewTemplate(); 46 | 47 | return 0; 48 | } 49 | 50 | public function getDefaultNamespace() : string 51 | { 52 | $module = $this->laravel['module']; 53 | 54 | return $module->config('paths.generator.component.namespace') ?: $module->config('paths.generator.component.path'); 55 | } 56 | 57 | /** 58 | * Get the console command arguments. 59 | * 60 | * @return array 61 | */ 62 | protected function getArguments() 63 | { 64 | return [ 65 | ['name', InputArgument::REQUIRED, 'The name of the command.'], 66 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 67 | ]; 68 | } 69 | 70 | /** 71 | * @return mixed 72 | */ 73 | protected function getTemplateContents() 74 | { 75 | $module = $this->getModule(); 76 | 77 | return (new Stub('/component.stub', [ 78 | 'ALIAS' => $this->getModuleAlias(), 79 | 'NAMESPACE' => $this->getClassNamespace($module), 80 | 'CLASS' => $this->getClass(), 81 | 'VIEW_NAME' => 'components.' . $this->getViewName() 82 | ]))->render(); 83 | } 84 | 85 | /** 86 | * @return mixed 87 | */ 88 | protected function getDestinationFilePath() 89 | { 90 | $path = module()->getModulePath($this->getModuleAlias()); 91 | 92 | $config = GenerateConfigReader::read('component'); 93 | 94 | return $path . $config->getPath() . '/' . $this->getFileName() . '.php'; 95 | } 96 | 97 | /** 98 | * @return string 99 | */ 100 | protected function getFileName() 101 | { 102 | return Str::studly($this->argument('name')); 103 | } 104 | 105 | /** 106 | * Create the view template of the component. 107 | * 108 | * @return void 109 | */ 110 | protected function createViewTemplate() 111 | { 112 | $overwrite_file = $this->hasOption('force') ? $this->option('force') : false; 113 | 114 | $path = $this->getViewTemplatePath(); 115 | 116 | $contents = $this->getViewTemplateContents(); 117 | 118 | (new FileGenerator($path, $contents))->withFileOverwrite($overwrite_file)->generate(); 119 | } 120 | 121 | protected function getViewTemplatePath() 122 | { 123 | $module_path = $this->laravel['module']->getModulePath($this->getModuleAlias()); 124 | 125 | $folder = $module_path . GenerateConfigReader::read('view')->getPath() . '/components/'; 126 | 127 | if (!File::isDirectory($folder)) { 128 | File::makeDirectory($folder); 129 | } 130 | 131 | return $folder . $this->getViewName() . '.blade.php'; 132 | } 133 | 134 | protected function getViewTemplateContents() 135 | { 136 | $quote = Inspiring::quote(); 137 | 138 | return << 140 | {$quote} 141 | 142 | HTML; 143 | } 144 | 145 | protected function getViewName() 146 | { 147 | return Str::kebab($this->argument('name')); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/Commands/InstallCommand.php: -------------------------------------------------------------------------------- 1 | argument('alias'))) { 41 | $this->installFromFile(); 42 | 43 | return; 44 | } 45 | 46 | $this->install( 47 | $this->argument('alias'), 48 | $this->argument('version'), 49 | $this->option('type'), 50 | $this->option('tree') 51 | ); 52 | } 53 | 54 | /** 55 | * Install modules from modules.json file. 56 | */ 57 | protected function installFromFile() 58 | { 59 | if (!file_exists($path = base_path('modules.json'))) { 60 | $this->error("File 'modules.json' does not exist in your project root."); 61 | 62 | return; 63 | } 64 | 65 | $modules = Json::make($path); 66 | 67 | $dependencies = $modules->get('require', []); 68 | 69 | foreach ($dependencies as $module) { 70 | $module = collect($module); 71 | 72 | $this->install( 73 | $module->get('alias'), 74 | $module->get('version'), 75 | $module->get('type') 76 | ); 77 | } 78 | } 79 | 80 | /** 81 | * Install the specified module. 82 | * 83 | * @param string $name 84 | * @param string $version 85 | * @param string $type 86 | * @param bool $tree 87 | */ 88 | protected function install($alias, $version = 'dev-master', $type = 'composer', $tree = false) 89 | { 90 | $installer = new Installer( 91 | $alias, 92 | $version, 93 | $type ?: $this->option('type'), 94 | $tree ?: $this->option('tree') 95 | ); 96 | 97 | $installer->setRepository($this->laravel['module']); 98 | 99 | $installer->setConsole($this); 100 | 101 | if ($timeout = $this->option('timeout')) { 102 | $installer->setTimeout($timeout); 103 | } 104 | 105 | if ($path = $this->option('path')) { 106 | $installer->setPath($path); 107 | } 108 | 109 | $installer->run(); 110 | 111 | if (!$this->option('no-update')) { 112 | $this->call('module:update', [ 113 | 'alias' => $installer->getModuleAlias(), 114 | ]); 115 | } 116 | } 117 | 118 | /** 119 | * Get the console command arguments. 120 | * 121 | * @return array 122 | */ 123 | protected function getArguments() 124 | { 125 | return [ 126 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be installed.'], 127 | ['version', InputArgument::OPTIONAL, 'The version of module will be installed.'], 128 | ]; 129 | } 130 | 131 | /** 132 | * Get the console command options. 133 | * 134 | * @return array 135 | */ 136 | protected function getOptions() 137 | { 138 | return [ 139 | ['timeout', null, InputOption::VALUE_OPTIONAL, 'The process timeout.', null], 140 | ['path', null, InputOption::VALUE_OPTIONAL, 'The installation path.', null], 141 | ['type', null, InputOption::VALUE_OPTIONAL, 'The type of installation.', null], 142 | ['tree', null, InputOption::VALUE_NONE, 'Install the module as a git subtree', null], 143 | ['no-update', null, InputOption::VALUE_NONE, 'Disables the automatic update of the dependencies.', null], 144 | ]; 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /src/Commands/ProviderMakeCommand.php: -------------------------------------------------------------------------------- 1 | laravel['module']->config('paths.generator.provider.path', 'Providers'); 41 | } 42 | 43 | /** 44 | * Get the console command arguments. 45 | * 46 | * @return array 47 | */ 48 | protected function getArguments() 49 | { 50 | return [ 51 | ['name', InputArgument::REQUIRED, 'The service provider name.'], 52 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 53 | ]; 54 | } 55 | 56 | /** 57 | * Get the console command options. 58 | * 59 | * @return array 60 | */ 61 | protected function getOptions() 62 | { 63 | return [ 64 | ['master', null, InputOption::VALUE_NONE, 'Indicates the master service provider', null], 65 | ]; 66 | } 67 | 68 | /** 69 | * @return mixed 70 | */ 71 | protected function getTemplateContents() 72 | { 73 | $stub = $this->option('master') ? 'scaffold/provider' : 'provider'; 74 | 75 | /** @var Module $module */ 76 | $module = $this->getModule(); 77 | 78 | return (new Stub('/' . $stub . '.stub', [ 79 | 'ALIAS' => $module->getAlias(), 80 | 'NAMESPACE' => $this->getClassNamespace($module), 81 | 'CLASS' => $this->getClass(), 82 | 'MODULE' => $this->getModuleName(), 83 | 'NAME' => $this->getFileName(), 84 | 'STUDLY_NAME' => $module->getStudlyName(), 85 | 'MODULE_NAMESPACE' => $this->laravel['module']->config('namespace'), 86 | 'COMPONENT_NAMESPACE' => $this->getComponentNamespace(), 87 | 'PATH_VIEWS' => GenerateConfigReader::read('view')->getPath(), 88 | 'PATH_LANG' => GenerateConfigReader::read('lang')->getPath(), 89 | 'PATH_CONFIG' => GenerateConfigReader::read('config')->getPath(), 90 | 'MIGRATIONS_PATH' => GenerateConfigReader::read('migration')->getPath(), 91 | 'FACTORIES_PATH' => GenerateConfigReader::read('factory')->getPath(), 92 | 'ROUTES_PATH' => GenerateConfigReader::read('route')->getPath(), 93 | ]))->render(); 94 | } 95 | /** 96 | * @return string 97 | */ 98 | private function getFileName() 99 | { 100 | return Str::studly($this->argument('name')); 101 | } 102 | 103 | /** 104 | * @return mixed 105 | */ 106 | protected function getDestinationFilePath() 107 | { 108 | $path = module()->getModulePath($this->getModuleAlias()); 109 | 110 | $generatorPath = GenerateConfigReader::read('provider'); 111 | 112 | return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php'; 113 | } 114 | 115 | protected function getComponentNamespace() 116 | { 117 | $module = $this->laravel['module']; 118 | 119 | $namespace = $module->config('namespace'); 120 | 121 | $namespace .= '\\' . $this->getModuleName() . '\\'; 122 | 123 | $namespace .= $module->config('paths.generator.component.namespace') ?: $module->config('paths.generator.component.path'); 124 | 125 | $namespace = str_replace('/', '\\', $namespace); 126 | 127 | return trim($namespace, '\\'); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/Support/Migrations/NameParser.php: -------------------------------------------------------------------------------- 1 | [ 28 | 'create', 29 | 'make', 30 | ], 31 | 'delete' => [ 32 | 'delete', 33 | 'remove', 34 | ], 35 | 'add' => [ 36 | 'add', 37 | 'update', 38 | 'append', 39 | 'insert', 40 | ], 41 | 'drop' => [ 42 | 'destroy', 43 | 'drop', 44 | ], 45 | ]; 46 | 47 | /** 48 | * The constructor. 49 | * 50 | * @param string $name 51 | */ 52 | public function __construct($name) 53 | { 54 | $this->name = $name; 55 | $this->data = $this->fetchData(); 56 | } 57 | 58 | /** 59 | * Get original migration name. 60 | * 61 | * @return string 62 | */ 63 | public function getOriginalName() 64 | { 65 | return $this->name; 66 | } 67 | 68 | /** 69 | * Get schema type or action. 70 | * 71 | * @return string 72 | */ 73 | public function getAction() 74 | { 75 | return head($this->data); 76 | } 77 | 78 | /** 79 | * Get the table will be used. 80 | * 81 | * @return string 82 | */ 83 | public function getTableName() 84 | { 85 | $matches = array_reverse($this->getMatches()); 86 | 87 | return array_shift($matches); 88 | } 89 | 90 | /** 91 | * Get matches data from regex. 92 | * 93 | * @return array 94 | */ 95 | public function getMatches() 96 | { 97 | preg_match($this->getPattern(), $this->name, $matches); 98 | 99 | return $matches; 100 | } 101 | 102 | /** 103 | * Get name pattern. 104 | * 105 | * @return string 106 | */ 107 | public function getPattern() 108 | { 109 | switch ($action = $this->getAction()) { 110 | case 'add': 111 | case 'append': 112 | case 'update': 113 | case 'insert': 114 | return "/{$action}_(.*)_to_(.*)_table/"; 115 | break; 116 | 117 | case 'delete': 118 | case 'remove': 119 | case 'alter': 120 | return "/{$action}_(.*)_from_(.*)_table/"; 121 | break; 122 | 123 | default: 124 | return "/{$action}_(.*)_table/"; 125 | break; 126 | } 127 | } 128 | 129 | /** 130 | * Fetch the migration name to an array data. 131 | * 132 | * @return array 133 | */ 134 | protected function fetchData() 135 | { 136 | return explode('_', $this->name); 137 | } 138 | 139 | /** 140 | * Get the array data. 141 | * 142 | * @return array 143 | */ 144 | public function getData() 145 | { 146 | return $this->data; 147 | } 148 | 149 | /** 150 | * Determine whether the given type is same with the current schema action or type. 151 | * 152 | * @param $type 153 | * 154 | * @return bool 155 | */ 156 | public function is($type) 157 | { 158 | return $type === $this->getAction(); 159 | } 160 | 161 | /** 162 | * Determine whether the current schema action is a adding action. 163 | * 164 | * @return bool 165 | */ 166 | public function isAdd() 167 | { 168 | return in_array($this->getAction(), $this->actions['add']); 169 | } 170 | 171 | /** 172 | * Determine whether the current schema action is a deleting action. 173 | * 174 | * @return bool 175 | */ 176 | public function isDelete() 177 | { 178 | return in_array($this->getAction(), $this->actions['delete']); 179 | } 180 | 181 | /** 182 | * Determine whether the current schema action is a creating action. 183 | * 184 | * @return bool 185 | */ 186 | public function isCreate() 187 | { 188 | return in_array($this->getAction(), $this->actions['create']); 189 | } 190 | 191 | /** 192 | * Determine whether the current schema action is a dropping action. 193 | * 194 | * @return bool 195 | */ 196 | public function isDrop() 197 | { 198 | return in_array($this->getAction(), $this->actions['drop']); 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /src/Publishing/Publisher.php: -------------------------------------------------------------------------------- 1 | module = $module; 61 | } 62 | 63 | /** 64 | * Show the result message. 65 | * 66 | * @return self 67 | */ 68 | public function showMessage() 69 | { 70 | $this->showMessage = true; 71 | 72 | return $this; 73 | } 74 | 75 | /** 76 | * Hide the result message. 77 | * 78 | * @return self 79 | */ 80 | public function hideMessage() 81 | { 82 | $this->showMessage = false; 83 | 84 | return $this; 85 | } 86 | 87 | /** 88 | * Get module instance. 89 | * 90 | * @return \Akaunting\Module\Module 91 | */ 92 | public function getModule() 93 | { 94 | return $this->module; 95 | } 96 | 97 | /** 98 | * Set modules repository instance. 99 | * @param RepositoryInterface $repository 100 | * @return $this 101 | */ 102 | public function setRepository(RepositoryInterface $repository) 103 | { 104 | $this->repository = $repository; 105 | 106 | return $this; 107 | } 108 | 109 | /** 110 | * Get modules repository instance. 111 | * 112 | * @return RepositoryInterface 113 | */ 114 | public function getRepository() 115 | { 116 | return $this->repository; 117 | } 118 | 119 | /** 120 | * Set console instance. 121 | * 122 | * @param \Illuminate\Console\Command $console 123 | * 124 | * @return $this 125 | */ 126 | public function setConsole(Command $console) 127 | { 128 | $this->console = $console; 129 | 130 | return $this; 131 | } 132 | 133 | /** 134 | * Get console instance. 135 | * 136 | * @return \Illuminate\Console\Command 137 | */ 138 | public function getConsole() 139 | { 140 | return $this->console; 141 | } 142 | 143 | /** 144 | * Get laravel filesystem instance. 145 | * 146 | * @return \Illuminate\Filesystem\Filesystem 147 | */ 148 | public function getFilesystem() 149 | { 150 | return $this->repository->getFiles(); 151 | } 152 | 153 | /** 154 | * Get destination path. 155 | * 156 | * @return string 157 | */ 158 | abstract public function getDestinationPath(); 159 | 160 | /** 161 | * Get source path. 162 | * 163 | * @return string 164 | */ 165 | abstract public function getSourcePath(); 166 | 167 | /** 168 | * Publish something. 169 | */ 170 | public function publish() 171 | { 172 | if (!$this->console instanceof Command) { 173 | $message = "The 'console' property must instance of \\Illuminate\\Console\\Command."; 174 | 175 | throw new \RuntimeException($message); 176 | } 177 | 178 | if (!$this->getFilesystem()->isDirectory($sourcePath = $this->getSourcePath())) { 179 | return; 180 | } 181 | 182 | if (!$this->getFilesystem()->isDirectory($destinationPath = $this->getDestinationPath())) { 183 | $this->getFilesystem()->makeDirectory($destinationPath, 0775, true); 184 | } 185 | 186 | if ($this->getFilesystem()->copyDirectory($sourcePath, $destinationPath)) { 187 | if ($this->showMessage === true) { 188 | $this->console->line("Published: {$this->module->getAlias()}"); 189 | } 190 | } else { 191 | $this->console->error($this->error); 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /src/Commands/MigrationMakeCommand.php: -------------------------------------------------------------------------------- 1 | option('fields')); 66 | } 67 | 68 | /** 69 | * @throws \InvalidArgumentException 70 | * 71 | * @return mixed 72 | */ 73 | protected function getTemplateContents() 74 | { 75 | $parser = new NameParser($this->argument('name')); 76 | 77 | if ($parser->isCreate()) { 78 | return Stub::create('/migration/create.stub', [ 79 | 'class' => $this->getClass(), 80 | 'table' => $parser->getTableName(), 81 | 'fields' => $this->getSchemaParser()->render(), 82 | ]); 83 | } elseif ($parser->isAdd()) { 84 | return Stub::create('/migration/add.stub', [ 85 | 'class' => $this->getClass(), 86 | 'table' => $parser->getTableName(), 87 | 'fields_up' => $this->getSchemaParser()->up(), 88 | 'fields_down' => $this->getSchemaParser()->down(), 89 | ]); 90 | } elseif ($parser->isDelete()) { 91 | return Stub::create('/migration/delete.stub', [ 92 | 'class' => $this->getClass(), 93 | 'table' => $parser->getTableName(), 94 | 'fields_down' => $this->getSchemaParser()->up(), 95 | 'fields_up' => $this->getSchemaParser()->down(), 96 | ]); 97 | } elseif ($parser->isDrop()) { 98 | return Stub::create('/migration/drop.stub', [ 99 | 'class' => $this->getClass(), 100 | 'table' => $parser->getTableName(), 101 | 'fields' => $this->getSchemaParser()->render(), 102 | ]); 103 | } 104 | 105 | return Stub::create('/migration/plain.stub', [ 106 | 'class' => $this->getClass(), 107 | ]); 108 | } 109 | 110 | /** 111 | * @return mixed 112 | */ 113 | protected function getDestinationFilePath() 114 | { 115 | $path = module()->getModulePath($this->getModuleAlias()); 116 | 117 | $generatorPath = GenerateConfigReader::read('migration'); 118 | 119 | return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php'; 120 | } 121 | 122 | /** 123 | * @return string 124 | */ 125 | private function getFileName() 126 | { 127 | return date('Y_m_d_His_') . $this->getSchemaName(); 128 | } 129 | 130 | /** 131 | * @return array|string 132 | */ 133 | private function getSchemaName() 134 | { 135 | return $this->argument('name'); 136 | } 137 | 138 | /** 139 | * @return string 140 | */ 141 | private function getClassName() 142 | { 143 | return Str::studly($this->argument('name')); 144 | } 145 | 146 | public function getClass() 147 | { 148 | return $this->getClassName(); 149 | } 150 | 151 | /** 152 | * Run the command. 153 | */ 154 | public function handle() 155 | { 156 | parent::handle(); 157 | 158 | if (app()->environment() === 'testing') { 159 | return; 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /src/Commands/ModelMakeCommand.php: -------------------------------------------------------------------------------- 1 | handleOptionalMigrationOption(); 42 | } 43 | 44 | /** 45 | * Create a proper migration name: 46 | * ProductDetail: product_details 47 | * Product: products 48 | * @return string 49 | */ 50 | private function createMigrationName() 51 | { 52 | $pieces = preg_split('/(?=[A-Z])/', $this->argument('model'), -1, PREG_SPLIT_NO_EMPTY); 53 | 54 | $string = ''; 55 | foreach ($pieces as $i => $piece) { 56 | if ($i+1 < count($pieces)) { 57 | $string .= strtolower($piece) . '_'; 58 | } else { 59 | $string .= Str::plural(strtolower($piece)); 60 | } 61 | } 62 | 63 | return $string; 64 | } 65 | 66 | /** 67 | * Get the console command arguments. 68 | * 69 | * @return array 70 | */ 71 | protected function getArguments() 72 | { 73 | return [ 74 | ['model', InputArgument::REQUIRED, 'The name of model will be created.'], 75 | ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], 76 | ]; 77 | } 78 | 79 | /** 80 | * Get the console command options. 81 | * 82 | * @return array 83 | */ 84 | protected function getOptions() 85 | { 86 | return [ 87 | ['fillable', null, InputOption::VALUE_OPTIONAL, 'The fillable attributes.', null], 88 | ['migration', 'm', InputOption::VALUE_NONE, 'Flag to create associated migrations', null], 89 | ['factory', 'f', InputOption::VALUE_NONE, 'Flag to create associated factory', null], 90 | ]; 91 | } 92 | 93 | /** 94 | * Create the migration file with the given model if migration flag was used 95 | */ 96 | private function handleOptionalMigrationOption() 97 | { 98 | if ($this->option('migration') === true) { 99 | $migrationName = 'create_' . $this->createMigrationName() . '_table'; 100 | $this->call('module:make-migration', ['name' => $migrationName, 'alias' => $this->argument('alias')]); 101 | } 102 | 103 | if ($this->option('factory') === true) { 104 | $this->call('module:make-factory', ['name' => $this->argument('model'), 'alias' => $this->argument('alias')]); 105 | } 106 | } 107 | 108 | /** 109 | * @return mixed 110 | */ 111 | protected function getTemplateContents() 112 | { 113 | $module = $this->getModule(); 114 | 115 | return (new Stub('/model.stub', [ 116 | 'NAME' => $this->getModelName(), 117 | 'ALIAS' => $module->getAlias(), 118 | 'FILLABLE' => $this->getFillable(), 119 | 'NAMESPACE' => $this->getClassNamespace($module), 120 | 'CLASS' => $this->getClass(), 121 | 'MODULE' => $this->getModuleName(), 122 | 'STUDLY_NAME' => $module->getStudlyName(), 123 | 'MODULE_NAMESPACE' => $this->laravel['module']->config('namespace'), 124 | 'FACTORY_NAMESPACE' => $this->getFactoryNamespace(), 125 | ]))->render(); 126 | } 127 | 128 | /** 129 | * @return mixed 130 | */ 131 | protected function getDestinationFilePath() 132 | { 133 | $path = module()->getModulePath($this->getModuleAlias()); 134 | 135 | $modelPath = GenerateConfigReader::read('model'); 136 | 137 | return $path . $modelPath->getPath() . '/' . $this->getModelName() . '.php'; 138 | } 139 | 140 | /** 141 | * @return mixed|string 142 | */ 143 | private function getModelName() 144 | { 145 | return Str::studly($this->argument('model')); 146 | } 147 | 148 | /** 149 | * @return string 150 | */ 151 | private function getFillable() 152 | { 153 | $fillable = $this->option('fillable'); 154 | 155 | if (!is_null($fillable)) { 156 | $arrays = explode(',', $fillable); 157 | 158 | return json_encode($arrays); 159 | } 160 | 161 | return '[]'; 162 | } 163 | 164 | /** 165 | * Get default namespace. 166 | * 167 | * @return string 168 | */ 169 | public function getDefaultNamespace() : string 170 | { 171 | return $this->laravel['module']->config('paths.generator.model.path', 'Models'); 172 | } 173 | 174 | /** 175 | * Get factory namespace. 176 | * 177 | * @return string 178 | */ 179 | public function getFactoryNamespace(): string 180 | { 181 | $module = $this->laravel['module']; 182 | 183 | $config = GenerateConfigReader::read('factory'); 184 | 185 | $path = str_replace('/', '\\', $config->getPath()); 186 | 187 | return $module->config('namespace') . '\\' . $this->getModuleName() . '\\' . $path; 188 | } 189 | } 190 | -------------------------------------------------------------------------------- /src/Json.php: -------------------------------------------------------------------------------- 1 | path = (string) $path; 40 | $this->filesystem = $filesystem ?: new Filesystem(); 41 | $this->attributes = Collection::make($this->getAttributes()); 42 | } 43 | 44 | /** 45 | * Get filesystem. 46 | * 47 | * @return Filesystem 48 | */ 49 | public function getFilesystem() 50 | { 51 | return $this->filesystem; 52 | } 53 | 54 | /** 55 | * Set filesystem. 56 | * 57 | * @param Filesystem $filesystem 58 | * 59 | * @return $this 60 | */ 61 | public function setFilesystem(Filesystem $filesystem) 62 | { 63 | $this->filesystem = $filesystem; 64 | 65 | return $this; 66 | } 67 | 68 | /** 69 | * Get path. 70 | * 71 | * @return string 72 | */ 73 | public function getPath() 74 | { 75 | return $this->path; 76 | } 77 | 78 | /** 79 | * Set path. 80 | * 81 | * @param mixed $path 82 | * 83 | * @return $this 84 | */ 85 | public function setPath($path) 86 | { 87 | $this->path = (string) $path; 88 | 89 | return $this; 90 | } 91 | 92 | /** 93 | * Make new instance. 94 | * 95 | * @param string $path 96 | * @param \Illuminate\Filesystem\Filesystem $filesystem 97 | * 98 | * @return static 99 | */ 100 | public static function make($path, Filesystem $filesystem = null) 101 | { 102 | return new static($path, $filesystem); 103 | } 104 | 105 | /** 106 | * Get file content. 107 | * 108 | * @return string 109 | */ 110 | public function getContents() 111 | { 112 | return $this->filesystem->get($this->getPath()); 113 | } 114 | 115 | /** 116 | * Get file contents as array. 117 | * @return array 118 | * @throws \Exception 119 | */ 120 | public function getAttributes() 121 | { 122 | $attributes = json_decode($this->getContents(), 1); 123 | 124 | // any JSON parsing errors should throw an exception 125 | if (json_last_error() > 0) { 126 | throw new InvalidJsonException('Error processing file: ' . $this->getPath() . '. Error: ' . json_last_error_msg()); 127 | } 128 | 129 | if (config('module.cache.enabled') === false) { 130 | return $attributes; 131 | } 132 | 133 | return app('cache')->remember($this->getPath(), config('module.cache.lifetime'), function () use ($attributes) { 134 | return $attributes; 135 | }); 136 | } 137 | 138 | /** 139 | * Convert the given array data to pretty json. 140 | * 141 | * @param array $data 142 | * 143 | * @return string 144 | */ 145 | public function toJsonPretty(array $data = null) 146 | { 147 | return json_encode($data ?: $this->attributes, JSON_PRETTY_PRINT); 148 | } 149 | 150 | /** 151 | * Update json contents from array data. 152 | * 153 | * @param array $data 154 | * 155 | * @return bool 156 | */ 157 | public function update(array $data) 158 | { 159 | $this->attributes = new Collection(array_merge($this->attributes->toArray(), $data)); 160 | 161 | return $this->save(); 162 | } 163 | 164 | /** 165 | * Set a specific key & value. 166 | * 167 | * @param string $key 168 | * @param mixed $value 169 | * 170 | * @return $this 171 | */ 172 | public function set($key, $value) 173 | { 174 | $this->attributes->offsetSet($key, $value); 175 | 176 | return $this; 177 | } 178 | 179 | /** 180 | * Save the current attributes array to the file storage. 181 | * 182 | * @return bool 183 | */ 184 | public function save() 185 | { 186 | return $this->filesystem->put($this->getPath(), $this->toJsonPretty()); 187 | } 188 | 189 | /** 190 | * Handle magic method __get. 191 | * 192 | * @param string $key 193 | * 194 | * @return mixed 195 | */ 196 | public function __get($key) 197 | { 198 | return $this->get($key); 199 | } 200 | 201 | /** 202 | * Get the specified attribute from json file. 203 | * 204 | * @param $key 205 | * @param null $default 206 | * 207 | * @return mixed 208 | */ 209 | public function get($key, $default = null) 210 | { 211 | return $this->attributes->get($key, $default); 212 | } 213 | 214 | /** 215 | * Handle call to __call method. 216 | * 217 | * @param string $method 218 | * @param array $arguments 219 | * 220 | * @return mixed 221 | */ 222 | public function __call($method, $arguments = []) 223 | { 224 | if (method_exists($this, $method)) { 225 | return call_user_func_array([$this, $method], $arguments); 226 | } 227 | 228 | return call_user_func_array([$this->attributes, $method], $arguments); 229 | } 230 | 231 | /** 232 | * Handle call to __toString method. 233 | * 234 | * @return string 235 | */ 236 | public function __toString() 237 | { 238 | return $this->getContents(); 239 | } 240 | } 241 | --------------------------------------------------------------------------------