Skip to content

Commit

Permalink
feat: add loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
ast21 committed Mar 8, 2023
1 parent b372350 commit ae6b9a9
Show file tree
Hide file tree
Showing 15 changed files with 977 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Facades/Porto.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
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
Expand Down
29 changes: 29 additions & 0 deletions src/Loaders/AliasesLoaderTrait.php
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);
}
}
47 changes: 47 additions & 0 deletions src/Loaders/AutoLoaderTrait.php
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);
}
}
}
43 changes: 43 additions & 0 deletions src/Loaders/CommandsLoaderTrait.php
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);
}
}
35 changes: 35 additions & 0 deletions src/Loaders/ConfigsLoaderTrait.php
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);
}
}
35 changes: 35 additions & 0 deletions src/Loaders/HelpersLoaderTrait.php
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);
}
}
40 changes: 40 additions & 0 deletions src/Loaders/LocalizationLoaderTrait.php
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');
}
}
80 changes: 80 additions & 0 deletions src/Loaders/MiddlewaresLoaderTrait.php
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);
}
}
}
27 changes: 27 additions & 0 deletions src/Loaders/MigrationsLoaderTrait.php
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);
}
}
Loading

0 comments on commit ae6b9a9

Please sign in to comment.