diff --git a/composer.json b/composer.json index e8e98a0..d49de6b 100644 --- a/composer.json +++ b/composer.json @@ -24,18 +24,12 @@ }, "autoload": { "psr-4": { - "Akaunting\\Module\\": "./src" + "Akaunting\\Module\\": "src" }, "files": [ "src/helpers.php" ] }, - "autoload-dev": { - "psr-4": { - "Akaunting\\Module\\Tests\\": "tests", - "Modules\\Recipe\\": "tests/stubs/valid/Recipe" - } - }, "extra": { "laravel": { "providers": [ diff --git a/src/Commands/GeneratorCommand.php b/src/Commands/GeneratorCommand.php index 029bbf5..850bd91 100644 --- a/src/Commands/GeneratorCommand.php +++ b/src/Commands/GeneratorCommand.php @@ -43,7 +43,8 @@ public function handle() $contents = $this->getTemplateContents(); try { - with(new FileGenerator($path, $contents))->generate(); + $overwrite_file = $this->hasOption('force') ? $this->option('force') : false; + (new FileGenerator($path, $contents))->withFileOverwrite($overwrite_file)->generate(); $this->info("Created : {$path}"); } catch (FileAlreadyExistException $e) { diff --git a/src/Commands/MigrateCommand.php b/src/Commands/MigrateCommand.php index 4bcf738..97a556e 100644 --- a/src/Commands/MigrateCommand.php +++ b/src/Commands/MigrateCommand.php @@ -60,7 +60,7 @@ public function handle() */ protected function migrate(Module $module) { - $path = str_replace(base_path(), '', (new Migrator($module))->getPath()); + $path = str_replace(base_path(), '', (new Migrator($module, $this->getLaravel()))->getPath()); if ($this->option('subpath')) { $path = $path . "/" . $this->option("subpath"); diff --git a/src/Commands/MigrateResetCommand.php b/src/Commands/MigrateResetCommand.php index 295a000..29fd6a7 100644 --- a/src/Commands/MigrateResetCommand.php +++ b/src/Commands/MigrateResetCommand.php @@ -64,7 +64,7 @@ public function reset($module) $module = $this->module->findOrFail($module); } - $migrator = new Migrator($module); + $migrator = new Migrator($module, $this->getLaravel()); $database = $this->option('database'); diff --git a/src/Commands/MigrateRollbackCommand.php b/src/Commands/MigrateRollbackCommand.php index 1072d91..79d7ca6 100644 --- a/src/Commands/MigrateRollbackCommand.php +++ b/src/Commands/MigrateRollbackCommand.php @@ -64,7 +64,7 @@ public function rollback($module) $module = $this->module->findOrFail($module); } - $migrator = new Migrator($module); + $migrator = new Migrator($module, $this->getLaravel()); $database = $this->option('database'); diff --git a/src/Commands/MigrateStatusCommand.php b/src/Commands/MigrateStatusCommand.php index d27956a..d7c70ca 100644 --- a/src/Commands/MigrateStatusCommand.php +++ b/src/Commands/MigrateStatusCommand.php @@ -59,7 +59,7 @@ public function handle() */ protected function migrateStatus(Module $module) { - $path = str_replace(base_path(), '', (new Migrator($module))->getPath()); + $path = str_replace(base_path(), '', (new Migrator($module, $this->getLaravel()))->getPath()); $this->call('migrate:status', [ '--path' => $path, diff --git a/src/Commands/PublishMigrationCommand.php b/src/Commands/PublishMigrationCommand.php index 4617ec9..6dc4b67 100644 --- a/src/Commands/PublishMigrationCommand.php +++ b/src/Commands/PublishMigrationCommand.php @@ -48,7 +48,7 @@ public function handle() */ public function publish($module) { - with(new MigrationPublisher(new Migrator($module))) + with(new MigrationPublisher(new Migrator($module, $this->getLaravel()))) ->setRepository($this->laravel['module']) ->setConsole($this) ->publish(); diff --git a/src/Commands/RouteProviderMakeCommand.php b/src/Commands/RouteProviderMakeCommand.php index 7caeada..3f3e492 100644 --- a/src/Commands/RouteProviderMakeCommand.php +++ b/src/Commands/RouteProviderMakeCommand.php @@ -6,6 +6,7 @@ use Akaunting\Module\Support\Stub; use Akaunting\Module\Traits\ModuleCommandTrait; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; class RouteProviderMakeCommand extends GeneratorCommand { @@ -39,6 +40,13 @@ protected function getArguments() ]; } + protected function getOptions() + { + return [ + ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when the file already exists.'], + ]; + } + /** * Get template contents. * @@ -86,7 +94,7 @@ protected function getDestinationFilePath() */ protected function getWebRoutesPath() { - return '/' . $this->laravel['config']->get('stubs.files.routes', 'Routes/web.php'); + return '/' . $this->laravel['modules']->config('stubs.files.routes/web', 'Routes/web.php'); } /** @@ -94,7 +102,7 @@ protected function getWebRoutesPath() */ protected function getApiRoutesPath() { - return '/' . $this->laravel['config']->get('stubs.files.routes', 'Routes/api.php'); + return '/' . $this->laravel['modules']->config('stubs.files.routes/api', 'Routes/api.php'); } public function getDefaultNamespace() : string diff --git a/src/Commands/stubs/route-provider.stub b/src/Commands/stubs/route-provider.stub index d9845ea..aa345c6 100644 --- a/src/Commands/stubs/route-provider.stub +++ b/src/Commands/stubs/route-provider.stub @@ -64,6 +64,6 @@ class $CLASS$ extends ServiceProvider Route::prefix('api') ->middleware('api') ->namespace($this->moduleNamespace) - ->group(__DIR__ . '/../Routes/api.php'); + ->group(__DIR__ . '/..$API_ROUTES_PATH$'); } } diff --git a/src/Commands/stubs/scaffold/provider.stub b/src/Commands/stubs/scaffold/provider.stub index 850b90c..636248a 100644 --- a/src/Commands/stubs/scaffold/provider.stub +++ b/src/Commands/stubs/scaffold/provider.stub @@ -90,7 +90,7 @@ class $CLASS$ extends ServiceProvider */ public function registerFactories() { - if (!app()->environment('production')) { + if (!app()->environment('production') && $this->app->runningInConsole()) { app(Factory::class)->load(__DIR__ . '/../$FACTORIES_PATH$'); } } diff --git a/src/Commands/stubs/views/index.stub b/src/Commands/stubs/views/index.stub index 81f62a2..46b22fd 100644 --- a/src/Commands/stubs/views/index.stub +++ b/src/Commands/stubs/views/index.stub @@ -6,4 +6,4 @@

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

-@stop +@endsection diff --git a/src/Contracts/RepositoryInterface.php b/src/Contracts/RepositoryInterface.php index f171493..6246798 100644 --- a/src/Contracts/RepositoryInterface.php +++ b/src/Contracts/RepositoryInterface.php @@ -100,4 +100,13 @@ public function getModulePath($moduleName); * @return \Illuminate\Filesystem\Filesystem */ public function getFiles(); + + /** + * Get a specific config data from a configuration file. + * @param string $key + * + * @param string|null $default + * @return mixed + */ + public function config(string $key, $default = null); } diff --git a/src/FileRepository.php b/src/FileRepository.php index bd4e993..2b30d7c 100644 --- a/src/FileRepository.php +++ b/src/FileRepository.php @@ -3,7 +3,11 @@ namespace Akaunting\Module; use Countable; +use Illuminate\Cache\CacheManager; use Illuminate\Container\Container; +use Illuminate\Contracts\Config\Repository as ConfigRepository; +use Illuminate\Contracts\Routing\UrlGenerator; +use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use Akaunting\Module\Contracts\RepositoryInterface; @@ -42,6 +46,26 @@ abstract class FileRepository implements RepositoryInterface, Countable */ protected $stubPath; + /** + * @var UrlGenerator + */ + private $url; + + /** + * @var ConfigRepository + */ + private $config; + + /** + * @var Filesystem + */ + private $files; + + /** + * @var CacheManager + */ + private $cache; + /** * The constructor. * @@ -52,6 +76,10 @@ public function __construct(Container $app, $path = null) { $this->app = $app; $this->path = $path; + $this->url = $app['url']; + $this->config = $app['config']; + $this->files = $app['files']; + $this->cache = $app['cache']; } /** @@ -104,8 +132,8 @@ public function getScanPaths() : array * Creates a new Module instance * * @param Container $app - * @param $name - * @param $path + * @param string $args + * @param string $path * @return \Akaunting\Module\Module */ abstract protected function createModule(...$args); @@ -122,7 +150,7 @@ public function scan() $modules = []; foreach ($paths as $key => $path) { - $manifests = $this->app['files']->glob("{$path}/module.json"); + $manifests = $this->getFiles()->glob("{$path}/module.json"); is_array($manifests) || $manifests = []; @@ -162,7 +190,7 @@ protected function formatCached($cached) $modules = []; foreach ($cached as $alias => $module) { - $path = $module["path"]; + $path = $module['path']; $modules[$alias] = $this->createModule($this->app, $alias, $path); } @@ -177,7 +205,7 @@ protected function formatCached($cached) */ public function getCached() { - return $this->app['cache']->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () { + return $this->cache->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () { return $this->toCollection()->toArray(); }); } @@ -434,9 +462,9 @@ public function assetPath($alias) : string * @param null $default * @return mixed */ - public function config($key, $default = null) + public function config(string $key, $default = null) { - return $this->app['config']->get('module.' . $key, $default); + return $this->config->get('module.' . $key, $default); } /** @@ -447,13 +475,13 @@ public function config($key, $default = null) public function getUsedStoragePath() : string { $directory = storage_path('app/modules'); - if ($this->app['files']->exists($directory) === false) { - $this->app['files']->makeDirectory($directory, 0777, true); + if ($this->getFiles()->exists($directory) === false) { + $this->getFiles()->makeDirectory($directory, 0777, true); } $path = storage_path('app/modules/modules.used'); - if (!$this->app['files']->exists($path)) { - $this->app['files']->put($path, ''); + if (!$this->getFiles()->exists($path)) { + $this->getFiles()->put($path, ''); } return $path; @@ -470,7 +498,7 @@ public function setUsed($name) { $module = $this->findOrFail($name); - $this->app['files']->put($this->getUsedStoragePath(), $module); + $this->getFiles()->put($this->getUsedStoragePath(), $module); } /** @@ -478,8 +506,8 @@ public function setUsed($name) */ public function forgetUsed() { - if ($this->app['files']->exists($this->getUsedStoragePath())) { - $this->app['files']->delete($this->getUsedStoragePath()); + if ($this->getFiles()->exists($this->getUsedStoragePath())) { + $this->getFiles()->delete($this->getUsedStoragePath()); } } @@ -490,17 +518,17 @@ public function forgetUsed() */ public function getUsedNow() : string { - return $this->findOrFail($this->app['files']->get($this->getUsedStoragePath())); + return $this->findOrFail($this->getFiles()->get($this->getUsedStoragePath())); } /** * Get laravel filesystem instance. * - * @return \Illuminate\Filesystem\Filesystem + * @return Filesystem */ public function getFiles() { - return $this->app['files']; + return $this->files; } /** @@ -528,7 +556,7 @@ public function asset($asset) : string $baseUrl = str_replace(public_path() . DIRECTORY_SEPARATOR, '', $this->getAssetsPath()); - $url = $this->app['url']->asset($baseUrl . "/{$name}/" . $url); + $url = $this->url->asset($baseUrl . "/{$name}/" . $url); return str_replace(['http://', 'https://'], '//', $url); } diff --git a/src/Generators/FileGenerator.php b/src/Generators/FileGenerator.php index d7aaa8e..ed1e365 100644 --- a/src/Generators/FileGenerator.php +++ b/src/Generators/FileGenerator.php @@ -114,12 +114,25 @@ public function setPath($path) return $this; } + public function withFileOverwrite(bool $overwrite): FileGenerator + { + $this->overwriteFile = $overwrite; + + return $this; + } + /** * Generate the file. */ public function generate() { - if (!$this->filesystem->exists($path = $this->getPath())) { + $path = $this->getPath(); + + if (!$this->filesystem->exists($path)) { + return $this->filesystem->put($path, $this->getContents()); + } + + if ($this->overwriteFile === true) { return $this->filesystem->put($path, $this->getContents()); } diff --git a/src/Migrations/Migrator.php b/src/Migrations/Migrator.php index 24ceda9..8e85e70 100644 --- a/src/Migrations/Migrator.php +++ b/src/Migrations/Migrator.php @@ -34,12 +34,13 @@ class Migrator /** * Create new instance. * - * @param \Akaunting\Module\Module $module + * @param Module $module + * @param Application $application */ - public function __construct(Module $module) + public function __construct(Module $module, Application $application) { $this->module = $module; - $this->laravel = $module->getLaravel(); + $this->laravel = $application; } /** diff --git a/src/Module.php b/src/Module.php index 337bea4..1fa5c34 100644 --- a/src/Module.php +++ b/src/Module.php @@ -2,13 +2,15 @@ namespace Akaunting\Module; +use Illuminate\Cache\CacheManager; use Illuminate\Container\Container; +use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Arr; -use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; +use Illuminate\Translation\Translator; -abstract class Module extends ServiceProvider +abstract class Module { use Macroable; @@ -38,6 +40,21 @@ abstract class Module extends ServiceProvider */ protected $moduleJson = []; + /** + * @var CacheManager + */ + private $cache; + + /** + * @var Filesystem + */ + private $files; + + /** + * @var Translator + */ + private $translator; + /** * The constructor. * @@ -45,21 +62,14 @@ abstract class Module extends ServiceProvider * @param $alias * @param $path */ - public function __construct(Container $app, $alias, $path) + public function __construct(Container $app, string $alias, $path) { - parent::__construct($app); $this->alias = $alias; $this->path = $path; - } - - /** - * Get laravel instance. - * - * @return \Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application - */ - public function getLaravel() - { - return $this->app; + $this->cache = $app['cache']; + $this->files = $app['files']; + $this->translator = $app['translator']; + $this->app = $app; } /** @@ -212,7 +222,7 @@ public function json($file = null) : Json } return Arr::get($this->moduleJson, $file, function () use ($file) { - return $this->moduleJson[$file] = new Json($this->getPath() . '/' . $file, $this->app['files']); + return $this->moduleJson[$file] = new Json($this->getPath() . '/' . $file, $this->files); }); } @@ -265,7 +275,7 @@ public function register() */ protected function fireEvent($event) { - $this->app['events']->dispatch(sprintf('modules.%s.' . $event, $this->getLowerName()), [$this]); + $this->app['events']->dispatch(sprintf('module.%s.' . $event, $this->getLowerName()), [$this]); } /** * Register the aliases from this module. @@ -423,7 +433,19 @@ protected function isLoadFilesOnBoot() private function flushCache(): void { if (config('module.cache.enabled')) { - $this->app['cache']->store()->flush(); + $this->cache->store()->flush(); } } + + /** + * Register a translation file namespace. + * + * @param string $path + * @param string $namespace + * @return void + */ + private function loadTranslationsFrom(string $path, string $namespace): void + { + $this->translator->addNamespace($namespace, $path); + } } diff --git a/src/Process/Installer.php b/src/Process/Installer.php index 53c3bc0..4360683 100644 --- a/src/Process/Installer.php +++ b/src/Process/Installer.php @@ -273,7 +273,7 @@ public function getPackageName() */ public function installViaGit() { - return new Process(sprintf( + return Process::fromShellCommandline(sprintf( 'cd %s && git clone %s %s && cd %s && git checkout %s', base_path(), $this->getRepoUrl(), @@ -290,7 +290,7 @@ public function installViaGit() */ public function installViaSubtree() { - return new Process(sprintf( + return Process::fromShellCommandline(sprintf( 'cd %s && git remote add %s %s && git subtree add --prefix=%s --squash %s %s', base_path(), $this->getModuleName(), @@ -308,7 +308,7 @@ public function installViaSubtree() */ public function installViaComposer() { - return new Process(sprintf( + return Process::fromShellCommandline(sprintf( 'cd %s && composer require %s', base_path(), $this->getPackageName() diff --git a/src/Providers/Bootstrap.php b/src/Providers/Bootstrap.php index 7da263d..023f547 100644 --- a/src/Providers/Bootstrap.php +++ b/src/Providers/Bootstrap.php @@ -2,6 +2,7 @@ namespace Akaunting\Module\Providers; +use Akaunting\Module\Contracts\RepositoryInterface; use Illuminate\Support\ServiceProvider; class Bootstrap extends ServiceProvider @@ -11,7 +12,7 @@ class Bootstrap extends ServiceProvider */ public function boot() { - $this->app['module']->boot(); + $this->app[RepositoryInterface::class]->boot(); } /** @@ -19,6 +20,6 @@ public function boot() */ public function register() { - $this->app['module']->register(); + $this->app[RepositoryInterface::class]->register(); } } diff --git a/src/Providers/Laravel.php b/src/Providers/Laravel.php index 13bde99..91ba368 100644 --- a/src/Providers/Laravel.php +++ b/src/Providers/Laravel.php @@ -35,8 +35,10 @@ public function setupStubPath() Stub::setBasePath(__DIR__ . '/Commands/stubs'); $this->app->booted(function ($app) { - if ($app['module']->config('stubs.enabled') === true) { - Stub::setBasePath($app['module']->config('stubs.path')); + $moduleRepository = $app[RepositoryInterface::class]; + + if ($moduleRepository->config('stubs.enabled') === true) { + Stub::setBasePath($moduleRepository->config('stubs.path')); } }); }