From cac45a5dbd217e223fd9edcf6662d145f6f10093 Mon Sep 17 00:00:00 2001 From: Diego Smania Date: Sun, 30 Jun 2024 11:17:21 -0300 Subject: [PATCH] Improve package resources (#1285) * [src/Console]: Improve abstract class PackageResource * [src/Console]: Improve AssetsResource class * [src/Console]: Fix Style CI in AssetsResource class * [src/Console]: Rename AssetsResource to AdminlteAssetsResoruce * [src/Console]: Fix some issues detected on AdminLtePluginCommand class * [src/Console]: Improve AuthViewsResource class * [src/Console]: Improve BasicRoutesResource class * [src/Console]: Fix Style CI in BasicRoutesResource class * [src/Console]: Rename BasicRoutesResource to AuthRoutesResource * [src/Console]: Remove BasicViewsResource * [src/Console]: Improve ConfigResource class * [src/Console]: Improve TranslationsResource class * [src/Console]: Add some fixes to AdminlteAssetsResource class * [src/Console]: Add some fixes to AuthRoutesResource class * [src/Console]: Add some fixes to AuthViewsResource class * [tests/Console]: Add fix when testing auth views resources * [src/Console]: Add some fixes to ConfigResource class * [src/Console]: Add some fixes to TranslationsResource class * [src/Console]: Rework all logic in the MainViewsResource class * [src/Console]: Rename MainViewsResource to LayoutViewsResource * [src/Console]: Improve PluginsResource class * [src/Console]: Fix method getSourceData() of PluginsResource class * [src/Console]: Revert install/uninstall return boolean There is no need to return boolean, it's better to let an exeption happen * [src/Console]: Minor fix on method documentation of PluginsResource * [src/Console] Solve some minor issues * [src/Console] Improve console tests * [tests/Console]: Minor improvements on the console tests --- src/Console/AdminLteInstallCommand.php | 22 +-- src/Console/AdminLtePluginCommand.php | 6 +- src/Console/AdminLteStatusCommand.php | 14 +- src/Console/AdminLteUpdateCommand.php | 19 +- ...esource.php => AdminlteAssetsResource.php} | 94 +++++----- .../PackageResources/AuthRoutesResource.php | 103 +++++++++++ .../PackageResources/AuthViewsResource.php | 58 +++--- .../PackageResources/BasicRoutesResource.php | 105 ----------- .../PackageResources/BasicViewsResource.php | 127 ------------- .../PackageResources/ConfigResource.php | 34 ++-- .../PackageResources/LayoutViewsResource.php | 170 ++++++++++++++++++ .../PackageResources/MainViewsResource.php | 82 --------- .../PackageResources/PackageResource.php | 31 ++-- .../PackageResources/PluginsResource.php | 90 +++++----- .../PackageResources/TranslationsResource.php | 26 +-- src/Console/stubs/home.stub | 19 -- src/Console/stubs/routes.stub | 4 - tests/Console/CommandTestCase.php | 122 +++++++------ tests/Console/InstallTest.php | 43 ++--- tests/Console/PluginsTest.php | 32 ++-- tests/Console/UpdateTest.php | 2 +- 21 files changed, 581 insertions(+), 622 deletions(-) rename src/Console/PackageResources/{AssetsResource.php => AdminlteAssetsResource.php} (66%) create mode 100644 src/Console/PackageResources/AuthRoutesResource.php delete mode 100644 src/Console/PackageResources/BasicRoutesResource.php delete mode 100644 src/Console/PackageResources/BasicViewsResource.php create mode 100644 src/Console/PackageResources/LayoutViewsResource.php delete mode 100644 src/Console/PackageResources/MainViewsResource.php delete mode 100644 src/Console/stubs/home.stub diff --git a/src/Console/AdminLteInstallCommand.php b/src/Console/AdminLteInstallCommand.php index d313ecf8..4ce4c70c 100644 --- a/src/Console/AdminLteInstallCommand.php +++ b/src/Console/AdminLteInstallCommand.php @@ -3,12 +3,11 @@ namespace JeroenNoten\LaravelAdminLte\Console; use Illuminate\Console\Command; -use JeroenNoten\LaravelAdminLte\Console\PackageResources\AssetsResource; +use JeroenNoten\LaravelAdminLte\Console\PackageResources\AdminlteAssetsResource; +use JeroenNoten\LaravelAdminLte\Console\PackageResources\AuthRoutesResource; use JeroenNoten\LaravelAdminLte\Console\PackageResources\AuthViewsResource; -use JeroenNoten\LaravelAdminLte\Console\PackageResources\BasicRoutesResource; -use JeroenNoten\LaravelAdminLte\Console\PackageResources\BasicViewsResource; use JeroenNoten\LaravelAdminLte\Console\PackageResources\ConfigResource; -use JeroenNoten\LaravelAdminLte\Console\PackageResources\MainViewsResource; +use JeroenNoten\LaravelAdminLte\Console\PackageResources\LayoutViewsResource; use JeroenNoten\LaravelAdminLte\Console\PackageResources\TranslationsResource; class AdminLteInstallCommand extends Command @@ -20,8 +19,8 @@ class AdminLteInstallCommand extends Command */ protected $signature = 'adminlte:install {--type=basic : The installation type: basic (default), enhanced or full} - {--only=* : To install only specific resources: assets, config, translations, auth_views, basic_views, basic_routes or main_views. Can\'t be used with option --with} - {--with=* : To install with additional resources: auth_views, basic_views, basic_routes or main_views} + {--only=* : To install only specific resources: assets, config, translations, auth_views, basic_routes or main_views. Can\'t be used with option --with} + {--with=* : To install with additional resources: auth_views, basic_routes or main_views} {--force : To force the overwrite of existing files} {--interactive : The installation will guide you through the process}'; @@ -79,20 +78,19 @@ public function __construct() // Fill the array with the package resources. $this->pkgResources = [ - 'assets' => new AssetsResource(), + 'assets' => new AdminlteAssetsResource(), 'config' => new ConfigResource(), 'translations' => new TranslationsResource(), - 'main_views' => new MainViewsResource(), + 'main_views' => new LayoutViewsResource(), 'auth_views' => new AuthViewsResource(), - 'basic_views' => new BasicViewsResource(), - 'basic_routes' => new BasicRoutesResource(), + 'basic_routes' => new AuthRoutesResource(), ]; // Add the resources related to each available --type option. $basic = ['assets', 'config', 'translations']; $enhanced = array_merge($basic, ['auth_views']); - $full = array_merge($enhanced, ['basic_views', 'basic_routes']); + $full = array_merge($enhanced, ['basic_routes']); $this->optTypeResources = [ 'basic' => $basic, @@ -108,7 +106,6 @@ public function __construct() 'translations' => ['translations'], 'main_views' => ['main_views'], 'auth_views' => ['auth_views'], - 'basic_views' => ['basic_views'], 'basic_routes' => ['basic_routes'], ]; @@ -117,7 +114,6 @@ public function __construct() $this->optWithResources = [ 'main_views' => ['main_views'], 'auth_views' => ['auth_views'], - 'basic_views' => ['basic_views'], 'basic_routes' => ['basic_routes'], ]; } diff --git a/src/Console/AdminLtePluginCommand.php b/src/Console/AdminLtePluginCommand.php index 348cb07f..a9c0ac2f 100644 --- a/src/Console/AdminLtePluginCommand.php +++ b/src/Console/AdminLtePluginCommand.php @@ -325,8 +325,8 @@ protected function installPlugin($pluginKey) { // Customize the output messages. - $confirmMsg = $this->plugins->getInstallMessage('install'); - $overwriteMsg = $this->plugins->getInstallMessage('overwrite'); + $confirmMsg = $this->plugins->getInstallMessage('install') ?? ''; + $overwriteMsg = $this->plugins->getInstallMessage('overwrite') ?? ''; $confirmMsg = strtr($confirmMsg, [':plugin' => $pluginKey]); $overwriteMsg = strtr($overwriteMsg, [':plugin' => $pluginKey]); @@ -420,7 +420,7 @@ protected function removePlugin($pluginKey) { // Customize the output messages. - $confirmMsg = $this->plugins->getInstallMessage('remove'); + $confirmMsg = $this->plugins->getInstallMessage('remove') ?? ''; $confirmMsg = strtr($confirmMsg, [':plugin' => $pluginKey]); // Check if the plugin is valid. diff --git a/src/Console/AdminLteStatusCommand.php b/src/Console/AdminLteStatusCommand.php index 3a952275..96789f58 100644 --- a/src/Console/AdminLteStatusCommand.php +++ b/src/Console/AdminLteStatusCommand.php @@ -3,12 +3,11 @@ namespace JeroenNoten\LaravelAdminLte\Console; use Illuminate\Console\Command; -use JeroenNoten\LaravelAdminLte\Console\PackageResources\AssetsResource; +use JeroenNoten\LaravelAdminLte\Console\PackageResources\AdminlteAssetsResource; +use JeroenNoten\LaravelAdminLte\Console\PackageResources\AuthRoutesResource; use JeroenNoten\LaravelAdminLte\Console\PackageResources\AuthViewsResource; -use JeroenNoten\LaravelAdminLte\Console\PackageResources\BasicRoutesResource; -use JeroenNoten\LaravelAdminLte\Console\PackageResources\BasicViewsResource; use JeroenNoten\LaravelAdminLte\Console\PackageResources\ConfigResource; -use JeroenNoten\LaravelAdminLte\Console\PackageResources\MainViewsResource; +use JeroenNoten\LaravelAdminLte\Console\PackageResources\LayoutViewsResource; use JeroenNoten\LaravelAdminLte\Console\PackageResources\TranslationsResource; class AdminLteStatusCommand extends Command @@ -69,13 +68,12 @@ public function __construct() // Fill the array with the package resources. $this->pkgResources = [ - 'assets' => new AssetsResource(), + 'assets' => new AdminlteAssetsResource(), 'config' => new ConfigResource(), 'translations' => new TranslationsResource(), - 'main_views' => new MainViewsResource(), + 'main_views' => new LayoutViewsResource(), 'auth_views' => new AuthViewsResource(), - 'basic_views' => new BasicViewsResource(), - 'basic_routes' => new BasicRoutesResource(), + 'basic_routes' => new AuthRoutesResource(), ]; } diff --git a/src/Console/AdminLteUpdateCommand.php b/src/Console/AdminLteUpdateCommand.php index 4cd5d314..0ebfb7b9 100644 --- a/src/Console/AdminLteUpdateCommand.php +++ b/src/Console/AdminLteUpdateCommand.php @@ -3,7 +3,7 @@ namespace JeroenNoten\LaravelAdminLte\Console; use Illuminate\Console\Command; -use JeroenNoten\LaravelAdminLte\Console\PackageResources\MainViewsResource; +use JeroenNoten\LaravelAdminLte\Console\PackageResources\LayoutViewsResource; class AdminLteUpdateCommand extends Command { @@ -27,9 +27,9 @@ class AdminLteUpdateCommand extends Command * * @var string */ - protected $mainViewsWarn = 'Outdated main views at %s + protected $layoutViewsWarn = 'Outdated layout views at %s - We detected that the package main views were previously published and they + We detected that the package layout views were previously published and they differs from the ones currently available. Note this package may not work correctly if you do not update those views manually in order to include the latest changes. In the particular case you have recently changed those views @@ -48,14 +48,15 @@ public function handle() $this->call('adminlte:install', $options); - // When the main views were previously installed and they differs from - // the original ones, alarm the user that those views may require a - // manual update. + // When the layout views were previously published and they differs + // from the package default ones, alarm the user to notify that those + // views may require a manual update. - $mainViewsRes = new MainViewsResource(); + $layoutViewsRes = new LayoutViewsResource(); - if ($mainViewsRes->exists() && ! $mainViewsRes->installed()) { - $this->info(sprintf($this->mainViewsWarn, $mainViewsRes->target)); + if ($layoutViewsRes->exists() && ! $layoutViewsRes->installed()) { + $msg = sprintf($this->layoutViewsWarn, $layoutViewsRes->target); + $this->info($msg); } } } diff --git a/src/Console/PackageResources/AssetsResource.php b/src/Console/PackageResources/AdminlteAssetsResource.php similarity index 66% rename from src/Console/PackageResources/AssetsResource.php rename to src/Console/PackageResources/AdminlteAssetsResource.php index d38a3051..30f8048d 100644 --- a/src/Console/PackageResources/AssetsResource.php +++ b/src/Console/PackageResources/AdminlteAssetsResource.php @@ -5,7 +5,7 @@ use Illuminate\Support\Facades\File; use JeroenNoten\LaravelAdminLte\Helpers\CommandHelper; -class AssetsResource extends PackageResource +class AdminlteAssetsResource extends PackageResource { /** * Create a new resource instance. @@ -16,16 +16,18 @@ public function __construct() { // Fill the resource data. - $this->description = 'The AdminLTE required assets'; + $this->description = 'The set of files required to use the AdminLTE template'; $this->target = public_path('vendor'); $this->required = true; // Define the array of required assets (source). + $adminltePath = base_path('vendor/almasaeed2010/adminlte/'); + $this->source = [ 'adminlte' => [ 'name' => 'AdminLTE v3', - 'source' => base_path('vendor/almasaeed2010/adminlte/'), + 'source' => $adminltePath, 'target' => public_path('vendor/adminlte/'), 'resources' => [ [ @@ -46,22 +48,22 @@ public function __construct() ], 'fontawesome' => [ 'name' => 'FontAwesome 5 Free', - 'source' => base_path('vendor/almasaeed2010/adminlte/plugins/fontawesome-free'), + 'source' => $adminltePath.'/plugins/fontawesome-free', 'target' => public_path('vendor/fontawesome-free'), ], 'bootstrap' => [ - 'name' => 'Bootstrap 4 (js files only)', - 'source' => base_path('vendor/almasaeed2010/adminlte/plugins/bootstrap'), + 'name' => 'Bootstrap 4 (only JS files)', + 'source' => $adminltePath.'/plugins/bootstrap', 'target' => public_path('vendor/bootstrap'), ], 'popper' => [ 'name' => 'Popper.js (Bootstrap 4 requirement)', - 'source' => base_path('vendor/almasaeed2010/adminlte/plugins/popper'), + 'source' => $adminltePath.'/plugins/popper', 'target' => public_path('vendor/popper'), ], 'jquery' => [ 'name' => 'jQuery (Bootstrap 4 requirement)', - 'source' => base_path('vendor/almasaeed2010/adminlte/plugins/jquery'), + 'source' => $adminltePath.'/plugins/jquery', 'target' => public_path('vendor/jquery'), 'ignore' => [ 'core.js', @@ -72,7 +74,7 @@ public function __construct() ], 'overlay' => [ 'name' => 'Overlay Scrollbars', - 'source' => base_path('vendor/almasaeed2010/adminlte/plugins/overlayScrollbars'), + 'source' => $adminltePath.'/plugins/overlayScrollbars', 'target' => public_path('vendor/overlayScrollbars'), ], ]; @@ -80,20 +82,20 @@ public function __construct() // Fill the set of installation messages. $this->messages = [ - 'install' => 'Install the basic package assets?', - 'overwrite' => 'The basic assets already exists. Want to replace the assets?', - 'success' => 'Basic assets installed successfully.', + 'install' => 'Do you want to publish the AdminLTE asset files?', + 'overwrite' => 'AdminLTE asset files were already published. Want to replace?', + 'success' => 'AdminLTE assets files published successfully', ]; } /** - * Install/Export the resource. + * Installs or publishes the resource. * * @return void */ public function install() { - // Install the AdminLTE basic assets. + // Install the AdminLTE asset files. foreach ($this->source as $asset) { $this->installAsset($asset); @@ -101,13 +103,13 @@ public function install() } /** - * Uninstall/Remove the resource. + * Uninstalls the resource. * * @return void */ public function uninstall() { - // Uninstall the AdminLTE basic assets. + // Uninstall the AdminLTE asset files. foreach ($this->source as $asset) { $this->uninstallAsset($asset); @@ -115,7 +117,7 @@ public function uninstall() } /** - * Check if the resource already exists on the target destination. + * Checks whether the resource already exists in the target location. * * @return bool */ @@ -131,7 +133,8 @@ public function exists() } /** - * Check if the resource is correctly installed. + * Checks whether the resource is correctly installed, i.e. if the source + * items matches with the items available at the target location. * * @return bool */ @@ -147,42 +150,42 @@ public function installed() } /** - * Install the specified AdminLTE asset. + * Installs the specified AdminLTE asset. * * @param array $asset An array with the asset data * @return void */ protected function installAsset($asset) { - // Check if we just need to export the entire asset. + // Check if we just need to publish the entire asset. if (! isset($asset['resources'])) { - $this->exportResource($asset); + $this->publishResource($asset); return; } - // Otherwise, export only the specified asset resources. + // Otherwise, publish only the specified asset resources. foreach ($asset['resources'] as $res) { $res['target'] = $res['target'] ?? $res['source']; $res['target'] = $asset['target'].$res['target']; $res['source'] = $asset['source'].$res['source']; - $this->exportResource($res); + $this->publishResource($res); } } /** - * Exports the specified resource (usually a file or folder). + * Publishes the specified resource (usually a file or folder). * * @param array $res An array with the resource data * @return void */ - protected function exportResource($res) + protected function publishResource($res) { - // Check the resource type in order to copy it. + // Check whether the resource is a file or a directory. - if (is_dir($res['source'])) { + if (File::isDirectory($res['source'])) { CommandHelper::copyDirectory( $res['source'], $res['target'], @@ -190,32 +193,32 @@ protected function exportResource($res) $res['recursive'] ?? true, $res['ignore'] ?? [] ); - } elseif (is_file($res['source'])) { - File::ensureDirectoryExists(dirname($res['target'])); - copy($res['source'], $res['target']); + } else { + File::ensureDirectoryExists(File::dirname($res['target'])); + File::copy($res['source'], $res['target']); } } /** - * Check if the specified asset already exists on the target destination. + * Checks whether the specified asset already exists in the target location. * * @param array $asset An array with the asset data * @return bool */ protected function assetExists($asset) { - return is_dir($asset['target']); + return File::exists($asset['target']); } /** - * Check if the specified asset is correctly installed. + * Checks whether the specified asset is correctly installed. * * @param array $asset An array with the asset data * @return bool */ protected function assetInstalled($asset) { - // Check if the asset have resources. + // Check whether the asset has resources or not. if (! isset($asset['resources'])) { return $this->resourceInstalled($asset); @@ -235,34 +238,29 @@ protected function assetInstalled($asset) } /** - * Check if the specified resource is correctly installed. + * Checks whether the specified resource is correctly installed. * * @param array $res An array with the resource data * @return bool */ protected function resourceInstalled($res) { - $installed = false; + // Check whether the resource is a file or a directory. - if (is_dir($res['source'])) { - $installed = (bool) CommandHelper::compareDirectories( + if (File::isDirectory($res['source'])) { + return (bool) CommandHelper::compareDirectories( $res['source'], $res['target'], $res['recursive'] ?? true, $res['ignore'] ?? [] ); - } elseif (is_file($res['source'])) { - $installed = CommandHelper::compareFiles( - $res['source'], - $res['target'] - ); } - return $installed; + return CommandHelper::compareFiles($res['source'], $res['target']); } /** - * Uninstall or remove the specified asset. + * Uninstalls the specified asset. * * @param array $asset An array with the asset data * @return void @@ -271,9 +269,11 @@ protected function uninstallAsset($asset) { $target = $asset['target']; - // Uninstall the asset (actually, the target should be a folder). + // Uninstall the specified asset. Note the asset target location is + // always a folder. When the target folder does not exists, we consider + // the asset as uninstalled. - if (is_dir($target)) { + if (File::isDirectory($target)) { File::deleteDirectory($target); } } diff --git a/src/Console/PackageResources/AuthRoutesResource.php b/src/Console/PackageResources/AuthRoutesResource.php new file mode 100644 index 00000000..7b2610d9 --- /dev/null +++ b/src/Console/PackageResources/AuthRoutesResource.php @@ -0,0 +1,103 @@ +description = 'The set of routes for the Laravel/UI auth scaffolding'; + $this->source = CommandHelper::getStubPath('routes.stub'); + $this->target = base_path('routes/web.php'); + $this->required = false; + + // Fill the installation messages. + + $this->messages = [ + 'install' => 'Do you want to publish the Laravel/UI auth routes?', + 'overwrite' => 'The auth routes were already published. Want to publish again?', + 'success' => 'Auth routes published successfully', + ]; + } + + /** + * Installs or publishes the resource. + * + * @return void + */ + public function install() + { + // If the routes already exists, we won't publish they again. + + if ($this->exists()) { + return; + } + + // Get the set of routes to be published. + + $routes = File::get($this->source); + + // Add the routes to the web routes file. + + File::ensureDirectoryExists(File::dirname($this->target)); + File::append($this->target, $routes); + } + + /** + * Uninstalls the resource. + * + * @return void + */ + public function uninstall() + { + // Get the set of routes to be removed. + + $routes = File::get($this->source); + + // If the target routes file exists, then remove the auth routes. + // Otherwise, we consider the routes as uninstalled. + + if (File::isFile($this->target)) { + $targetContent = File::get($this->target); + $targetContent = str_replace($routes, '', $targetContent); + File::put($this->target, $targetContent); + } + } + + /** + * Checks whether the resource already exists in the target location. + * + * @return bool + */ + public function exists() + { + $routes = File::get($this->source); + + // Check whether the target routes file exists and contains the + // expected routes. + + return File::isFile($this->target) + && (strpos(File::get($this->target), $routes) !== false); + } + + /** + * Checks whether the resource is correctly installed, i.e. if the source + * items matches with the items available at the target location. + * + * @return bool + */ + public function installed() + { + return $this->exists(); + } +} diff --git a/src/Console/PackageResources/AuthViewsResource.php b/src/Console/PackageResources/AuthViewsResource.php index fcec3812..6f716fe7 100644 --- a/src/Console/PackageResources/AuthViewsResource.php +++ b/src/Console/PackageResources/AuthViewsResource.php @@ -8,7 +8,8 @@ class AuthViewsResource extends PackageResource { /** - * Array with the replacement content of the authentication views. + * Array with the replacement content for the authentication views of the + * legacy Laravel/UI package. * * @var array */ @@ -30,7 +31,7 @@ public function __construct() { // Fill the resource data. - $this->description = 'The default package authentication views'; + $this->description = 'The set of AdminLTE replacement auth views for the Laravel/UI package'; $this->source = $this->authViewsContent; $this->target = CommandHelper::getViewPath('auth'); $this->required = false; @@ -38,60 +39,63 @@ public function __construct() // Fill the set of installation messages. $this->messages = [ - 'install' => 'Install the AdminLTE authentication views?', - 'overwrite' => 'The authentication views already exists. Want to replace the views?', - 'success' => 'Authentication views installed successfully.', + 'install' => 'Do you want to publish the replacement auth views for Laravel/UI?', + 'overwrite' => 'The auth views were already published. Want to replace?', + 'success' => 'Auth views published successfully', ]; } /** - * Install/Export the resource. + * Installs or publishes the resource. * * @return void */ public function install() { - // Install the authentication views. We going to replace the content - // of any existing authentication view. + // Publish the authentication views. We actually need to replace the + // content of any existing authentication view that were originally + // provided by the legacy Laravel/UI package. foreach ($this->source as $file => $content) { $target = $this->target.DIRECTORY_SEPARATOR.$file; - File::ensureDirectoryExists(dirname($target)); - file_put_contents($target, $content); + File::ensureDirectoryExists(File::dirname($target)); + File::put($target, $content); } } /** - * Uninstall/Remove the resource. + * Uninstalls the resource. * * @return void */ public function uninstall() { - // Remove the package authentication views. + // Remove the published authentication views. foreach ($this->source as $file => $content) { $target = $this->target.DIRECTORY_SEPARATOR.$file; - if (is_file($target)) { - unlink($target); + if (File::isFile($target)) { + File::delete($target); } } } /** - * Check if the resource already exists on the target destination. + * Checks whether the resource already exists in the target location. * * @return bool */ public function exists() { - // Check if any of the authentication views already exists. + // Check if any of the authentication views is published. We need to + // check that at least one of the target files exists and the + // replacement content is present. foreach ($this->source as $file => $content) { $target = $this->target.DIRECTORY_SEPARATOR.$file; - if (is_file($target)) { + if ($this->authViewExists($target, $content)) { return true; } } @@ -100,7 +104,8 @@ public function exists() } /** - * Check if the resource is correctly installed. + * Checks whether the resource is correctly installed, i.e. if the source + * items matches with the items available at the target location. * * @return bool */ @@ -118,7 +123,20 @@ public function installed() } /** - * Check if an authentication view is correctly installed. + * Checks whether an authentication view exists. + * + * @param string $path Absolute path of the authentication view + * @param string $content The expected content of the view + * @return bool + */ + protected function authViewExists($path, $content) + { + return File::isFile($path) + && strpos(File::get($path), $content) !== false; + } + + /** + * Checks whether an authentication view is correctly installed. * * @param string $path Absolute path of the authentication view * @param string $content The expected content of the view @@ -126,6 +144,6 @@ public function installed() */ protected function authViewInstalled($path, $content) { - return is_file($path) && (file_get_contents($path) === $content); + return File::isFile($path) && File::get($path) === $content; } } diff --git a/src/Console/PackageResources/BasicRoutesResource.php b/src/Console/PackageResources/BasicRoutesResource.php deleted file mode 100644 index b6b37762..00000000 --- a/src/Console/PackageResources/BasicRoutesResource.php +++ /dev/null @@ -1,105 +0,0 @@ -description = 'The package routes'; - $this->source = CommandHelper::getStubPath('routes.stub'); - $this->target = base_path('routes/web.php'); - $this->required = false; - - // Fill the installation messages. - - $this->messages = [ - 'install' => 'Install the basic package routes?', - 'overwrite' => 'Basic routes are already installed. Want to install they again?', - 'success' => 'Basic routes installed successfully.', - ]; - } - - /** - * Install/Export the resource. - * - * @return void - */ - public function install() - { - // If routes already exists, there is no need to install again. - - if ($this->exists()) { - return; - } - - // Get the routes to install. - - $routes = file_get_contents($this->source); - - // Add the routes. - - File::ensureDirectoryExists(dirname($this->target)); - file_put_contents($this->target, $routes, FILE_APPEND); - } - - /** - * Uninstall/Remove the resource. - * - * @return void - */ - public function uninstall() - { - $routes = file_get_contents($this->source); - - // If the target routes file exists, remove the package routes. - - if (is_file($this->target)) { - $targetContent = file_get_contents($this->target); - $targetContent = str_replace($routes, '', $targetContent); - file_put_contents($this->target, $targetContent); - } - } - - /** - * Check if the resource already exists on the target destination. - * - * @return bool - */ - public function exists() - { - $routes = file_get_contents($this->source); - - // First, check if the target routes file exists. - - if (! is_file($this->target)) { - return false; - } - - // Now, check if the target file already contains the routes. - - $targetContent = file_get_contents($this->target); - - return strpos($targetContent, $routes) !== false; - } - - /** - * Check if the resource is correctly installed. - * - * @return bool - */ - public function installed() - { - return $this->exists(); - } -} diff --git a/src/Console/PackageResources/BasicViewsResource.php b/src/Console/PackageResources/BasicViewsResource.php deleted file mode 100644 index b19edef5..00000000 --- a/src/Console/PackageResources/BasicViewsResource.php +++ /dev/null @@ -1,127 +0,0 @@ - 'home.stub', - ]; - - /** - * Create a new resource instance. - * - * @return void - */ - public function __construct() - { - // Fill the resource data. - - $this->description = 'The default package basic views'; - $this->source = $this->basicViewsContent; - $this->target = CommandHelper::getViewPath(); - $this->required = false; - - // Fill the set of installation messages. - - $this->messages = [ - 'install' => 'Install the AdminLTE basic views?', - 'overwrite' => 'The basic views already exists. Want to replace the views?', - 'success' => 'Basic views installed successfully.', - ]; - } - - /** - * Install/Export the resource. - * - * @return void - */ - public function install() - { - // Install the basic views. We going to replace the content of any - // existing basic view. - - foreach ($this->source as $file => $stub) { - $target = $this->target.DIRECTORY_SEPARATOR.$file; - File::ensureDirectoryExists(dirname($target)); - copy(CommandHelper::getStubPath($stub), $target); - } - } - - /** - * Uninstall/Remove the resource. - * - * @return void - */ - public function uninstall() - { - // Remove the package basic views. - - foreach ($this->source as $file => $tub) { - $target = $this->target.DIRECTORY_SEPARATOR.$file; - - if (is_file($target)) { - unlink($target); - } - } - } - - /** - * Check if the resource already exists on the target destination. - * - * @return bool - */ - public function exists() - { - // Check if any of the basic views already exists. - - foreach ($this->source as $file => $stub) { - $target = $this->target.DIRECTORY_SEPARATOR.$file; - - if (is_file($target)) { - return true; - } - } - - return false; - } - - /** - * Check if the resource is correctly installed. - * - * @return bool - */ - public function installed() - { - foreach ($this->source as $file => $stub) { - $target = $this->target.DIRECTORY_SEPARATOR.$file; - $content = file_get_contents(CommandHelper::getStubPath($stub)); - - if (! $this->basicViewInstalled($target, $content)) { - return false; - } - } - - return true; - } - - /** - * Check if a basic view is correctly installed. - * - * @param string $path Absolute path of the view - * @param string $content The expected content of the view - * @return bool - */ - protected function basicViewInstalled($path, $content) - { - return is_file($path) && (file_get_contents($path) === $content); - } -} diff --git a/src/Console/PackageResources/ConfigResource.php b/src/Console/PackageResources/ConfigResource.php index 7057d0a7..e9ed6220 100644 --- a/src/Console/PackageResources/ConfigResource.php +++ b/src/Console/PackageResources/ConfigResource.php @@ -8,7 +8,7 @@ class ConfigResource extends PackageResource { /** - * Create a new resource instance. + * Create a new package resource instance. * * @return void */ @@ -16,7 +16,7 @@ public function __construct() { // Fill the resource data. - $this->description = 'The default package configuration file'; + $this->description = 'The package configuration file with default options'; $this->source = CommandHelper::getPackagePath('config/adminlte.php'); $this->target = config_path('adminlte.php'); $this->required = true; @@ -24,51 +24,53 @@ public function __construct() // Fill the set of installation messages. $this->messages = [ - 'install' => 'Install the package config file?', - 'overwrite' => 'The config file already exists. Want to replace it?', - 'success' => 'Configuration file installed successfully.', + 'install' => 'Do you want to publish the package config file?', + 'overwrite' => 'Config file was already published. Want to replace it?', + 'success' => 'Configuration file published successfully', ]; } /** - * Install/Export the resource. + * Installs or publishes the resource. * * @return void */ public function install() { - // Install the configuration file. + // Copy the configuration file to the target file. - File::ensureDirectoryExists(dirname($this->target)); - copy($this->source, $this->target); + File::ensureDirectoryExists(File::dirname($this->target)); + File::copy($this->source, $this->target); } /** - * Uninstall/Remove the resource. + * Uninstalls the resource. * * @return void */ public function uninstall() { - // Uninstall the configuration file. + // Delete the published configuration file. When file does not exists, + // we consider the config file as uninstalled. - if (is_file($this->target)) { - unlink($this->target); + if (File::isFile($this->target)) { + File::delete($this->target); } } /** - * Check if the resource already exists on the target destination. + * Checks whether the resource already exists in the target location. * * @return bool */ public function exists() { - return is_file($this->target); + return File::isFile($this->target); } /** - * Check if the resource is correctly installed. + * Checks whether the resource is correctly installed, i.e. if the source + * items matches with the items available at the target location. * * @return bool */ diff --git a/src/Console/PackageResources/LayoutViewsResource.php b/src/Console/PackageResources/LayoutViewsResource.php new file mode 100644 index 00000000..c5a6e7ef --- /dev/null +++ b/src/Console/PackageResources/LayoutViewsResource.php @@ -0,0 +1,170 @@ +description = 'The set of views that defines the AdminLTE layout'; + $this->target = CommandHelper::getViewPath('vendor/adminlte'); + $this->required = false; + + // Note we declare the source as an array of files and folders, we do + // this way o avoid copying the components views located at the + // 'resources/views/components' folder. + + $this->source = [ + CommandHelper::getPackagePath('resources/views/auth'), + CommandHelper::getPackagePath('resources/views/master.blade.php'), + CommandHelper::getPackagePath('resources/views/page.blade.php'), + CommandHelper::getPackagePath('resources/views/partials'), + CommandHelper::getPackagePath('resources/views/plugins.blade.php'), + ]; + + // Fill the set of installation messages. + + $this->messages = [ + 'install' => 'Do you want to publish the AdminLTE layout views?', + 'overwrite' => 'The layout views were already published. Want to replace?', + 'success' => 'AdminLTE layout views published successfully', + ]; + } + + /** + * Installs or publishes the resource. + * + * @return void + */ + public function install() + { + // Publish the package layout views. + + foreach ($this->source as $src) { + $tgt = $this->target.DIRECTORY_SEPARATOR.File::basename($src); + $this->publishResource($src, $tgt); + } + } + + /** + * Uninstalls the resource. + * + * @return void + */ + public function uninstall() + { + // Uninstall the package layout views. + + foreach ($this->source as $src) { + $tgt = $this->target.DIRECTORY_SEPARATOR.File::basename($src); + $this->uninstallResource($tgt); + } + } + + /** + * Checks whether the resource already exists in the target location. + * + * @return bool + */ + public function exists() + { + foreach ($this->source as $src) { + $tgt = $this->target.DIRECTORY_SEPARATOR.File::basename($src); + + if (File::exists($tgt)) { + return true; + } + } + + return false; + } + + /** + * Checks whether the resource is correctly installed, i.e. if the source + * items matches with the items available at the target location. + * + * @return bool + */ + public function installed() + { + foreach ($this->source as $src) { + $tgt = $this->target.DIRECTORY_SEPARATOR.File::basename($src); + + if (! $this->resourceInstalled($src, $tgt)) { + return false; + } + } + + return true; + } + + /** + * Publishes the specified source (usually a file or folder) at the + * specified target location. + * + * @param string $source The source path + * @param string $target The target path + * @return void + */ + protected function publishResource($source, $target) + { + // Check whether the resource is a file or a directory. + + if (File::isDirectory($source)) { + CommandHelper::copyDirectory($source, $target, true, true); + } else { + File::copy($source, $target); + } + } + + /** + * Uninstalls the resource at the specified target location. + * + * @param string $target The target path + * @return void + */ + protected function uninstallResource($target) + { + // When the target does not exists, we consider the resource as + // unistalled. + + if (! File::exists($target)) { + return; + } + + // Uninstall the resource at the specified target location. + + if (File::isDirectory($target)) { + File::deleteDirectory($target); + } else { + File::delete($target); + } + } + + /** + * Checks whether a resource is correctly installed at the specified target + * location. + * + * @param string $source The source path + * @param string $target The target path + * @return bool + */ + protected function resourceInstalled($source, $target) + { + // Check whether the resource is a file or a directory. + + return File::isDirectory($source) + ? (bool) CommandHelper::compareDirectories($source, $target, true) + : CommandHelper::compareFiles($source, $target); + } +} diff --git a/src/Console/PackageResources/MainViewsResource.php b/src/Console/PackageResources/MainViewsResource.php deleted file mode 100644 index a1282fb3..00000000 --- a/src/Console/PackageResources/MainViewsResource.php +++ /dev/null @@ -1,82 +0,0 @@ -description = 'The default package main views'; - $this->source = CommandHelper::getPackagePath('resources/views'); - $this->target = CommandHelper::getViewPath('vendor/adminlte'); - $this->required = false; - - // Fill the set of installation messages. - - $this->messages = [ - 'install' => 'Install the AdminLTE main views?', - 'overwrite' => 'The main views already exists. Want to replace the views?', - 'success' => 'Main views installed successfully.', - ]; - } - - /** - * Install/Export the resource. - * - * @return void - */ - public function install() - { - // Install the main views. - - CommandHelper::copyDirectory($this->source, $this->target, true, true); - } - - /** - * Uninstall/Remove the resource. - * - * @return void - */ - public function uninstall() - { - // Uninstall the package main views. - - if (is_dir($this->target)) { - File::deleteDirectory($this->target); - } - } - - /** - * Check if the resource already exists on the target destination. - * - * @return bool - */ - public function exists() - { - return is_dir($this->target); - } - - /** - * Check if the resource is correctly installed. - * - * @return bool - */ - public function installed() - { - return (bool) CommandHelper::compareDirectories( - $this->source, - $this->target, - true - ); - } -} diff --git a/src/Console/PackageResources/PackageResource.php b/src/Console/PackageResources/PackageResource.php index f403af82..267cdde0 100644 --- a/src/Console/PackageResources/PackageResource.php +++ b/src/Console/PackageResources/PackageResource.php @@ -5,15 +5,15 @@ abstract class PackageResource { /** - * The package resource description. A litle summary of what this - * resource contains. + * The package resource description. A little summary of what this resource + * contains. * * @var string */ public $description; /** - * The resource source. Usually the source will be a set of paths to files + * The source items of this resource. Usually a set of paths to files * and/or folders. * * @var mixed @@ -21,66 +21,67 @@ abstract class PackageResource protected $source; /** - * The resource target. The destination of the resource, usually a root - * folder or file. + * The install location of this resource. Usually a target folder or file. * * @var mixed */ public $target; /** - * Whether this resource is required for the package in order to work fine. + * Whether this resource is required for the package to work fine. * * @var bool */ public $required; /** - * The set of installation messages for this resource. Usually, the array - * should contains keys for 'install', 'overwrite' and 'success' messages. + * A set of messages that will be used during the resource installation. + * Usually, the array should contains keys for 'install', 'overwrite' and + * 'success' messages. * * @var array */ protected $messages; /** - * Install or export the resource. + * Installs or publishes the resource. * * @return void */ abstract public function install(); /** - * Uninstall or remove the resource. + * Uninstalls the resource. * * @return void */ abstract public function uninstall(); /** - * Check if the resource already exists on the target destination. + * Checks whether the resource already exists in the target location. * * @return bool */ abstract public function exists(); /** - * Check if the resource is correctly installed. + * Checks whether the resource is correctly installed, i.e. if the source + * items matches with the items available at the target location. * * @return bool */ abstract public function installed(); /** - * Get an installation message. + * Gets an installation message. * * @param string $key The message keyword - * @return string + * @return string|null */ public function getInstallMessage($key) { if (! isset($this->messages[$key])) { - return ''; + return null; } return $this->messages[$key]; diff --git a/src/Console/PackageResources/PluginsResource.php b/src/Console/PackageResources/PluginsResource.php index cfad8689..c7cb41f9 100644 --- a/src/Console/PackageResources/PluginsResource.php +++ b/src/Console/PackageResources/PluginsResource.php @@ -206,7 +206,7 @@ public function __construct() { // Fill the basic resource data. - $this->description = 'The set of AdminLTE additional plugins'; + $this->description = 'The set of extra plugins available with AdminLTE'; $this->required = false; // Define the base source folder of the plugins. @@ -220,8 +220,8 @@ public function __construct() // Fill the set of installation messages templates. $this->messages = [ - 'install' => 'Install the AdminLTE :plugin plugin?', - 'overwrite' => 'The :plugin plugin already exists. Want to replace the plugin?', + 'install' => 'Do you want to plublish the :plugin plugin?', + 'overwrite' => 'The :plugin plugin was already published. Want to replace?', 'remove' => 'Do you really want to remove the :plugin plugin?', ]; } @@ -229,15 +229,15 @@ public function __construct() /** * Gets the plugins source data. * - * @param string $pluginKey A plugin string key + * @param string $pluginKey A plugin key * @return array */ public function getSourceData($pluginKey = null) { // Check if we need to get data of a specific AdminLTE plugin. - if (isset($pluginKey)) { - return $this->plugins[$pluginKey] ?? null; + if (! empty($pluginKey)) { + return $this->plugins[$pluginKey] ?? []; } // Otherwise, return all the AdminLTE plugins data. @@ -246,9 +246,9 @@ public function getSourceData($pluginKey = null) } /** - * Install/Export a plugin. + * Installs or publishes the specified plugin. * - * @param string $pluginKey A plugin string key + * @param string $pluginKey A plugin key * @return void */ public function install($pluginKey = null) @@ -260,9 +260,9 @@ public function install($pluginKey = null) } /** - * Uninstall/Remove a plugin. + * Uninstalls the specified plugin. * - * @param string $pluginKey A plugin string key + * @param string $pluginKey A plugin key * @return void */ public function uninstall($pluginKey = null) @@ -274,9 +274,9 @@ public function uninstall($pluginKey = null) } /** - * Check if a plugin already exists on the target destination. + * Checks whether a plugin already exists in the target location. * - * @param string $pluginKey A plugin string key + * @param string $pluginKey A plugin key * @return bool */ public function exists($pluginKey = null) @@ -291,9 +291,10 @@ public function exists($pluginKey = null) } /** - * Check if a plugin is correctly installed. + * Checks whether a plugin is correctly installed, i.e. if the source items + * matches with the items available at the target location. * - * @param string $pluginKey A plugin string key + * @param string $pluginKey A plugin key * @return bool */ public function installed($pluginKey = null) @@ -308,10 +309,10 @@ public function installed($pluginKey = null) } /** - * Prepare a plugin with some sort of normalizations. + * Prepares a plugin with some sort of normalizations in its data. * * @param array $plugin An array with the plugin data - * @return array An array with normalized plugin data + * @return array */ protected function preparePlugin($plugin) { @@ -347,7 +348,7 @@ protected function preparePlugin($plugin) } /** - * Install the specified AdminLTE plugin. + * Installs the specified AdminLTE plugin. * * @param array $plugin An array with the plugin data * @return void @@ -357,29 +358,29 @@ protected function installPlugin($plugin) // Check if we need to export the entire plugin. if (! isset($plugin['resources'])) { - $this->exportResource($plugin); + $this->publishResource($plugin); return; } - // Otherwise, export only the specified plugin resources. + // Otherwise, publish only the specified plugin resources. foreach ($plugin['resources'] as $res) { - $this->exportResource($res); + $this->publishResource($res); } } /** - * Exports the specified resource (usually a folder). + * Publishes the specified resource (usually a file or folder). * * @param array $res An array with the resource data * @return void */ - protected function exportResource($res) + protected function publishResource($res) { - // Check the resource source type. + // Check whether the resource is a file or a directory. - if (is_dir($res['source'])) { + if (File::isDirectory($res['source'])) { CommandHelper::copyDirectory( $res['source'], $res['target'], @@ -387,27 +388,31 @@ protected function exportResource($res) $res['recursive'] ?? true, $res['ignore'] ?? [] ); + } else { + File::ensureDirectoryExists(File::dirname($res['target'])); + File::copy($res['source'], $res['target']); } } /** - * Check if the specified plugin already exists on the target destination. + * Checks whether the specified plugin already exists in the target + * location. * * @param array $plugin An array with the plugin data * @return bool */ protected function pluginExists($plugin) { - // When the plugin is not a resources list, check if target exists. + // When the plugin is not a resources list, just check if target exists. if (! isset($plugin['resources'])) { - return file_exists($plugin['target']); + return File::exists($plugin['target']); } - // Otherwise, check if any of the resources already exists. + // Otherwise, check if any of the plugin resources already exists. foreach ($plugin['resources'] as $res) { - if (file_exists($res['target'])) { + if (File::exists($res['target'])) { return true; } } @@ -416,21 +421,19 @@ protected function pluginExists($plugin) } /** - * Check if the specified plugin is correctly installed. + * Checks whether the specified plugin is correctly installed. * * @param array $plugin An array with the plugin data * @return bool */ protected function pluginInstalled($plugin) { - // When the plugin is not a resources list, check if installed. + // Check whether the plugin has resources or not. if (! isset($plugin['resources'])) { return $this->resourceInstalled($plugin); } - // Otherwise, check if all the resources are installed. - foreach ($plugin['resources'] as $res) { if (! $this->resourceInstalled($res)) { return false; @@ -441,17 +444,17 @@ protected function pluginInstalled($plugin) } /** - * Check if the specified resource is correctly installed. + * Checks whether the specified resource is correctly installed. * * @param array $res An array with the resource data * @return bool */ protected function resourceInstalled($res) { - $installed = false; + // Check whether the resource is a file or a directory. - if (is_dir($res['source'])) { - $installed = (bool) CommandHelper::compareDirectories( + if (File::isDirectory($res['source'])) { + return (bool) CommandHelper::compareDirectories( $res['source'], $res['target'], $res['recursive'] ?? true, @@ -459,18 +462,19 @@ protected function resourceInstalled($res) ); } - return $installed; + return CommandHelper::compareFiles($res['source'], $res['target']); } /** - * Uninstall or remove the specified plugin. + * Uninstalls the specified plugin. * * @param array $plugin An array with the plugin data * @return void */ protected function uninstallPlugin($plugin) { - // Check if we need to remove the entire plugin. + // If the plugin doensn't have resources, remove the main target + // location folder. if (! isset($plugin['resources'])) { $this->uninstallResource($plugin); @@ -495,7 +499,11 @@ protected function uninstallResource($res) { $target = $res['target']; - if (is_dir($target)) { + // Uninstall the specified resource. Note the target location is always + // a folder. When the target folder does not exists, we consider the + // resource as uninstalled. + + if (File::isDirectory($target)) { File::deleteDirectory($target); } } diff --git a/src/Console/PackageResources/TranslationsResource.php b/src/Console/PackageResources/TranslationsResource.php index ac2d5a2f..121ab51f 100644 --- a/src/Console/PackageResources/TranslationsResource.php +++ b/src/Console/PackageResources/TranslationsResource.php @@ -9,7 +9,7 @@ class TranslationsResource extends PackageResource { /** - * Create a new resource instance. + * Create a new package resource instance. * * @return void */ @@ -25,50 +25,52 @@ public function __construct() // Fill the set of installation messages. $this->messages = [ - 'install' => 'Install the package translations files?', - 'overwrite' => 'The translation files already exists. Want to replace the files?', - 'success' => 'Translation files installed successfully.', + 'install' => 'Do you want to publish the package translations?', + 'overwrite' => 'Translations were already published. Want to replace?', + 'success' => 'Translation files published successfully', ]; } /** - * Install/Export the resource. + * Installs or publishes the resource. * * @return void */ public function install() { - // Install the translations files. + // Copy the translation files to the target folder. CommandHelper::copyDirectory($this->source, $this->target, true, true); } /** - * Uninstall/Remove the resource. + * Uninstalls the resource. * * @return void */ public function uninstall() { - // Uninstall the translation files. + // Remove the translation files from the target folder. When + // translations does not exists, we consider they as uninstalled. - if (is_dir($this->target)) { + if (File::isDirectory($this->target)) { File::deleteDirectory($this->target); } } /** - * Check if the resource already exists on the target destination. + * Checks whether the resource already exists in the target location. * * @return bool */ public function exists() { - return is_dir($this->target); + return File::isDirectory($this->target); } /** - * Check if the resource is correctly installed. + * Checks whether the resource is correctly installed, i.e. if the source + * items matches with the items available at the target location. * * @return bool */ diff --git a/src/Console/stubs/home.stub b/src/Console/stubs/home.stub deleted file mode 100644 index 91ff6a22..00000000 --- a/src/Console/stubs/home.stub +++ /dev/null @@ -1,19 +0,0 @@ -@extends('adminlte::page') - -@section('title', 'AdminLTE') - -@section('content_header') -

Dashboard

-@stop - -@section('content') -
-
-
-
-

You are logged in!

-
-
-
-
-@stop diff --git a/src/Console/stubs/routes.stub b/src/Console/stubs/routes.stub index cbb2ff2b..2e7a1338 100644 --- a/src/Console/stubs/routes.stub +++ b/src/Console/stubs/routes.stub @@ -1,6 +1,2 @@ Auth::routes(); - -Route::get('/home', function() { - return view('home'); -})->name('home')->middleware('auth'); diff --git a/tests/Console/CommandTestCase.php b/tests/Console/CommandTestCase.php index a0ace1cb..2e18b7da 100644 --- a/tests/Console/CommandTestCase.php +++ b/tests/Console/CommandTestCase.php @@ -1,12 +1,11 @@ resources = [ + 'assets' => new AdminlteAssetsResource(), + 'config' => new ConfigResource(), + 'translations' => new TranslationsResource(), + 'main_views' => new LayoutViewsResource(), + 'auth_views' => new AuthViewsResource(), + 'basic_routes' => new AuthRoutesResource(), + ]; + } + + /** + * Gets the array of resources or a specific package resource. * - * @param string $name Name of the resource. + * @param string $name Name of the specific resource to get * @return array */ protected function getResources($name = null) { - if (! isset($this->resources)) { - $this->resources = [ - 'assets' => new AssetsResource(), - 'config' => new ConfigResource(), - 'translations' => new TranslationsResource(), - 'main_views' => new MainViewsResource(), - 'auth_views' => new AuthViewsResource(), - 'basic_views' => new BasicViewsResource(), - 'basic_routes' => new BasicRoutesResource(), - ]; - } - return $name ? $this->resources[$name] : $this->resources; } /** - * Create dummy files for a particular resource. + * Creates a set of dummy files for the specified resource. * - * @param string $resName - * @param PackageResource $res + * @param string $name The name of the resource + * @param PackageResource $resource The package resource instance * @return void */ - protected function createDummyResource($resName, $res) + protected function createDummyResource($name, $resource) { - // Uninstall the resource. + // Uninstall the package resource. We need a clean target location + // before creating dummy files. - $res->uninstall(); + $resource->uninstall(); - // Create a dummy resource on the target destination. This will fire - // an overwrite warning when trying to install the resource later. + // Create dummy files on the target location. This way, an overwrite + // warning will be fired when trying to install the resource later. - $target = $res->target; + $target = $resource->target; - if ($resName === 'assets') { + if ($name === 'assets') { $target = $target.DIRECTORY_SEPARATOR.'adminlte'; File::EnsureDirectoryExists($target); - } elseif ($resName === 'config') { + } elseif ($name === 'config') { $this->createDummyFile($target); - } elseif ($resName === 'translations') { + } elseif ($name === 'translations') { $target = $target.DIRECTORY_SEPARATOR.'en/adminlte.php'; $this->createDummyFile($target); - } elseif ($resName === 'main_views') { + } elseif ($name === 'main_views') { $target = $target.DIRECTORY_SEPARATOR.'master.blade.php'; $this->createDummyFile($target); - } elseif ($resName === 'auth_views') { + } elseif ($name === 'auth_views') { $target = $target.DIRECTORY_SEPARATOR.'login.blade.php'; - $this->createDummyFile($target); - } elseif ($resName === 'basic_views') { - $target = $target.DIRECTORY_SEPARATOR.'home.blade.php'; - $this->createDummyFile($target); - } elseif ($resName === 'basic_routes') { + $loginContent = '@extends(\'adminlte::auth.login\')'; + $this->createDummyFile($target, $loginContent); + } elseif ($name === 'basic_routes') { $stubFile = CommandHelper::getStubPath('routes.stub'); - $content = file_get_contents($stubFile); + $content = File::get($stubFile); $this->createDummyFile($target, $content); } } /** - * Create a dummy file with some content. + * Creates a dummy file with some content at the specified path. * - * @param string $filePath - * @param string $content + * @param string $filePath The file path + * @param string $content The file content * @return void */ protected function createDummyFile($filePath, $content = null) { $content = $content ?? 'dummy-content'; - File::ensureDirectoryExists(dirname($filePath)); - file_put_contents($filePath, $content); + File::ensureDirectoryExists(File::dirname($filePath)); + File::put($filePath, $content); } /** - * Install the required vendor asset "vendor/almasaeed2010" into the - * laravel testing project. + * Installs the required AdminLTE assets files ("vendor/almasaeed2010") + * into the laravel testing project. * * @return void */ @@ -112,19 +117,32 @@ protected function installVendorAssets() // Check if vendor assets are already installed. - if (is_link($target)) { + if (File::exists($target)) { return; } - // Check if vendor folder exists on the laravel testing project. If + // Ensure the vendor folder exists on the laravel testing project. If // vendor folder do not exists, create it. - if (! is_dir(base_path('vendor'))) { - mkdir(base_path('vendor')); - } + File::ensureDirectoryExists(base_path('vendor')); // Create a symbolic link to the required vendor assets. - symlink($resource, $target); + File::link($resource, $target); + } + + /** + * Returns whether the expectsConfirmation() method is supported by the + * underlying Laravel framework. + * + * @return bool + */ + protected function canExpectsConfirmation() + { + return class_exists('Illuminate\Testing\PendingCommand') + && method_exists( + 'Illuminate\Testing\PendingCommand', + 'expectsConfirmation' + ); } } diff --git a/tests/Console/InstallTest.php b/tests/Console/InstallTest.php index 02a2a38d..defc0612 100644 --- a/tests/Console/InstallTest.php +++ b/tests/Console/InstallTest.php @@ -56,17 +56,12 @@ public function testInstallOnly() } } - public function testInstallOnlyInteractive() + public function testInstallOnlyWithInteractiveFlag() { - // We can't do these test on old laravel versions. + // We can't perfom these tests on old Laravel versions. We need support + // for the expect confirmation method. - if (! class_exists('Illuminate\Testing\PendingCommand')) { - $this->assertTrue(true); - - return; - } - - if (! method_exists('Illuminate\Testing\PendingCommand', 'expectsConfirmation')) { + if (! $this->canExpectsConfirmation()) { $this->assertTrue(true); return; @@ -87,14 +82,14 @@ public function testInstallOnlyInteractive() $res->uninstall(); - // Test with --interactive option (response with no). + // Test with --interactive option and respond with NO. $this->artisan("adminlte:install --only={$name} --interactive") ->expectsConfirmation($confirmMsg, 'no'); $this->assertFalse($res->installed()); - // Test with --interactive option (response with yes). + // Test with --interactive option and respond with YES. $this->artisan("adminlte:install --only={$name} --interactive") ->expectsConfirmation($confirmMsg, 'yes'); @@ -108,23 +103,18 @@ public function testInstallOnlyInteractive() } } - public function testInstallOnlyOverwrite() + public function testInstallOnlyWithOverwriteWarning() { - // We can't do these test on old laravel versions. - - if (! class_exists('Illuminate\Testing\PendingCommand')) { - $this->assertTrue(true); - - return; - } + // We can't perfom these tests on old Laravel versions. We need support + // for the expect confirmation method. - if (! method_exists('Illuminate\Testing\PendingCommand', 'expectsConfirmation')) { + if (! $this->canExpectsConfirmation()) { $this->assertTrue(true); return; } - // Test installation of the resources when an overwrite will occurs. + // Test installation of the resources when an overwrite event occurs. foreach ($this->getResources() as $name => $res) { $confirmMsg = $res->getInstallMessage('overwrite'); @@ -139,7 +129,7 @@ public function testInstallOnlyOverwrite() $this->createDummyResource($name, $res); - // Test when not confirm the overwrite. + // Test when overwrite is not confirmed. $this->artisan("adminlte:install --only={$name}") ->expectsConfirmation($confirmMsg, 'no'); @@ -150,14 +140,14 @@ public function testInstallOnlyOverwrite() $this->assertFalse($res->installed()); } - // Test when confirm the overwrite. + // Test when overwrite is confirmed. $this->artisan("adminlte:install --only={$name}") ->expectsConfirmation($confirmMsg, 'yes'); $this->assertTrue($res->installed()); - // Test when using --force. + // Test when using --force flag. $this->createDummyResource($name, $res); $this->artisan("adminlte:install --only={$name} --force"); @@ -170,7 +160,7 @@ public function testInstallOnlyOverwrite() } } - public function testInstallOnlyMultipleResources() + public function testInstallOnlyWithMultipleResources() { $resources = [ $this->getResources('auth_views'), @@ -285,7 +275,6 @@ public function testInstallWithTypeFull() $this->getResources('config'), $this->getResources('translations'), $this->getResources('auth_views'), - $this->getResources('basic_views'), $this->getResources('basic_routes'), ]; @@ -330,7 +319,7 @@ public function testInstallWithAdditionalResource() $this->getResources('translations'), ]; - $newRes = ['main_views', 'auth_views', 'basic_views', 'basic_routes']; + $newRes = ['main_views', 'auth_views', 'basic_routes']; // Ensure the required vendor assets exists. diff --git a/tests/Console/PluginsTest.php b/tests/Console/PluginsTest.php index 5b3bbb9c..edf86fc5 100644 --- a/tests/Console/PluginsTest.php +++ b/tests/Console/PluginsTest.php @@ -31,7 +31,7 @@ public function testWithInvalidOperation() |-------------------------------------------------------------------------- */ - public function testInstallAndUninstallAll() + public function testInstallAndUninstallAllPlugins() { $plugins = new PluginsResource(); $pluginsKeys = array_keys($plugins->getSourceData()); @@ -63,7 +63,7 @@ public function testInstallAndUninstallAll() } } - public function testInstallAndUninstallSpecific() + public function testInstallAndUninstallSpecificPlugins() { $plugins = new PluginsResource(); $pluginsKeys = ['datatables', 'icheckBootstrap']; @@ -97,7 +97,7 @@ public function testInstallAndUninstallSpecific() } } - public function testInstallAndUninstallSpecificInteractive() + public function testInstallAndUninstallSpecificPLuginInteractively() { $plugins = new PluginsResource(); $pluginKey = 'icheckBootstrap'; @@ -110,15 +110,10 @@ public function testInstallAndUninstallSpecificInteractive() [':plugin' => $pluginKey] ); - // We can't do these test on old laravel versions. + // We can't perfom these tests on old Laravel versions. We need support + // for the expect confirmation method. - if (! class_exists('Illuminate\Testing\PendingCommand')) { - $this->assertTrue(true); - - return; - } - - if (! method_exists('Illuminate\Testing\PendingCommand', 'expectsConfirmation')) { + if (! $this->canExpectsConfirmation()) { $this->assertTrue(true); return; @@ -157,7 +152,7 @@ public function testInstallAndUninstallSpecificInteractive() $this->assertFalse($plugins->installed($pluginKey)); } - public function testInstallAndUninstallSpecificOverwrite() + public function testInstallAndUninstallSpecificPluginWithOverwrite() { $plugins = new PluginsResource(); $pluginKey = 'icheckBootstrap'; @@ -166,15 +161,10 @@ public function testInstallAndUninstallSpecificOverwrite() [':plugin' => $pluginKey] ); - // We can't do these test on old laravel versions. - - if (! class_exists('Illuminate\Testing\PendingCommand')) { - $this->assertTrue(true); - - return; - } + // We can't perfom these tests on old Laravel versions. We need support + // for the expect confirmation method. - if (! method_exists('Illuminate\Testing\PendingCommand', 'expectsConfirmation')) { + if (! $this->canExpectsConfirmation()) { $this->assertTrue(true); return; @@ -209,7 +199,7 @@ public function testInstallAndUninstallSpecificOverwrite() |-------------------------------------------------------------------------- */ - public function testAllPluginStatus() + public function testGetAllPluginStatus() { $plugins = new PluginsResource(); diff --git a/tests/Console/UpdateTest.php b/tests/Console/UpdateTest.php index b042d8c3..7597b41a 100644 --- a/tests/Console/UpdateTest.php +++ b/tests/Console/UpdateTest.php @@ -61,7 +61,7 @@ public function testUpdateShowsMainViewsWarning() Artisan::call('adminlte:update'); $this->assertStringContainsString( - 'Outdated main views', + 'Outdated layout views', Artisan::output() );