├── .gitignore ├── .travis.yml ├── README.md ├── composer.json ├── phpunit.xml ├── src └── Mrterryh │ └── Modules │ ├── Console │ ├── DisableModuleCommand.php │ ├── EnableModuleCommand.php │ ├── MakeModuleCommand.php │ ├── MakeModuleCommandCommand.php │ ├── MakeModuleEventCommand.php │ ├── MakeModuleHandlerCommand.php │ ├── MakeModuleMigrationCommand.php │ ├── MakeModuleRequestCommand.php │ ├── ModuleListCommand.php │ ├── ModuleMigrateCommand.php │ ├── ModuleMigrateRollbackCommand.php │ ├── ModuleSeedCommand.php │ └── stubs │ │ ├── moduleBusProvider.stub │ │ ├── moduleCommand.stub │ │ ├── moduleCommandHandler.stub │ │ ├── moduleDbMigration.stub │ │ ├── moduleDbSeeder.stub │ │ ├── moduleEvent.stub │ │ ├── moduleEventHandler.stub │ │ ├── moduleJson.stub │ │ ├── moduleProvider.stub │ │ ├── moduleRequest.stub │ │ ├── moduleRouteProvider.stub │ │ └── moduleRoutes.stub │ ├── Facades │ └── Modules.php │ ├── Generators │ ├── Generator.php │ ├── ModuleCommandGenerator.php │ ├── ModuleEventGenerator.php │ ├── ModuleGenerator.php │ ├── ModuleHandlerGenerator.php │ ├── ModuleMigrationGenerator.php │ └── ModuleRequestGenerator.php │ ├── Module.php │ ├── ModulesServiceProvider.php │ └── Repository.php └── tests └── .gitkeep /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | - hhvm 8 | 9 | before_script: 10 | - travis_retry composer self-update 11 | - travis_retry composer install --prefer-source --no-interaction --dev 12 | 13 | script: phpunit 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Modules 2 | A simple Laravel 5 modules package. 3 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mrterryh/modules", 3 | "description": "A simple Laravel 5 modules package.", 4 | "keywords": ["modules", "plugins", "addons", "mrterryh", "laravel", "module"], 5 | "authors": [ 6 | { 7 | "name": "Terry Harvey", 8 | "email": "mrterrymh@gmail.com" 9 | } 10 | ], 11 | "require": { 12 | "php": ">=5.4.0", 13 | "illuminate/support": "5.0.*" 14 | }, 15 | "autoload": { 16 | "psr-0": { 17 | "Mrterryh\\Modules": "src/" 18 | } 19 | }, 20 | "minimum-stability": "stable" 21 | } 22 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/DisableModuleCommand.php: -------------------------------------------------------------------------------- 1 | moduleRepo = $moduleRepo; 33 | 34 | parent::__construct(); 35 | } 36 | 37 | /** 38 | * Execute the console command. 39 | */ 40 | public function fire() 41 | { 42 | $name = $this->argument('name'); 43 | $module = $this->moduleRepo->getByName($name); 44 | 45 | if (!$module) 46 | return $this->error("The [$name] module does not exist."); 47 | 48 | if (!$module->isEnabled()) 49 | return $this->error("The [$name] module is already disabled."); 50 | 51 | $module->disable(); 52 | 53 | $this->info("The [$name] module has been disabled."); 54 | } 55 | 56 | /** 57 | * Get the console command arguments. 58 | * @return array 59 | */ 60 | protected function getArguments() 61 | { 62 | return [ 63 | ['name', InputArgument::REQUIRED, 'The name of the module.'], 64 | ]; 65 | } 66 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/EnableModuleCommand.php: -------------------------------------------------------------------------------- 1 | moduleRepo = $moduleRepo; 33 | 34 | parent::__construct(); 35 | } 36 | 37 | /** 38 | * Execute the console command. 39 | */ 40 | public function fire() 41 | { 42 | $name = $this->argument('name'); 43 | $module = $this->moduleRepo->getByName($name); 44 | 45 | if (!$module) 46 | return $this->error("The [$name] module does not exist."); 47 | 48 | if ($module->isEnabled()) 49 | return $this->error("The [$name] module is already enabled."); 50 | 51 | $module->enable(); 52 | 53 | $this->info("The [$name] module has been enabled."); 54 | } 55 | 56 | /** 57 | * Get the console command arguments. 58 | * @return array 59 | */ 60 | protected function getArguments() 61 | { 62 | return [ 63 | ['name', InputArgument::REQUIRED, 'The name of the module.'], 64 | ]; 65 | } 66 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/MakeModuleCommand.php: -------------------------------------------------------------------------------- 1 | argument('name'), $this->laravel['files'], $this); 39 | $generator->generate(); 40 | } 41 | 42 | /** 43 | * Get the console command arguments. 44 | * @return array 45 | */ 46 | protected function getArguments() 47 | { 48 | return [ 49 | ['name', InputArgument::REQUIRED, 'The name of your module.'], 50 | ]; 51 | } 52 | 53 | /** 54 | * Get the console command options. 55 | * @return array 56 | */ 57 | protected function getOptions() 58 | { 59 | return []; 60 | } 61 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/MakeModuleCommandCommand.php: -------------------------------------------------------------------------------- 1 | moduleRepo = $moduleRepo; 35 | 36 | parent::__construct(); 37 | } 38 | 39 | /** 40 | * Execute the console command. 41 | * 42 | * @return mixed 43 | */ 44 | public function fire() 45 | { 46 | $moduleName = $this->argument('moduleName'); 47 | $module = $this->moduleRepo->getByName($moduleName); 48 | 49 | if (!$module) 50 | return $this->error("Module [$moduleName] does not exist."); 51 | 52 | $commandName = $this->argument('commandName'); 53 | 54 | $generator = new ModuleCommandGenerator($commandName, $this->laravel, $module); 55 | $generator->generate(); 56 | 57 | $this->info("The command [$commandName] has been created for module [$moduleName]."); 58 | 59 | $this->call('module:make-handler', [ 60 | 'type' => 'command', 61 | 'moduleName' => $moduleName, 62 | 'handlerName' => $commandName . 'Handler', 63 | 'handlingName' => $commandName 64 | ]); 65 | } 66 | 67 | /** 68 | * Get the console command arguments. 69 | * @return array 70 | */ 71 | protected function getArguments() 72 | { 73 | return [ 74 | ['moduleName', InputArgument::REQUIRED, 'The name of your module.'], 75 | ['commandName', InputArgument::REQUIRED, 'The name of your command.'], 76 | ]; 77 | } 78 | 79 | /** 80 | * Get the console command options. 81 | * @return array 82 | */ 83 | protected function getOptions() 84 | { 85 | return []; 86 | } 87 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/MakeModuleEventCommand.php: -------------------------------------------------------------------------------- 1 | moduleRepo = $moduleRepo; 35 | 36 | parent::__construct(); 37 | } 38 | 39 | /** 40 | * Execute the console command. 41 | * 42 | * @return mixed 43 | */ 44 | public function fire() 45 | { 46 | $moduleName = $this->argument('moduleName'); 47 | $module = $this->moduleRepo->getByName($moduleName); 48 | 49 | if (!$module) 50 | return $this->error("Module [$moduleName] does not exist."); 51 | 52 | $eventName = $this->argument('eventName'); 53 | 54 | $generator = new ModuleEventGenerator($eventName, $this->laravel, $module); 55 | $generator->generate(); 56 | 57 | $this->info("The event [$eventName] has been created for module [$moduleName]."); 58 | } 59 | 60 | /** 61 | * Get the console command arguments. 62 | * @return array 63 | */ 64 | protected function getArguments() 65 | { 66 | return [ 67 | ['moduleName', InputArgument::REQUIRED, 'The name of your module.'], 68 | ['eventName', InputArgument::REQUIRED, 'The name of your event.'], 69 | ]; 70 | } 71 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/MakeModuleHandlerCommand.php: -------------------------------------------------------------------------------- 1 | moduleRepo = $moduleRepo; 35 | 36 | parent::__construct(); 37 | } 38 | 39 | /** 40 | * Execute the console command. 41 | * 42 | * @return mixed 43 | */ 44 | public function fire() 45 | { 46 | $handlerType = $this->argument('type'); 47 | 48 | if ($handlerType != 'event' && $handlerType != 'command') 49 | return $this->error("Invalid handler type: [$handlerType]."); 50 | 51 | $moduleName = $this->argument('moduleName'); 52 | $module = $this->moduleRepo->getByName($moduleName); 53 | 54 | if (!$module) 55 | return $this->error("Module [$moduleName] does not exist."); 56 | 57 | $handlerName = $this->argument('handlerName'); 58 | $handlingName = $this->argument('handlingName'); 59 | 60 | $generator = new ModuleHandlerGenerator($handlerName, $handlingName, $handlerType, $this->laravel, $module); 61 | $generator->generate(); 62 | 63 | $this->info("The handler [$handlerName] has been created for module [$moduleName]."); 64 | } 65 | 66 | /** 67 | * Get the console command arguments. 68 | * @return array 69 | */ 70 | protected function getArguments() 71 | { 72 | return [ 73 | ['type', InputArgument::REQUIRED, 'The type of handler to create (event or command).'], 74 | ['moduleName', InputArgument::REQUIRED, 'The name of your module.'], 75 | ['handlerName', InputArgument::REQUIRED, 'The name of your handler.'], 76 | ['handlingName', InputArgument::REQUIRED, 'The name of the class that the handler is handling.'] 77 | ]; 78 | } 79 | 80 | /** 81 | * Get the console command options. 82 | * @return array 83 | */ 84 | protected function getOptions() 85 | { 86 | return []; 87 | } 88 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/MakeModuleMigrationCommand.php: -------------------------------------------------------------------------------- 1 | moduleRepo = $moduleRepo; 35 | 36 | parent::__construct(); 37 | } 38 | 39 | /** 40 | * Execute the console command. 41 | * 42 | * @return mixed 43 | */ 44 | public function fire() 45 | { 46 | $moduleName = $this->argument('name'); 47 | $module = $this->moduleRepo->getByName($moduleName); 48 | 49 | if (!$module) 50 | return $this->error("Module [$moduleName] does not exist."); 51 | 52 | $migrationName = $this->argument('migration'); 53 | 54 | $generator = new ModuleMigrationGenerator($migrationName, $this->laravel, $module); 55 | $generator->generate(); 56 | 57 | $this->info("The migration [$migrationName] has been created for module [$moduleName]."); 58 | } 59 | 60 | /** 61 | * Get the console command arguments. 62 | * @return array 63 | */ 64 | protected function getArguments() 65 | { 66 | return [ 67 | ['name', InputArgument::REQUIRED, 'The name of your module.'], 68 | ['migration', InputArgument::REQUIRED, 'The name of your migration.'], 69 | ]; 70 | } 71 | 72 | /** 73 | * Get the console command options. 74 | * @return array 75 | */ 76 | protected function getOptions() 77 | { 78 | return []; 79 | } 80 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/MakeModuleRequestCommand.php: -------------------------------------------------------------------------------- 1 | moduleRepo = $moduleRepo; 35 | 36 | parent::__construct(); 37 | } 38 | 39 | /** 40 | * Execute the console command. 41 | * 42 | * @return mixed 43 | */ 44 | public function fire() 45 | { 46 | $moduleName = $this->argument('name'); 47 | $module = $this->moduleRepo->getByName($moduleName); 48 | 49 | if (!$module) 50 | return $this->error("Module [$moduleName] does not exist."); 51 | 52 | $requestName = $this->argument('request'); 53 | 54 | $generator = new ModuleRequestGenerator($requestName, $this->laravel, $module); 55 | $generator->generate(); 56 | 57 | $this->info("The request [$requestName] has been generated for module [$moduleName]."); 58 | } 59 | 60 | /** 61 | * Get the console command arguments. 62 | * @return array 63 | */ 64 | protected function getArguments() 65 | { 66 | return [ 67 | ['name', InputArgument::REQUIRED, 'The name of your module.'], 68 | ['request', InputArgument::REQUIRED, 'The name of your request.'], 69 | ]; 70 | } 71 | 72 | /** 73 | * Get the console command options. 74 | * @return array 75 | */ 76 | protected function getOptions() 77 | { 78 | return []; 79 | } 80 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/ModuleListCommand.php: -------------------------------------------------------------------------------- 1 | moduleRepo = $moduleRepo; 34 | 35 | parent::__construct(); 36 | } 37 | 38 | /** 39 | * Execute the console command. 40 | */ 41 | public function fire() 42 | { 43 | $modules = $this->moduleRepo->getModules(); 44 | 45 | foreach ($modules as $module) 46 | $rows[] = $this->moduleToRow($module); 47 | 48 | $this->table(['Name', 'Description', 'Status'], $rows); 49 | } 50 | 51 | /** 52 | * Converts a module object to a table row. 53 | * @param Module $module 54 | * @return array 55 | */ 56 | public function moduleToRow(Module $module) 57 | { 58 | return [ 59 | 'name' => $module->name, 60 | 'description' => $module->description, 61 | 'enabled' => $module->isEnabled() ? 'Enabled' : 'Disabled' 62 | ]; 63 | } 64 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/ModuleMigrateCommand.php: -------------------------------------------------------------------------------- 1 | moduleRepo = $moduleRepo; 35 | 36 | parent::__construct(); 37 | } 38 | 39 | /** 40 | * Execute the console command. 41 | */ 42 | public function fire() 43 | { 44 | $moduleName = $this->argument('name'); 45 | $module = $this->moduleRepo->getByName($moduleName); 46 | 47 | if (!$module) 48 | return $this->error("Module [$moduleName] does not exist."); 49 | 50 | $this->call('migrate', $this->getParameters($module)); 51 | } 52 | 53 | /** 54 | * Get the console command parameters. 55 | * @param Module $module 56 | * @return array 57 | */ 58 | protected function getParameters(Module $module) 59 | { 60 | $params = []; 61 | 62 | $params['--path'] = str_replace(base_path(), '', $module->getPath()); 63 | $params['--path'] .= 'Database/Migrations/'; 64 | 65 | if ($option = $this->option('database')) $params['--database'] = $option; 66 | if ($option = $this->option('force')) $params['--force'] = $option; 67 | if ($option = $this->option('pretend')) $params['--pretend'] = $option; 68 | if ($option = $this->option('seed')) $params['--seed'] = $option; 69 | 70 | return $params; 71 | } 72 | 73 | /** 74 | * Get the console command arguments. 75 | * @return array 76 | */ 77 | protected function getArguments() 78 | { 79 | return [ 80 | ['name', InputArgument::REQUIRED, 'The name of the module.'], 81 | ]; 82 | } 83 | 84 | /** 85 | * Get the console command options. 86 | * @return array 87 | */ 88 | protected function getOptions() 89 | { 90 | return [ 91 | ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'], 92 | ['force', null, InputOption::VALUE_NONE, 'Force the operation to run while in production.'], 93 | ['pretend', null, InputOption::VALUE_OPTIONAL, 'Dump the SQL queries that would be run.'], 94 | ['seed', null, InputOption::VALUE_OPTIONAL, 'Indicates if the seed task should be re-run.'] 95 | ]; 96 | } 97 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/ModuleMigrateRollbackCommand.php: -------------------------------------------------------------------------------- 1 | moduleRepo = $moduleRepo; 35 | 36 | parent::__construct(); 37 | } 38 | 39 | /** 40 | * Execute the console command. 41 | */ 42 | public function fire() 43 | { 44 | $moduleName = $this->argument('name'); 45 | $module = $this->moduleRepo->getByName($moduleName); 46 | 47 | if (!$module) 48 | return $this->error("Module [$moduleName] does not exist."); 49 | 50 | $migrationsPath = $module->getMigrationPath(); 51 | $files = $this->laravel['files']->glob($migrationsPath . '*'); 52 | 53 | foreach ($files as $file) { 54 | $this->laravel['files']->requireOnce($file); 55 | } 56 | 57 | $this->call('migrate:rollback', $this->getParameters($module)); 58 | } 59 | 60 | /** 61 | * Get the console command parameters. 62 | * @param Module $module 63 | * @return array 64 | */ 65 | protected function getParameters(Module $module) 66 | { 67 | $params = []; 68 | 69 | if ($option = $this->option('database')) $params['--database'] = $option; 70 | if ($option = $this->option('force')) $params['--force'] = $option; 71 | if ($option = $this->option('pretend')) $params['--pretend'] = $option; 72 | 73 | return $params; 74 | } 75 | 76 | /** 77 | * Get the console command arguments. 78 | * @return array 79 | */ 80 | protected function getArguments() 81 | { 82 | return [ 83 | ['name', InputArgument::REQUIRED, 'The name of the module.'], 84 | ]; 85 | } 86 | 87 | /** 88 | * Get the console command options. 89 | * @return array 90 | */ 91 | protected function getOptions() 92 | { 93 | return [ 94 | ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'], 95 | ['force', null, InputOption::VALUE_NONE, 'Force the operation to run while in production.'], 96 | ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'] 97 | ]; 98 | } 99 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/ModuleSeedCommand.php: -------------------------------------------------------------------------------- 1 | moduleRepo = $moduleRepo; 35 | 36 | parent::__construct(); 37 | } 38 | 39 | /** 40 | * Execute the console command. 41 | */ 42 | public function fire() 43 | { 44 | $name = $this->argument('name'); 45 | $module = $this->moduleRepo->getByName($name); 46 | 47 | if (!$module) 48 | return $this->error("The [$name] module does not exist."); 49 | 50 | $params = $this->getParameters($module); 51 | 52 | $this->call('db:seed', $params); 53 | } 54 | 55 | /** 56 | * Get the console command parameters. 57 | * @param Module $module 58 | * @return array 59 | */ 60 | protected function getParameters(Module $module) 61 | { 62 | $params = []; 63 | 64 | $namespace = $module->getNamespace(); 65 | $dbSeeder = $module->getName() . 'DatabaseSeeder'; 66 | $dbSeeder = $namespace . 'Database\Seeders\\' . $dbSeeder; 67 | 68 | $params['--class'] = $this->option('class') ? $this->option('class') : $dbSeeder; 69 | $params['--database'] = $this->option('database'); 70 | 71 | return $params; 72 | } 73 | 74 | /** 75 | * Get the console command arguments. 76 | * @return array 77 | */ 78 | protected function getArguments() 79 | { 80 | return [ 81 | ['name', InputArgument::REQUIRED, 'The name of the module.'], 82 | ]; 83 | } 84 | 85 | /** 86 | * Get the console command options. 87 | * @return array 88 | */ 89 | protected function getOptions() 90 | { 91 | return [ 92 | ['class', null, InputOption::VALUE_OPTIONAL, 'The class name of the module\'s root seeder.'], 93 | ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed.'] 94 | ]; 95 | } 96 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/stubs/moduleBusProvider.stub: -------------------------------------------------------------------------------- 1 | mapUsing(function($command) { 17 | return Dispatcher::simpleMapping($command, '{namespace}Commands', '{namespace}Handlers\Commands'); 18 | }); 19 | } 20 | 21 | /** 22 | * Register any application services. 23 | */ 24 | public function register() 25 | { 26 | // 27 | } 28 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/stubs/moduleCommand.stub: -------------------------------------------------------------------------------- 1 | call('{namespace}Database\Seeders\UserTableSeeder'); 18 | } 19 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/stubs/moduleEvent.stub: -------------------------------------------------------------------------------- 1 | group(['namespace' => $this->namespace], function($router) { 29 | require base_path('Modules/{name}/Http/routes.php'); 30 | }); 31 | } 32 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Console/stubs/moduleRoutes.stub: -------------------------------------------------------------------------------- 1 | '{identifier}'], function() { 4 | Route::get('/', function() { 5 | return 'This route was generated automatically.'; 6 | }); 7 | }); -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Facades/Modules.php: -------------------------------------------------------------------------------- 1 | getStubContents($this->stub); 26 | $path = $this->getFilePath(); 27 | 28 | return app('files')->put($path, $stub); 29 | } 30 | 31 | /** 32 | * Returns the stub contents of the given stub. 33 | * @param string $stub 34 | * @return string 35 | */ 36 | protected function getStubContents($stub) 37 | { 38 | $contents = app('files')->get(__DIR__ . '/../Console/stubs/' . $stub); 39 | $namespace = 'Modules\\' . $this->getModuleName() . '\\'; 40 | 41 | $contents = str_replace('{name}', $this->getModuleName(), $contents); 42 | $contents = str_replace('{identifier}', str_slug($this->getModuleName()), $contents); 43 | $contents = str_replace('{namespace}', $namespace, $contents); 44 | 45 | if (method_exists($this, 'replaceStubContents')) 46 | $contents = $this->replaceStubContents($contents); 47 | 48 | return $contents; 49 | } 50 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Generators/ModuleCommandGenerator.php: -------------------------------------------------------------------------------- 1 | commandName = $commandName; 40 | $this->app = $app; 41 | $this->module = $module; 42 | } 43 | 44 | /** 45 | * Returns the name of the module. 46 | * @return string 47 | */ 48 | public function getModuleName() 49 | { 50 | return $this->module->getName(); 51 | } 52 | 53 | /** 54 | * Returns the path to the destination file. 55 | * @return string 56 | */ 57 | public function getFilePath() 58 | { 59 | $fileName = studly_case($this->commandName) . '.php'; 60 | 61 | return $this->module->getPath() . 'Commands/' . $fileName; 62 | } 63 | 64 | /** 65 | * @param string $contents 66 | * @return string 67 | */ 68 | public function replaceStubContents($contents) 69 | { 70 | return str_replace('{className}', studly_case($this->commandName), $contents); 71 | } 72 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Generators/ModuleEventGenerator.php: -------------------------------------------------------------------------------- 1 | eventName = $eventName; 40 | $this->app = $app; 41 | $this->module = $module; 42 | } 43 | 44 | /** 45 | * Returns the name of the module. 46 | * @return string 47 | */ 48 | public function getModuleName() 49 | { 50 | return $this->module->getName(); 51 | } 52 | 53 | /** 54 | * Returns the path to the destination file. 55 | * @return string 56 | */ 57 | public function getFilePath() 58 | { 59 | $fileName = studly_case($this->eventName) . '.php'; 60 | 61 | return $this->module->getPath() . 'Events/' . $fileName; 62 | } 63 | 64 | /** 65 | * @param string $contents 66 | * @return string 67 | */ 68 | public function replaceStubContents($contents) 69 | { 70 | return str_replace('{className}', studly_case($this->eventName), $contents); 71 | } 72 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Generators/ModuleGenerator.php: -------------------------------------------------------------------------------- 1 | 'moduleRoutes.stub', 53 | 'Providers/{name}ServiceProvider.php' => 'moduleProvider.stub', 54 | 'Providers/RouteServiceProvider.php' => 'moduleRouteProvider.stub', 55 | 'Providers/BusServiceProvider.php' => 'moduleBusProvider.stub', 56 | 'module.json' => 'moduleJson.stub', 57 | 'Database/Seeders/{name}DatabaseSeeder.php' => 'moduleDbSeeder.stub' 58 | ]; 59 | 60 | /** 61 | * Class constructor. 62 | * @param string $name 63 | * @param Filesystem $filesystem 64 | * @param Console $console 65 | */ 66 | public function __construct($name, Filesystem $filesystem, Console $console) 67 | { 68 | $this->name = $name; 69 | $this->filesystem = $filesystem; 70 | $this->console = $console; 71 | } 72 | 73 | /** 74 | * Runs the generator. 75 | */ 76 | public function generate() 77 | { 78 | $this->createModulesDir(); 79 | $this->createFolders(); 80 | $this->createFiles(); 81 | 82 | $this->console->info("Your module [$this->name] has been generated."); 83 | } 84 | 85 | /** 86 | * Returns the name of the module. 87 | * @return string 88 | */ 89 | public function getModuleName() 90 | { 91 | return $this->name; 92 | } 93 | 94 | /** 95 | * Returns the path to the destination file. 96 | * @return string 97 | */ 98 | public function getFilePath() 99 | { 100 | // Not needed for this class. 101 | } 102 | 103 | /** 104 | * Creates the modules directory if it doesn't already exist. 105 | */ 106 | protected function createModulesDir() 107 | { 108 | $path = base_path('Modules'); 109 | 110 | if (!$this->filesystem->isDirectory($path)) 111 | $this->filesystem->makeDirectory($path); 112 | } 113 | 114 | /** 115 | * Creates the module directory and other folders. 116 | */ 117 | protected function createFolders() 118 | { 119 | $path = base_path('Modules/' . $this->name); 120 | 121 | $this->filesystem->makeDirectory($path); 122 | 123 | foreach ($this->foldersToGenerate as $folder) 124 | $this->filesystem->makeDirectory($path . '/' . $folder); 125 | } 126 | 127 | /** 128 | * Creates the module files. 129 | */ 130 | protected function createFiles() 131 | { 132 | foreach ($this->filesToCreate as $file => $stub) { 133 | $file = str_replace('{name}', $this->name, $file); 134 | $path = base_path('Modules/' . $this->name . '/' . $file); 135 | $stub = $this->getStubContents($stub); 136 | 137 | $this->filesystem->put($path, $stub); 138 | } 139 | } 140 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Generators/ModuleHandlerGenerator.php: -------------------------------------------------------------------------------- 1 | handlerName = $handlerName; 46 | $this->handlerType = $handlerType; 47 | $this->handlingName = $handlingName; 48 | $this->app = $app; 49 | $this->module = $module; 50 | 51 | $this->stub = $handlerType == 'event' ? 'moduleEventHandler.stub' : 'moduleCommandHandler.stub'; 52 | } 53 | 54 | /** 55 | * Returns the name of the module. 56 | * @return string 57 | */ 58 | public function getModuleName() 59 | { 60 | return $this->module->getName(); 61 | } 62 | 63 | /** 64 | * Returns the path to the destination file. 65 | * @return string 66 | */ 67 | public function getFilePath() 68 | { 69 | $fileName = studly_case($this->handlerName) . '.php'; 70 | 71 | return $this->module->getPath() . 'Handlers/' . ucfirst($this->handlerType) . 's/' . $fileName; 72 | } 73 | 74 | /** 75 | * @param string $contents 76 | * @return string 77 | */ 78 | public function replaceStubContents($contents) 79 | { 80 | $contents = str_replace('{className}', studly_case($this->handlerName), $contents); 81 | $contents = str_replace('{handlerType}', studly_case($this->handlerType), $contents); 82 | $contents = str_replace('{handlingClass}', studly_case($this->handlingName), $contents); 83 | 84 | return $contents; 85 | } 86 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Generators/ModuleMigrationGenerator.php: -------------------------------------------------------------------------------- 1 | migrationName = $migrationName; 40 | $this->app = $app; 41 | $this->module = $module; 42 | } 43 | 44 | /** 45 | * Returns the name of the module. 46 | * @return string 47 | */ 48 | public function getModuleName() 49 | { 50 | return $this->module->getName(); 51 | } 52 | 53 | /** 54 | * Generates a migration filename. 55 | * @return string 56 | */ 57 | public function getMigrationName() 58 | { 59 | return date('Y_m_d_His') . '_' . studly_case($this->migrationName) . '.php'; 60 | } 61 | 62 | /** 63 | * Returns the path to the destination file. 64 | * @return string 65 | */ 66 | public function getFilePath() 67 | { 68 | $migrationName = $this->getMigrationName(); 69 | 70 | return $this->module->getPath() . 'Database/Migrations/' . $migrationName; 71 | } 72 | 73 | /** 74 | * @param string $contents 75 | * @return string 76 | */ 77 | public function replaceStubContents($contents) 78 | { 79 | return str_replace('{className}', studly_case($this->migrationName), $contents); 80 | } 81 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Generators/ModuleRequestGenerator.php: -------------------------------------------------------------------------------- 1 | requestName = $requestName; 40 | $this->app = $app; 41 | $this->module = $module; 42 | } 43 | 44 | /** 45 | * Returns the name of the module. 46 | * @return string 47 | */ 48 | public function getModuleName() 49 | { 50 | return $this->module->getName(); 51 | } 52 | 53 | /** 54 | * Generates a migration filename. 55 | * @return string 56 | */ 57 | public function getRequestName() 58 | { 59 | return studly_case($this->requestName); 60 | } 61 | 62 | /** 63 | * Returns the path to the destination file. 64 | * @return string 65 | */ 66 | public function getFilePath() 67 | { 68 | return $this->module->getPath() . 'Http/Requests/' . $this->getRequestName() . '.php'; 69 | } 70 | 71 | /** 72 | * @param string $contents 73 | * @return string 74 | */ 75 | public function replaceStubContents($contents) 76 | { 77 | return str_replace('{className}', studly_case($this->requestName), $contents); 78 | } 79 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Module.php: -------------------------------------------------------------------------------- 1 | app = $app; 32 | $this->name = $name; 33 | } 34 | 35 | /** 36 | * Returns the module name. 37 | * @return string 38 | */ 39 | public function getName() 40 | { 41 | return $this->getData()->name; 42 | } 43 | 44 | /** 45 | * Returns the module namespace. 46 | * @return string 47 | */ 48 | public function getNamespace() 49 | { 50 | return 'Modules\\' . $this->name . '\\'; 51 | } 52 | 53 | /** 54 | * Returns the data for the module. 55 | * @return array 56 | */ 57 | public function getData() 58 | { 59 | if (!$this->data) 60 | $this->data = $this->loadData(); 61 | 62 | return $this->data; 63 | } 64 | 65 | /** 66 | * Determines whether the module is enabled. 67 | * @return boolean 68 | */ 69 | public function isEnabled() 70 | { 71 | return $this->getData()->enabled; 72 | } 73 | 74 | /** 75 | * Returns the path to the module directory. 76 | * @return string 77 | */ 78 | public function getPath() 79 | { 80 | return base_path('Modules/' . $this->name . '/'); 81 | } 82 | 83 | /** 84 | * Returns the path to the database migrations directory. 85 | * @return string 86 | */ 87 | public function getMigrationPath() 88 | { 89 | return $this->getPath() . 'Database/Migrations/'; 90 | } 91 | 92 | /** 93 | * Enables the module. 94 | * @return mixed 95 | */ 96 | public function enable() 97 | { 98 | return $this->setData('enabled', true); 99 | } 100 | 101 | /** 102 | * Disables the module. 103 | * @return mixed 104 | */ 105 | public function disable() 106 | { 107 | return $this->setData('enabled', false); 108 | } 109 | 110 | /** 111 | * Registers the service provider. 112 | * @return mixed 113 | */ 114 | public function registerServiceProvider() 115 | { 116 | $provider = 'Modules\\' . $this->name . '\\Providers\\' . $this->name . 'ServiceProvider'; 117 | 118 | return $this->app->register($provider); 119 | } 120 | 121 | /** 122 | * Quicker method to access the module data. 123 | * @param string $key 124 | * @return mixed 125 | */ 126 | public function __get($key) 127 | { 128 | return $this->getData()->$key; 129 | } 130 | 131 | /** 132 | * Returns the JSON decoded module data, loaded from the module.json file. 133 | * @return array 134 | */ 135 | protected function loadData() 136 | { 137 | $jsonPath = $this->getPath() . 'module.json'; 138 | $contents = $this->app['files']->get($jsonPath); 139 | 140 | return json_decode($contents); 141 | } 142 | 143 | /** 144 | * Sets a value in the module.json file and saves it if $save is true. 145 | * @param string $key 146 | * @param mixed $value 147 | * @param boolean $save 148 | * @return mixed 149 | */ 150 | protected function setData($key, $value, $save = true) 151 | { 152 | $data = $this->getData(); 153 | $data->$key = $value; 154 | 155 | if (!$save) 156 | return $this->data = $data; 157 | 158 | $data = json_encode($this->data, \JSON_PRETTY_PRINT); 159 | $path = $this->getPath() . 'module.json'; 160 | 161 | return $this->app['files']->put($path, $data); 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /src/Mrterryh/Modules/ModulesServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bindShared('modules', function($app) { 38 | return new Repository($app, $app['files']); 39 | }); 40 | 41 | $this->commands($this->commands); 42 | } 43 | 44 | /** 45 | * Boot the service provider. 46 | */ 47 | public function boot() 48 | { 49 | $this->app['modules']->register(); 50 | } 51 | 52 | /** 53 | * Get the services provided by the provider. 54 | * @return array 55 | */ 56 | public function provides() 57 | { 58 | return ['modules']; 59 | } 60 | } -------------------------------------------------------------------------------- /src/Mrterryh/Modules/Repository.php: -------------------------------------------------------------------------------- 1 | app = $app; 33 | $this->filesystem = $filesystem; 34 | } 35 | 36 | /** 37 | * Boots the modules individually. 38 | */ 39 | public function register() 40 | { 41 | $modules = $this->getEnabledModules(); 42 | 43 | foreach ($modules as $module) 44 | $module->registerServiceProvider(); 45 | } 46 | 47 | /** 48 | * Returns an array containing all enabled modules. 49 | * @return array 50 | */ 51 | public function getEnabledModules() 52 | { 53 | $modules = $this->getModules(); 54 | $enabledModules = []; 55 | 56 | foreach ($modules as $module) { 57 | if ($module->isEnabled()) $enabledModules[] = $module; 58 | } 59 | 60 | return $enabledModules; 61 | } 62 | 63 | /** 64 | * Returns all modules in an array, either by returning the cached 65 | * $modules property, or scanning the modules directory if it 66 | * hasn't been populated yet. 67 | * @return array 68 | */ 69 | public function getModules() 70 | { 71 | if (!$this->modules) 72 | $this->modules = $this->scanModulesDirectory(); 73 | 74 | return $this->modules; 75 | } 76 | 77 | /** 78 | * Returns a module by its name, or false if it doesn't exist. 79 | * @param string $name 80 | * @return Module|bool 81 | */ 82 | public function getByName($name) 83 | { 84 | foreach ($this->getModules() as $module) { 85 | if ($module->getName() == $name) return $module; 86 | } 87 | 88 | return false; 89 | } 90 | 91 | /** 92 | * Scans the modules directory for modules and returns an array 93 | * of Module objects. 94 | * @return array 95 | */ 96 | protected function scanModulesDirectory() 97 | { 98 | $modules = []; 99 | 100 | $path = base_path('Modules'); 101 | $files = $this->filesystem->glob($path . '/*/module.json'); 102 | 103 | foreach ($files as $file) { 104 | $moduleName = dirname($file); 105 | $moduleData = $this->getJsonContents($file); 106 | 107 | $modules[] = $this->createModuleObject($moduleData->name); 108 | } 109 | 110 | return $modules; 111 | } 112 | 113 | /** 114 | * Grabs the contents of the given JSON file 115 | * and decodes it into an array. 116 | * @param string $filePath 117 | * @return array 118 | */ 119 | protected function getJsonContents($filePath) 120 | { 121 | return json_decode($this->filesystem->get($filePath)); 122 | } 123 | 124 | /** 125 | * Creates a Module object. 126 | * @param string $identifier 127 | * @return Module 128 | */ 129 | protected function createModuleObject($identifier) 130 | { 131 | return new Module($this->app, $identifier); 132 | } 133 | } -------------------------------------------------------------------------------- /tests/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrterryh/Modules/73a424387b91e338557f8fec94a46fdbd81a53c3/tests/.gitkeep --------------------------------------------------------------------------------