generated from admin-kit/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
977 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace AdminKit\Porto\Loaders; | ||
|
||
use Illuminate\Foundation\AliasLoader; | ||
|
||
trait AliasesLoaderTrait | ||
{ | ||
public function loadAliases(): static | ||
{ | ||
// `$this->aliases` is declared on each Container's Main Service Provider | ||
foreach ($this->aliases ?? [] as $aliasKey => $aliasValue) { | ||
if (class_exists($aliasValue)) { | ||
$this->loadAlias($aliasKey, $aliasValue); | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $aliasKey | ||
* @param $aliasValue | ||
*/ | ||
private function loadAlias($aliasKey, $aliasValue): void | ||
{ | ||
AliasLoader::getInstance()->alias($aliasKey, $aliasValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace AdminKit\Porto\Loaders; | ||
|
||
use AdminKit\Porto\Facades\Porto; | ||
|
||
trait AutoLoaderTrait | ||
{ | ||
// Using each component loader trait | ||
use ConfigsLoaderTrait; | ||
use LocalizationLoaderTrait; | ||
use MigrationsLoaderTrait; | ||
use ViewsLoaderTrait; | ||
use ProvidersLoaderTrait; | ||
use CommandsLoaderTrait; | ||
use AliasesLoaderTrait; | ||
use HelpersLoaderTrait; | ||
|
||
public function runLoaderBoot(): void | ||
{ | ||
$this->loadMigrationsFromShip(); | ||
$this->loadLocalsFromShip(); | ||
$this->loadViewsFromShip(); | ||
$this->loadHelpersFromShip(); | ||
$this->loadCommandsFromShip(); | ||
|
||
// Iterate over all the containers folders and autoload most of the components | ||
foreach (Porto::getAllContainerPaths() as $containerPath) { | ||
$this->loadMigrationsFromContainers($containerPath); | ||
$this->loadLocalsFromContainers($containerPath); | ||
$this->loadViewsFromContainers($containerPath); | ||
$this->loadHelpersFromContainers($containerPath); | ||
$this->loadCommandsFromContainers($containerPath); | ||
} | ||
} | ||
|
||
public function runLoaderRegister(): void | ||
{ | ||
$this->loadConfigsFromShip(); | ||
$this->loadShipServiceProviderFromShip(); | ||
|
||
foreach (Porto::getAllContainerPaths() as $containerPath) { | ||
$this->loadConfigsFromContainers($containerPath); | ||
$this->loadMainServiceProvidersFromContainers($containerPath); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace AdminKit\Porto\Loaders; | ||
|
||
use AdminKit\Porto\Facades\Porto; | ||
use Illuminate\Support\Facades\File; | ||
|
||
trait CommandsLoaderTrait | ||
{ | ||
public function loadCommandsFromContainers($containerPath): void | ||
{ | ||
$containerCommandsDirectory = $containerPath . '/UI/CLI/Commands'; | ||
$this->loadTheConsoles($containerCommandsDirectory); | ||
} | ||
|
||
private function loadTheConsoles($directory): void | ||
{ | ||
if (File::isDirectory($directory)) { | ||
$files = File::allFiles($directory); | ||
|
||
foreach ($files as $consoleFile) { | ||
// Do not load route files | ||
if (!$this->isRouteFile($consoleFile)) { | ||
$consoleClass = Porto::getClassFullNameFromFile($consoleFile->getPathname()); | ||
// When user from the Main Service Provider, which extends Laravel | ||
// service provider you get access to `$this->commands` | ||
$this->commands([$consoleClass]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private function isRouteFile($consoleFile): bool | ||
{ | ||
return $consoleFile->getFilename() === "closures.php"; | ||
} | ||
|
||
public function loadCommandsFromShip(): void | ||
{ | ||
$shipCommandsDirectory = base_path('app/Ship/Commands'); | ||
$this->loadTheConsoles($shipCommandsDirectory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace AdminKit\Porto\Loaders; | ||
|
||
|
||
use Illuminate\Support\Facades\File; | ||
|
||
trait ConfigsLoaderTrait | ||
{ | ||
public function loadConfigsFromShip(): void | ||
{ | ||
$shipConfigsDirectory = base_path('app/Ship/Configs'); | ||
$this->loadConfigs($shipConfigsDirectory); | ||
} | ||
|
||
private function loadConfigs($configFolder): void | ||
{ | ||
if (File::isDirectory($configFolder)) { | ||
$files = File::files($configFolder); | ||
|
||
foreach ($files as $file) { | ||
$name = File::name($file); | ||
$path = $configFolder . '/' . $name . '.php'; | ||
|
||
$this->mergeConfigFrom($path, $name); | ||
} | ||
} | ||
} | ||
|
||
public function loadConfigsFromContainers($containerPath): void | ||
{ | ||
$containerConfigsDirectory = $containerPath . '/Configs'; | ||
$this->loadConfigs($containerConfigsDirectory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace AdminKit\Porto\Loaders; | ||
|
||
use Illuminate\Contracts\Filesystem\FileNotFoundException; | ||
use Illuminate\Support\Facades\File; | ||
|
||
trait HelpersLoaderTrait | ||
{ | ||
public function loadHelpersFromContainers($containerPath): void | ||
{ | ||
$containerHelpersDirectory = $containerPath . '/Helpers'; | ||
$this->loadHelpers($containerHelpersDirectory); | ||
} | ||
|
||
private function loadHelpers($helpersFolder): void | ||
{ | ||
if (File::isDirectory($helpersFolder)) { | ||
$files = File::files($helpersFolder); | ||
|
||
foreach ($files as $file) { | ||
try { | ||
require($file); | ||
} catch (FileNotFoundException $e) { | ||
} | ||
} | ||
} | ||
} | ||
|
||
public function loadHelpersFromShip(): void | ||
{ | ||
$shipHelpersDirectory = base_path('app/Ship/Helpers'); | ||
$this->loadHelpers($shipHelpersDirectory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace AdminKit\Porto\Loaders; | ||
|
||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Str; | ||
|
||
trait LocalizationLoaderTrait | ||
{ | ||
public function loadLocalsFromContainers($containerPath): void | ||
{ | ||
$containerLocaleDirectory = $containerPath . '/Languages'; | ||
$containerName = basename($containerPath); | ||
$pathParts = explode(DIRECTORY_SEPARATOR, $containerPath); | ||
$sectionName = $pathParts[count($pathParts) - 2]; | ||
|
||
$this->loadLocals($containerLocaleDirectory, $containerName, $sectionName); | ||
} | ||
|
||
private function loadLocals($directory, $containerName, $sectionName = null): void | ||
{ | ||
if (File::isDirectory($directory)) { | ||
$this->loadTranslationsFrom($directory, $this->buildLocaleNamespace($sectionName, $containerName)); | ||
$this->loadJsonTranslationsFrom($directory); | ||
} | ||
} | ||
|
||
private function buildLocaleNamespace(?string $sectionName, string $containerName): string | ||
{ | ||
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace AdminKit\Porto\Loaders; | ||
|
||
use Illuminate\Contracts\Container\BindingResolutionException; | ||
use Illuminate\Contracts\Http\Kernel; | ||
|
||
trait MiddlewaresLoaderTrait | ||
{ | ||
/** | ||
* @void | ||
* @throws BindingResolutionException | ||
*/ | ||
public function loadMiddlewares() | ||
{ | ||
$this->registerMiddleware($this->middlewares); | ||
$this->registerMiddlewareGroups($this->middlewareGroups); | ||
$this->registerMiddlewarePriority($this->middlewarePriority); | ||
$this->registerRouteMiddleware($this->routeMiddleware); | ||
} | ||
|
||
/** | ||
* Registering Route Group's | ||
* | ||
* @param array $middlewares | ||
* @throws BindingResolutionException | ||
*/ | ||
private function registerMiddleware(array $middlewares = []) | ||
{ | ||
$httpKernel = $this->app->make(Kernel::class); | ||
|
||
foreach ($middlewares as $middleware) { | ||
$httpKernel->prependMiddleware($middleware); | ||
} | ||
} | ||
|
||
/** | ||
* Registering Route Group's | ||
* | ||
* @param array $middlewareGroups | ||
*/ | ||
private function registerMiddlewareGroups(array $middlewareGroups = []) | ||
{ | ||
foreach ($middlewareGroups as $key => $middleware) { | ||
if (!is_array($middleware)) { | ||
$this->app['router']->pushMiddlewareToGroup($key, $middleware); | ||
} else { | ||
foreach ($middleware as $item) { | ||
$this->app['router']->pushMiddlewareToGroup($key, $item); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 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)) { | ||
$this->app['router']->middlewarePriority[] = $middleware; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Registering Route Middleware's | ||
* | ||
* @param array $routeMiddleware | ||
*/ | ||
private function registerRouteMiddleware(array $routeMiddleware = []) | ||
{ | ||
foreach ($routeMiddleware as $key => $value) { | ||
$this->app['router']->aliasMiddleware($key, $value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace AdminKit\Porto\Loaders; | ||
|
||
use Illuminate\Support\Facades\File; | ||
|
||
trait MigrationsLoaderTrait | ||
{ | ||
public function loadMigrationsFromContainers($containerPath): void | ||
{ | ||
$containerMigrationDirectory = $containerPath . '/Data/Migrations'; | ||
$this->loadMigrations($containerMigrationDirectory); | ||
} | ||
|
||
private function loadMigrations($directory): void | ||
{ | ||
if (File::isDirectory($directory)) { | ||
$this->loadMigrationsFrom($directory); | ||
} | ||
} | ||
|
||
public function loadMigrationsFromShip(): void | ||
{ | ||
$shipMigrationDirectory = base_path('app/Ship/Migrations'); | ||
$this->loadMigrations($shipMigrationDirectory); | ||
} | ||
} |
Oops, something went wrong.