diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a91953b..e005ac7 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -6,7 +6,6 @@ parameters: paths: - src - config - - database tmpDir: build/phpstan checkOctaneCompatibility: true checkModelProperties: true diff --git a/src/Abstracts/Actions/Action.php b/src/Abstracts/Actions/Action.php index 094fadb..ac0f6ef 100644 --- a/src/Abstracts/Actions/Action.php +++ b/src/Abstracts/Actions/Action.php @@ -4,5 +4,4 @@ abstract class Action { - } diff --git a/src/Abstracts/Actions/SubAction.php b/src/Abstracts/Actions/SubAction.php index b99036a..c847075 100644 --- a/src/Abstracts/Actions/SubAction.php +++ b/src/Abstracts/Actions/SubAction.php @@ -4,5 +4,4 @@ abstract class SubAction extends Action { - } diff --git a/src/Abstracts/Commands/ConsoleCommand.php b/src/Abstracts/Commands/ConsoleCommand.php index 4da821d..c963b5d 100644 --- a/src/Abstracts/Commands/ConsoleCommand.php +++ b/src/Abstracts/Commands/ConsoleCommand.php @@ -6,5 +6,4 @@ abstract class ConsoleCommand extends LaravelCommand { - } diff --git a/src/Abstracts/Controllers/ApiController.php b/src/Abstracts/Controllers/ApiController.php index f7b60de..0e610eb 100644 --- a/src/Abstracts/Controllers/ApiController.php +++ b/src/Abstracts/Controllers/ApiController.php @@ -4,5 +4,4 @@ abstract class ApiController extends Controller { - } diff --git a/src/Abstracts/Controllers/WebController.php b/src/Abstracts/Controllers/WebController.php index b451bad..2c15a2d 100644 --- a/src/Abstracts/Controllers/WebController.php +++ b/src/Abstracts/Controllers/WebController.php @@ -4,5 +4,4 @@ abstract class WebController extends Controller { - } diff --git a/src/Abstracts/Exceptions/Exception.php b/src/Abstracts/Exceptions/Exception.php index cf1ab03..f564e62 100644 --- a/src/Abstracts/Exceptions/Exception.php +++ b/src/Abstracts/Exceptions/Exception.php @@ -6,5 +6,4 @@ abstract class Exception extends LaravelException { - } diff --git a/src/Abstracts/Factories/Factory.php b/src/Abstracts/Factories/Factory.php index 80aee9a..59df9b5 100644 --- a/src/Abstracts/Factories/Factory.php +++ b/src/Abstracts/Factories/Factory.php @@ -6,5 +6,4 @@ abstract class Factory extends LaravelFactory { - } diff --git a/src/Abstracts/Listeners/Listener.php b/src/Abstracts/Listeners/Listener.php index 29aba52..da86067 100644 --- a/src/Abstracts/Listeners/Listener.php +++ b/src/Abstracts/Listeners/Listener.php @@ -4,5 +4,4 @@ abstract class Listener { - } diff --git a/src/Abstracts/Middlewares/Middleware.php b/src/Abstracts/Middlewares/Middleware.php index bffe528..dd89e11 100644 --- a/src/Abstracts/Middlewares/Middleware.php +++ b/src/Abstracts/Middlewares/Middleware.php @@ -4,5 +4,4 @@ abstract class Middleware { - } diff --git a/src/Abstracts/Notifications/Notification.php b/src/Abstracts/Notifications/Notification.php index 1783895..048231c 100644 --- a/src/Abstracts/Notifications/Notification.php +++ b/src/Abstracts/Notifications/Notification.php @@ -6,5 +6,4 @@ abstract class Notification extends LaravelNotification { - } diff --git a/src/Abstracts/Policies/Policy.php b/src/Abstracts/Policies/Policy.php index 8833629..1172817 100644 --- a/src/Abstracts/Policies/Policy.php +++ b/src/Abstracts/Policies/Policy.php @@ -4,5 +4,4 @@ abstract class Policy { - } diff --git a/src/Abstracts/Providers/AuthServiceProvider.php b/src/Abstracts/Providers/AuthServiceProvider.php index 71c7df6..c74e357 100644 --- a/src/Abstracts/Providers/AuthServiceProvider.php +++ b/src/Abstracts/Providers/AuthServiceProvider.php @@ -17,8 +17,6 @@ abstract class AuthServiceProvider extends LaravelAuthServiceProvider /** * Register any authentication / authorization services. - * - * @return void */ public function boot(): void { diff --git a/src/Abstracts/Providers/BroadcastServiceProvider.php b/src/Abstracts/Providers/BroadcastServiceProvider.php index 86151fd..96d0d11 100644 --- a/src/Abstracts/Providers/BroadcastServiceProvider.php +++ b/src/Abstracts/Providers/BroadcastServiceProvider.php @@ -6,5 +6,4 @@ abstract class BroadcastServiceProvider extends LaravelBroadcastServiceProvider { - } diff --git a/src/Abstracts/Providers/EventServiceProvider.php b/src/Abstracts/Providers/EventServiceProvider.php index f5647f5..cdcc7df 100644 --- a/src/Abstracts/Providers/EventServiceProvider.php +++ b/src/Abstracts/Providers/EventServiceProvider.php @@ -6,5 +6,4 @@ abstract class EventServiceProvider extends LaravelEventServiceProvider { - } diff --git a/src/Abstracts/Providers/MainServiceProvider.php b/src/Abstracts/Providers/MainServiceProvider.php index 52a8322..0b75bd3 100644 --- a/src/Abstracts/Providers/MainServiceProvider.php +++ b/src/Abstracts/Providers/MainServiceProvider.php @@ -13,8 +13,6 @@ abstract class MainServiceProvider extends LaravelAppServiceProvider /** * Register any application services. - * - * @return void */ public function register(): void { @@ -24,11 +22,8 @@ public function register(): void /** * Bootstrap any application services. - * - * @return void */ public function boot(): void { - } } diff --git a/src/Abstracts/Providers/MiddlewareServiceProvider.php b/src/Abstracts/Providers/MiddlewareServiceProvider.php index f073bf0..e7cf016 100644 --- a/src/Abstracts/Providers/MiddlewareServiceProvider.php +++ b/src/Abstracts/Providers/MiddlewareServiceProvider.php @@ -19,6 +19,7 @@ abstract class MiddlewareServiceProvider extends MainServiceProvider /** * Perform post-registration booting of services. + * * @throws BindingResolutionException */ public function boot(): void diff --git a/src/Abstracts/Providers/RouteServiceProvider.php b/src/Abstracts/Providers/RouteServiceProvider.php index b3bfafd..750c0e9 100644 --- a/src/Abstracts/Providers/RouteServiceProvider.php +++ b/src/Abstracts/Providers/RouteServiceProvider.php @@ -9,19 +9,8 @@ abstract class RouteServiceProvider extends LaravelRouteServiceProvider { use RoutesLoaderTrait; - /** - * The controller namespace for the application. - * - * When present, controller route declarations will automatically be prefixed with this namespace. - * - * @var string|null - */ - // protected $namespace = 'App\\Http\\Controllers'; - /** * Define your route model bindings, pattern filters, etc. - * - * @return void */ public function boot(): void { diff --git a/src/Abstracts/Requests/Request.php b/src/Abstracts/Requests/Request.php index 9f2c863..1ed5a2b 100644 --- a/src/Abstracts/Requests/Request.php +++ b/src/Abstracts/Requests/Request.php @@ -6,5 +6,4 @@ abstract class Request extends LaravelRequest { - } diff --git a/src/Abstracts/Seeders/Seeder.php b/src/Abstracts/Seeders/Seeder.php index b3df55a..06b8c45 100644 --- a/src/Abstracts/Seeders/Seeder.php +++ b/src/Abstracts/Seeders/Seeder.php @@ -6,5 +6,4 @@ abstract class Seeder extends LaravelSeeder { - } diff --git a/src/Abstracts/Tasks/Task.php b/src/Abstracts/Tasks/Task.php index a7e7253..b2032a4 100644 --- a/src/Abstracts/Tasks/Task.php +++ b/src/Abstracts/Tasks/Task.php @@ -4,5 +4,4 @@ abstract class Task { - } diff --git a/src/Abstracts/Tests/PhpUnit/TestCase.php b/src/Abstracts/Tests/PhpUnit/TestCase.php index 3fa05cf..acaeca9 100644 --- a/src/Abstracts/Tests/PhpUnit/TestCase.php +++ b/src/Abstracts/Tests/PhpUnit/TestCase.php @@ -6,5 +6,4 @@ abstract class TestCase extends LaravelTestCase { - } diff --git a/src/Facades/Porto.php b/src/Facades/Porto.php index ccb3a76..2094a28 100644 --- a/src/Facades/Porto.php +++ b/src/Facades/Porto.php @@ -5,19 +5,6 @@ use Illuminate\Support\Facades\Facade; /** - * @method static array getShipFoldersNames() - * @method static array getShipPath() - * @method static array getSectionContainerNames(string $sectionName) - * @method static mixed getClassObjectFromFile($filePathName) - * @method static string getClassFullNameFromFile($filePathName) - * @method static array getSectionPaths() - * @method static mixed getClassType($className) - * @method static array getAllContainerNames() - * @method static array getAllContainerPaths() - * @method static array getSectionNames() - * @method static array getSectionContainerPaths(string $sectionName) - * @method static void verifyClassExist(string $className) - * * @see \AdminKit\Porto\Porto */ class Porto extends Facade diff --git a/src/Loaders/AliasesLoaderTrait.php b/src/Loaders/AliasesLoaderTrait.php index 2e5a638..bad5c2d 100644 --- a/src/Loaders/AliasesLoaderTrait.php +++ b/src/Loaders/AliasesLoaderTrait.php @@ -18,10 +18,6 @@ public function loadAliases(): static return $this; } - /** - * @param $aliasKey - * @param $aliasValue - */ private function loadAlias($aliasKey, $aliasValue): void { AliasLoader::getInstance()->alias($aliasKey, $aliasValue); diff --git a/src/Loaders/AutoLoaderTrait.php b/src/Loaders/AutoLoaderTrait.php index 9f024d7..87206d2 100644 --- a/src/Loaders/AutoLoaderTrait.php +++ b/src/Loaders/AutoLoaderTrait.php @@ -2,8 +2,6 @@ namespace AdminKit\Porto\Loaders; -use AdminKit\Porto\Facades\Porto; - trait AutoLoaderTrait { // Using each component loader trait @@ -15,6 +13,7 @@ trait AutoLoaderTrait use CommandsLoaderTrait; use AliasesLoaderTrait; use HelpersLoaderTrait; + use PathsLoaderTrait; public function runLoaderBoot(): void { @@ -25,7 +24,7 @@ public function runLoaderBoot(): void $this->loadCommandsFromShip(); // Iterate over all the containers folders and autoload most of the components - foreach (Porto::getAllContainerPaths() as $containerPath) { + foreach ($this->getAllContainerPaths() as $containerPath) { $this->loadMigrationsFromContainers($containerPath); $this->loadLocalsFromContainers($containerPath); $this->loadViewsFromContainers($containerPath); @@ -39,7 +38,7 @@ public function runLoaderRegister(): void $this->loadConfigsFromShip(); $this->loadShipServiceProviderFromShip(); - foreach (Porto::getAllContainerPaths() as $containerPath) { + foreach ($this->getAllContainerPaths() as $containerPath) { $this->loadConfigsFromContainers($containerPath); $this->loadMainServiceProvidersFromContainers($containerPath); } diff --git a/src/Loaders/CommandsLoaderTrait.php b/src/Loaders/CommandsLoaderTrait.php index 6f556db..6d4cc14 100644 --- a/src/Loaders/CommandsLoaderTrait.php +++ b/src/Loaders/CommandsLoaderTrait.php @@ -2,14 +2,15 @@ namespace AdminKit\Porto\Loaders; -use AdminKit\Porto\Facades\Porto; use Illuminate\Support\Facades\File; trait CommandsLoaderTrait { + use PathsLoaderTrait; + public function loadCommandsFromContainers($containerPath): void { - $containerCommandsDirectory = $containerPath . '/UI/CLI/Commands'; + $containerCommandsDirectory = $containerPath.'/UI/CLI/Commands'; $this->loadTheConsoles($containerCommandsDirectory); } @@ -20,8 +21,8 @@ private function loadTheConsoles($directory): void foreach ($files as $consoleFile) { // Do not load route files - if (!$this->isRouteFile($consoleFile)) { - $consoleClass = Porto::getClassFullNameFromFile($consoleFile->getPathname()); + if (! $this->isRouteFile($consoleFile)) { + $consoleClass = $this->getClassFullNameFromFile($consoleFile->getPathname()); // When user from the Main Service Provider, which extends Laravel // service provider you get access to `$this->commands` $this->commands([$consoleClass]); @@ -32,12 +33,12 @@ private function loadTheConsoles($directory): void private function isRouteFile($consoleFile): bool { - return $consoleFile->getFilename() === "closures.php"; + return $consoleFile->getFilename() === 'closures.php'; } public function loadCommandsFromShip(): void { - $shipCommandsDirectory = base_path('app/Ship/Commands'); + $shipCommandsDirectory = $this->getShipPath().'/Commands'; $this->loadTheConsoles($shipCommandsDirectory); } } diff --git a/src/Loaders/ConfigsLoaderTrait.php b/src/Loaders/ConfigsLoaderTrait.php index 91a932a..d14bf7e 100644 --- a/src/Loaders/ConfigsLoaderTrait.php +++ b/src/Loaders/ConfigsLoaderTrait.php @@ -2,14 +2,15 @@ namespace AdminKit\Porto\Loaders; - use Illuminate\Support\Facades\File; trait ConfigsLoaderTrait { + use PathsLoaderTrait; + public function loadConfigsFromShip(): void { - $shipConfigsDirectory = base_path('app/Ship/Configs'); + $shipConfigsDirectory = $this->getShipPath().'/Configs'; $this->loadConfigs($shipConfigsDirectory); } @@ -20,7 +21,7 @@ private function loadConfigs($configFolder): void foreach ($files as $file) { $name = File::name($file); - $path = $configFolder . '/' . $name . '.php'; + $path = $configFolder.'/'.$name.'.php'; $this->mergeConfigFrom($path, $name); } @@ -29,7 +30,7 @@ private function loadConfigs($configFolder): void public function loadConfigsFromContainers($containerPath): void { - $containerConfigsDirectory = $containerPath . '/Configs'; + $containerConfigsDirectory = $containerPath.'/Configs'; $this->loadConfigs($containerConfigsDirectory); } } diff --git a/src/Loaders/HelpersLoaderTrait.php b/src/Loaders/HelpersLoaderTrait.php index 5296ce4..50258eb 100644 --- a/src/Loaders/HelpersLoaderTrait.php +++ b/src/Loaders/HelpersLoaderTrait.php @@ -7,9 +7,11 @@ trait HelpersLoaderTrait { + use PathsLoaderTrait; + public function loadHelpersFromContainers($containerPath): void { - $containerHelpersDirectory = $containerPath . '/Helpers'; + $containerHelpersDirectory = $containerPath.'/Helpers'; $this->loadHelpers($containerHelpersDirectory); } @@ -20,7 +22,7 @@ private function loadHelpers($helpersFolder): void foreach ($files as $file) { try { - require($file); + require $file; } catch (FileNotFoundException $e) { } } @@ -29,7 +31,7 @@ private function loadHelpers($helpersFolder): void public function loadHelpersFromShip(): void { - $shipHelpersDirectory = base_path('app/Ship/Helpers'); + $shipHelpersDirectory = $this->getShipPath().'/Helpers'; $this->loadHelpers($shipHelpersDirectory); } } diff --git a/src/Loaders/LocalizationLoaderTrait.php b/src/Loaders/LocalizationLoaderTrait.php index 485df0c..fd6d758 100644 --- a/src/Loaders/LocalizationLoaderTrait.php +++ b/src/Loaders/LocalizationLoaderTrait.php @@ -7,9 +7,11 @@ trait LocalizationLoaderTrait { + use PathsLoaderTrait; + public function loadLocalsFromContainers($containerPath): void { - $containerLocaleDirectory = $containerPath . '/Languages'; + $containerLocaleDirectory = $containerPath.'/Languages'; $containerName = basename($containerPath); $pathParts = explode(DIRECTORY_SEPARATOR, $containerPath); $sectionName = $pathParts[count($pathParts) - 2]; @@ -27,14 +29,14 @@ private function loadLocals($directory, $containerName, $sectionName = null): vo private function buildLocaleNamespace(?string $sectionName, string $containerName): string { - return $sectionName ? (Str::camel($sectionName) . '@' . Str::camel($containerName)) : Str::camel( + return $sectionName ? (Str::camel($sectionName).'@'.Str::camel($containerName)) : Str::camel( $containerName ); } public function loadLocalsFromShip(): void { - $shipLocaleDirectory = base_path('app/Ship/Languages'); - $this->loadLocals($shipLocaleDirectory, 'ship'); + $shipLocaleDirectory = $this->getShipPath().'/Languages'; + $this->loadLocals($shipLocaleDirectory, $this->shipFolderName); } } diff --git a/src/Loaders/MiddlewaresLoaderTrait.php b/src/Loaders/MiddlewaresLoaderTrait.php index e81c333..d88b486 100644 --- a/src/Loaders/MiddlewaresLoaderTrait.php +++ b/src/Loaders/MiddlewaresLoaderTrait.php @@ -9,6 +9,7 @@ trait MiddlewaresLoaderTrait { /** * @void + * * @throws BindingResolutionException */ public function loadMiddlewares() @@ -22,7 +23,6 @@ public function loadMiddlewares() /** * Registering Route Group's * - * @param array $middlewares * @throws BindingResolutionException */ private function registerMiddleware(array $middlewares = []) @@ -36,13 +36,11 @@ private function registerMiddleware(array $middlewares = []) /** * Registering Route Group's - * - * @param array $middlewareGroups */ private function registerMiddlewareGroups(array $middlewareGroups = []) { foreach ($middlewareGroups as $key => $middleware) { - if (!is_array($middleware)) { + if (! is_array($middleware)) { $this->app['router']->pushMiddlewareToGroup($key, $middleware); } else { foreach ($middleware as $item) { @@ -54,13 +52,11 @@ private function registerMiddlewareGroups(array $middlewareGroups = []) /** * Registering Route Middleware's priority - * - * @param array $middlewarePriority */ private function registerMiddlewarePriority(array $middlewarePriority = []) { foreach ($middlewarePriority as $key => $middleware) { - if (!in_array($middleware, $this->app['router']->middlewarePriority)) { + if (! in_array($middleware, $this->app['router']->middlewarePriority)) { $this->app['router']->middlewarePriority[] = $middleware; } } @@ -68,8 +64,6 @@ private function registerMiddlewarePriority(array $middlewarePriority = []) /** * Registering Route Middleware's - * - * @param array $routeMiddleware */ private function registerRouteMiddleware(array $routeMiddleware = []) { diff --git a/src/Loaders/MigrationsLoaderTrait.php b/src/Loaders/MigrationsLoaderTrait.php index 7e386d6..02cc747 100644 --- a/src/Loaders/MigrationsLoaderTrait.php +++ b/src/Loaders/MigrationsLoaderTrait.php @@ -6,9 +6,11 @@ trait MigrationsLoaderTrait { + use PathsLoaderTrait; + public function loadMigrationsFromContainers($containerPath): void { - $containerMigrationDirectory = $containerPath . '/Data/Migrations'; + $containerMigrationDirectory = $containerPath.'/Data/Migrations'; $this->loadMigrations($containerMigrationDirectory); } @@ -21,7 +23,7 @@ private function loadMigrations($directory): void public function loadMigrationsFromShip(): void { - $shipMigrationDirectory = base_path('app/Ship/Migrations'); + $shipMigrationDirectory = $this->getShipPath().'/Migrations'; $this->loadMigrations($shipMigrationDirectory); } } diff --git a/src/Loaders/PathsLoaderTrait.php b/src/Loaders/PathsLoaderTrait.php new file mode 100644 index 0000000..042e26f --- /dev/null +++ b/src/Loaders/PathsLoaderTrait.php @@ -0,0 +1,206 @@ +portoPath = $portoPath; + $this->shipFolderName = $shipFolderName; + $this->containersFolderName = $containersFolderName; + + return $this; + } + + public function getShipFoldersNames(): array + { + $shipFoldersNames = []; + + foreach ($this->getShipFolders() as $shipFoldersPath) { + $shipFoldersNames[] = basename($shipFoldersPath); + } + + return $shipFoldersNames; + } + + public function getShipPath(): string + { + return $this->portoPath.DIRECTORY_SEPARATOR.$this->shipFolderName; + } + + public function getShipFolders(): array + { + return File::directories($this->getShipPath()); + } + + public function getContainersPath(): string + { + return $this->portoPath.DIRECTORY_SEPARATOR.$this->containersFolderName; + } + + public function getSectionPath(string $sectionName): string + { + return $this->portoPath.DIRECTORY_SEPARATOR.$this->containersFolderName.DIRECTORY_SEPARATOR.$sectionName; + } + + public function getSectionFolders(): array + { + if (! File::isDirectory($this->getContainersPath())) { + return []; + } + + return File::directories($this->getContainersPath()); + } + + public function getSectionContainerFolders(string $sectionName): array + { + return File::directories($this->portoPath.DIRECTORY_SEPARATOR.$this->containersFolderName.DIRECTORY_SEPARATOR.$sectionName); + } + + public function getSectionContainerNames(string $sectionName): array + { + $containerNames = []; + foreach (File::directories($this->getSectionPath($sectionName)) as $key => $name) { + $containerNames[] = basename($name); + } + + return $containerNames; + } + + /** + * Build and return an object of a class from its file path + */ + public function getClassObjectFromFile(string $filePathName): mixed + { + $classString = $this->getClassFullNameFromFile($filePathName); + + return new $classString(); + } + + /** + * Get the full name (name \ namespace) of a class from its file path + * result example: (string) "I\Am\The\Namespace\Of\This\Class" + */ + public function getClassFullNameFromFile(string $filePathName): string + { + return "{$this->getClassNamespaceFromFile($filePathName)}\\{$this->getClassNameFromFile($filePathName)}"; + } + + /** + * Get the class namespace form file path using token + */ + protected function getClassNamespaceFromFile(string $filePathName): ?string + { + $src = file_get_contents($filePathName); + + $tokens = token_get_all($src); + $count = count($tokens); + $i = 0; + $namespace = ''; + $namespace_ok = false; + while ($i < $count) { + $token = $tokens[$i]; + if (is_array($token) && $token[0] === T_NAMESPACE) { + // Found namespace declaration + while (++$i < $count) { + if ($tokens[$i] === ';') { + $namespace_ok = true; + $namespace = trim($namespace); + + break; + } + $namespace .= is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i]; + } + + break; + } + $i++; + } + if (! $namespace_ok) { + return null; + } + + return $namespace; + } + + /** + * Get the class name from file path using token + */ + protected function getClassNameFromFile(string $filePathName): mixed + { + $php_code = file_get_contents($filePathName); + + $classes = []; + $tokens = token_get_all($php_code); + $count = count($tokens); + for ($i = 2; $i < $count; $i++) { + if ($tokens[$i - 2][0] == T_CLASS + && $tokens[$i - 1][0] == T_WHITESPACE + && $tokens[$i][0] == T_STRING + ) { + $class_name = $tokens[$i][1]; + $classes[] = $class_name; + } + } + + return $classes[0]; + } + + /** + * Get the last part of a camel case string. + * Example input = helloDearWorld | returns = World + */ + public function getClassType(string $className): mixed + { + $array = preg_split('/(?=[A-Z])/', $className); + + return end($array); + } + + public function getAllContainerNames(): array + { + $containersNames = []; + + foreach ($this->getAllContainerPaths() as $containersPath) { + $containersNames[] = basename($containersPath); + } + + return $containersNames; + } + + public function getAllContainerPaths(): array + { + $sectionNames = $this->getSectionNames(); + $containerPaths = []; + foreach ($sectionNames as $name) { + $sectionContainerPaths = $this->getSectionContainerFolders($name); + foreach ($sectionContainerPaths as $containerPath) { + $containerPaths[] = $containerPath; + } + } + + return $containerPaths; + } + + public function getSectionNames(): array + { + $sectionNames = []; + + foreach ($this->getSectionFolders() as $sectionPath) { + $sectionNames[] = basename($sectionPath); + } + + return $sectionNames; + } +} diff --git a/src/Loaders/ProvidersLoaderTrait.php b/src/Loaders/ProvidersLoaderTrait.php index 1e04ed4..b99d1ed 100644 --- a/src/Loaders/ProvidersLoaderTrait.php +++ b/src/Loaders/ProvidersLoaderTrait.php @@ -2,23 +2,23 @@ namespace AdminKit\Porto\Loaders; -use AdminKit\Porto\Facades\Porto; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\File; use Illuminate\Support\Str; trait ProvidersLoaderTrait { + use PathsLoaderTrait; + /** * Loads only the Main Service Providers from the Containers. * All the Service Providers (registered inside the main), will be * loaded from the `boot()` function on the parent of the Main * Service Providers. - * @param $containerPath */ public function loadMainServiceProvidersFromContainers($containerPath): static { - $containerProvidersDirectory = $containerPath . '/Providers'; + $containerProvidersDirectory = $containerPath.'/Providers'; $this->loadProviders($containerProvidersDirectory); return $this; @@ -35,7 +35,7 @@ private function loadProviders($directory): void if (File::isFile($file)) { // Check if this is the Main Service Provider if (Str::startsWith($file->getFilename(), $mainServiceProviderNameStartWith)) { - $serviceProviderClass = Porto::getClassFullNameFromFile($file->getPathname()); + $serviceProviderClass = $this->getClassFullNameFromFile($file->getPathname()); $this->loadProvider($serviceProviderClass); } } @@ -65,10 +65,11 @@ public function loadServiceProviders(): static public function loadShipServiceProviderFromShip(): void { - $shipProvider = 'App\Ship\Providers\ShipProvider'; + $shipProviderFile = $this->getShipPath().'/Providers/ShipProvider.php'; - if (class_exists($shipProvider)) { - $this->loadProvider($shipProvider); + if (file_exists($shipProviderFile)) { + $shipProviderClass = $this->getClassFullNameFromFile($shipProviderFile); + $this->loadProvider($shipProviderClass); } } } diff --git a/src/Loaders/RoutesLoaderTrait.php b/src/Loaders/RoutesLoaderTrait.php index a0e47e5..ebe18d4 100644 --- a/src/Loaders/RoutesLoaderTrait.php +++ b/src/Loaders/RoutesLoaderTrait.php @@ -2,7 +2,6 @@ namespace AdminKit\Porto\Loaders; -use AdminKit\Porto\Facades\Porto; use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Http\Request; use Illuminate\Support\Arr; @@ -14,12 +13,14 @@ trait RoutesLoaderTrait { + use PathsLoaderTrait; + /** * Register all the containers routes files in the framework */ public function runRoutesAutoLoader(): void { - $containersPaths = Porto::getAllContainerPaths(); + $containersPaths = $this->getAllContainerPaths(); foreach ($containersPaths as $containerPath) { $this->loadApiContainerRoutes($containerPath); @@ -29,14 +30,13 @@ public function runRoutesAutoLoader(): void /** * Register the Containers API routes files - * @param string $containerPath */ private function loadApiContainerRoutes(string $containerPath): void { // Build the container api routes path - $apiRoutesPath = $containerPath . '/UI/API/Routes'; + $apiRoutesPath = $containerPath.'/UI/API/Routes'; // Build the namespace from the path - $controllerNamespace = $containerPath . '\\UI\API\Controllers'; + $controllerNamespace = $containerPath.'\\UI\API\Controllers'; if (File::isDirectory($apiRoutesPath)) { $files = File::allFiles($apiRoutesPath); @@ -49,10 +49,6 @@ private function loadApiContainerRoutes(string $containerPath): void } } - /** - * @param $file - * @param $controllerNamespace - */ private function loadApiRoute($file, $controllerNamespace): void { $routeGroupArray = $this->getRouteGroup($file, $controllerNamespace); @@ -63,10 +59,7 @@ private function loadApiRoute($file, $controllerNamespace): void } /** - * @param $endpointFileOrPrefixString - * @param null $controllerNamespace - * - * @return array + * @param null $controllerNamespace */ public function getRouteGroup($endpointFileOrPrefixString, $controllerNamespace = null): array { @@ -81,9 +74,6 @@ public function getRouteGroup($endpointFileOrPrefixString, $controllerNamespace ]; } - /** - * @return array - */ private function getMiddlewares(): array { return array_filter([ @@ -92,9 +82,6 @@ private function getMiddlewares(): array ]); } - /** - * @return null|string - */ private function getRateLimitMiddleware(): ?string { $rateLimitMiddleware = null; @@ -121,23 +108,13 @@ private function getApiUrl() return Config::get('apiato.api.url'); } - /** - * @param $file - * - * @return string - */ private function getApiVersionPrefix($file): string { - return Config::get('apiato.api.prefix') . (Config::get( - 'apiato.api.enable_version_prefix' - ) ? $this->getRouteFileVersionFromFileName($file) : ''); + return Config::get('apiato.api.prefix').(Config::get( + 'apiato.api.enable_version_prefix' + ) ? $this->getRouteFileVersionFromFileName($file) : ''); } - /** - * @param $file - * - * @return string|bool - */ private function getRouteFileVersionFromFileName($file): string|bool { $fileNameWithoutExtension = $this->getRouteFileNameWithoutExtension($file); @@ -156,11 +133,6 @@ private function getRouteFileVersionFromFileName($file): string|bool return $apiVersion; } - /** - * @param SplFileInfo $file - * - * @return mixed - */ private function getRouteFileNameWithoutExtension(SplFileInfo $file): mixed { return pathinfo($file->getFileName())['filename']; @@ -168,15 +140,13 @@ private function getRouteFileNameWithoutExtension(SplFileInfo $file): mixed /** * Register the Containers WEB routes files - * - * @param $containerPath */ private function loadWebContainerRoutes($containerPath): void { // build the container web routes path - $webRoutesPath = $containerPath . '/UI/WEB/Routes'; + $webRoutesPath = $containerPath.'/UI/WEB/Routes'; // build the namespace from the path - $controllerNamespace = $containerPath . '\\UI\WEB\Controllers'; + $controllerNamespace = $containerPath.'\\UI\WEB\Controllers'; if (File::isDirectory($webRoutesPath)) { $files = File::allFiles($webRoutesPath); @@ -189,10 +159,6 @@ private function loadWebContainerRoutes($containerPath): void } } - /** - * @param $file - * @param $controllerNamespace - */ private function loadWebRoute($file, $controllerNamespace): void { Route::group([ diff --git a/src/Loaders/SeederLoaderTrait.php b/src/Loaders/SeederLoaderTrait.php index 366a485..605f593 100644 --- a/src/Loaders/SeederLoaderTrait.php +++ b/src/Loaders/SeederLoaderTrait.php @@ -2,7 +2,6 @@ namespace AdminKit\Porto\Loaders; -use AdminKit\Porto\Facades\Porto; use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; @@ -13,6 +12,8 @@ */ trait SeederLoaderTrait { + use PathsLoaderTrait; + protected string $seedersPath = '/Data/Seeders'; public function runLoadingSeeders(): void @@ -26,11 +27,10 @@ private function loadSeedersFromContainers(): void $containersDirectories = []; - foreach (Porto::getSectionNames() as $sectionName) { - foreach (Porto::getSectionContainerNames($sectionName) as $containerName) { - $containersDirectories[] = base_path( - 'app/Containers/' . $sectionName . '/' . $containerName . $this->seedersPath - ); + foreach ($this->getSectionNames() as $sectionName) { + foreach ($this->getSectionContainerNames($sectionName) as $containerName) { + $containersDirectories[] = $this->portoPath. + '/Containers/'.$sectionName.'/'.$containerName.$this->seedersPath; } } @@ -50,7 +50,7 @@ private function findSeedersClasses(array $directories, $seedersClasses) if (File::isFile($seederClass)) { // do not seed the classes now, just store them in a collection and w $seedersClasses->push( - Porto::getClassFullNameFromFile( + $this->getClassFullNameFromFile( $seederClass->getPathname() ) ); @@ -72,7 +72,7 @@ private function sortSeeders($seedersClasses): Collection foreach ($seedersClasses as $key => $seederFullClassName) { // if the class full namespace contain "_" it means it needs to be seeded in order - if (str_contains($seederFullClassName, "_")) { + if (str_contains($seederFullClassName, '_')) { // move all the seeder classes that needs to be seeded in order to their own Collection $orderedSeederClasses->push($seederFullClassName); // delete the moved classes from the original collection @@ -83,7 +83,7 @@ private function sortSeeders($seedersClasses): Collection // sort the classes that needed to be ordered $orderedSeederClasses = $orderedSeederClasses->sortBy(function ($seederFullClassName) { // get the order number form the end of each class name - return substr($seederFullClassName, strpos($seederFullClassName, "_") + 1); + return substr($seederFullClassName, strpos($seederFullClassName, '_') + 1); }); // append the randomly ordered seeder classes to the end of the ordered seeder classes @@ -94,9 +94,6 @@ private function sortSeeders($seedersClasses): Collection return $orderedSeederClasses; } - /** - * @param $seedersClasses - */ private function loadSeeders($seedersClasses): void { foreach ($seedersClasses as $seeder) { diff --git a/src/Loaders/ViewsLoaderTrait.php b/src/Loaders/ViewsLoaderTrait.php index e4c6b5a..1227722 100644 --- a/src/Loaders/ViewsLoaderTrait.php +++ b/src/Loaders/ViewsLoaderTrait.php @@ -9,8 +9,8 @@ trait ViewsLoaderTrait { public function loadViewsFromContainers($containerPath): void { - $containerViewDirectory = $containerPath . '/UI/WEB/Views/'; - $containerMailTemplatesDirectory = $containerPath . '/Mails/Templates/'; + $containerViewDirectory = $containerPath.'/UI/WEB/Views/'; + $containerMailTemplatesDirectory = $containerPath.'/Mails/Templates/'; $containerName = basename($containerPath); $pathParts = explode(DIRECTORY_SEPARATOR, $containerPath); @@ -29,14 +29,14 @@ private function loadViews($directory, $containerName, $sectionName = null): voi private function buildViewNamespace(?string $sectionName, string $containerName): string { - return $sectionName ? (Str::camel($sectionName) . '@' . Str::camel($containerName)) : Str::camel( + return $sectionName ? (Str::camel($sectionName).'@'.Str::camel($containerName)) : Str::camel( $containerName ); } public function loadViewsFromShip(): void { - $shipMailTemplatesDirectory = base_path('app/Ship/Mails/Templates/'); + $shipMailTemplatesDirectory = $this->getShipPath().'/Mails/Templates/'; $this->loadViews($shipMailTemplatesDirectory, 'ship'); // Ship views accessible via `ship::`. } } diff --git a/src/Porto.php b/src/Porto.php index 7002216..2fcf8c3 100755 --- a/src/Porto.php +++ b/src/Porto.php @@ -2,197 +2,7 @@ namespace AdminKit\Porto; -use Illuminate\Support\Facades\File; - class Porto { - private const SHIP_DIRECTORY_NAME = 'Ship'; - private const CONTAINERS_DIRECTORY_NAME = 'Containers'; - - public function getShipFoldersNames(): array - { - $shipFoldersNames = []; - - foreach ($this->getShipPath() as $shipFoldersPath) { - $shipFoldersNames[] = basename($shipFoldersPath); - } - - return $shipFoldersNames; - } - - public function getShipPath(): array - { - return File::directories(app_path(self::SHIP_DIRECTORY_NAME)); - } - - public function getSectionContainerNames(string $sectionName): array - { - $containerNames = []; - foreach (File::directories($this->getSectionPath($sectionName)) as $key => $name) { - $containerNames[] = basename($name); - } - - return $containerNames; - } - - private function getSectionPath(string $sectionName): string - { - return app_path(self::CONTAINERS_DIRECTORY_NAME . DIRECTORY_SEPARATOR . $sectionName); - } - - /** - * Build and return an object of a class from its file path - * - * @param string $filePathName - * - * @return mixed - */ - public function getClassObjectFromFile(string $filePathName): mixed - { - $classString = $this->getClassFullNameFromFile($filePathName); - - return new $classString(); - } - - /** - * Get the full name (name \ namespace) of a class from its file path - * result example: (string) "I\Am\The\Namespace\Of\This\Class" - * - * @param string $filePathName - * - * @return string - */ - public function getClassFullNameFromFile(string $filePathName): string - { - return "{$this->getClassNamespaceFromFile($filePathName)}\\{$this->getClassNameFromFile($filePathName)}"; - } - - /** - * Get the class namespace form file path using token - * - * @param string $filePathName - * - * @return null|string - */ - protected function getClassNamespaceFromFile(string $filePathName): ?string - { - $src = file_get_contents($filePathName); - - $tokens = token_get_all($src); - $count = count($tokens); - $i = 0; - $namespace = ''; - $namespace_ok = false; - while ($i < $count) { - $token = $tokens[$i]; - if (is_array($token) && $token[0] === T_NAMESPACE) { - // Found namespace declaration - while (++$i < $count) { - if ($tokens[$i] === ';') { - $namespace_ok = true; - $namespace = trim($namespace); - - break; - } - $namespace .= is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i]; - } - - break; - } - $i++; - } - if (!$namespace_ok) { - return null; - } - - return $namespace; - } - - /** - * Get the class name from file path using token - * - * @param string $filePathName - * - * @return mixed - */ - protected function getClassNameFromFile(string $filePathName): mixed - { - $php_code = file_get_contents($filePathName); - - $classes = []; - $tokens = token_get_all($php_code); - $count = count($tokens); - for ($i = 2; $i < $count; $i++) { - if ($tokens[$i - 2][0] == T_CLASS - && $tokens[$i - 1][0] == T_WHITESPACE - && $tokens[$i][0] == T_STRING - ) { - $class_name = $tokens[$i][1]; - $classes[] = $class_name; - } - } - - return $classes[0]; - } - - /** - * Get the last part of a camel case string. - * Example input = helloDearWorld | returns = World - * - * @param string $className - * - * @return mixed - */ - public function getClassType(string $className): mixed - { - $array = preg_split('/(?=[A-Z])/', $className); - - return end($array); - } - - public function getAllContainerNames(): array - { - $containersNames = []; - - foreach ($this->getAllContainerPaths() as $containersPath) { - $containersNames[] = basename($containersPath); - } - - return $containersNames; - } - - public function getAllContainerPaths(): array - { - $sectionNames = $this->getSectionNames(); - $containerPaths = []; - foreach ($sectionNames as $name) { - $sectionContainerPaths = $this->getSectionContainerPaths($name); - foreach ($sectionContainerPaths as $containerPath) { - $containerPaths[] = $containerPath; - } - } - - return $containerPaths; - } - - public function getSectionNames(): array - { - $sectionNames = []; - - foreach ($this->getSectionPaths() as $sectionPath) { - $sectionNames[] = basename($sectionPath); - } - - return $sectionNames; - } - - public function getSectionPaths(): array - { - return File::directories(app_path(self::CONTAINERS_DIRECTORY_NAME)); - } - - public function getSectionContainerPaths(string $sectionName): array - { - return File::directories(app_path(self::CONTAINERS_DIRECTORY_NAME . DIRECTORY_SEPARATOR . $sectionName)); - } + public const VERSION = '1.0.2'; } diff --git a/src/PortoServiceProvider.php b/src/PortoServiceProvider.php index 370016b..e45ce81 100644 --- a/src/PortoServiceProvider.php +++ b/src/PortoServiceProvider.php @@ -2,10 +2,10 @@ namespace AdminKit\Porto; +use AdminKit\Porto\Commands\PortoCommand; use AdminKit\Porto\Loaders\AutoLoaderTrait; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; -use AdminKit\Porto\Commands\PortoCommand; class PortoServiceProvider extends PackageServiceProvider { @@ -26,11 +26,15 @@ public function configurePackage(Package $package): void public function bootingPackage() { - $this->runLoaderBoot(); + $this + ->initPorto(portoPath: app_path()) + ->runLoaderBoot(); } public function registeringPackage() { - $this->runLoaderRegister(); + $this + ->initPorto(portoPath: app_path()) + ->runLoaderRegister(); } } diff --git a/src/Traits/CyrillicChars.php b/src/Traits/CyrillicChars.php index 0cc79e8..8dcadc4 100644 --- a/src/Traits/CyrillicChars.php +++ b/src/Traits/CyrillicChars.php @@ -6,9 +6,6 @@ trait CyrillicChars { /** * save cyrillic characters to the database - * - * @param mixed $value - * @return string */ public function asJson(mixed $value): string { diff --git a/tests/TestCase.php b/tests/TestCase.php index 281dea0..d48fe57 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,9 @@ namespace AdminKit\Porto\Tests; +use AdminKit\Porto\PortoServiceProvider; use Illuminate\Database\Eloquent\Factories\Factory; use Orchestra\Testbench\TestCase as Orchestra; -use AdminKit\Porto\PortoServiceProvider; class TestCase extends Orchestra {