Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Jan 1, 2025
1 parent 5f13267 commit 4155eef
Show file tree
Hide file tree
Showing 30 changed files with 497 additions and 256 deletions.
9 changes: 9 additions & 0 deletions src/Abstract/Providers/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Apiato\Abstract\Providers;

use Illuminate\Support\ServiceProvider as LaravelServiceProvider;

abstract class ServiceProvider extends LaravelServiceProvider
{
}
123 changes: 96 additions & 27 deletions src/Foundation/Loaders/Apiato.php → src/Foundation/Apiato.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
<?php

namespace Apiato\Foundation\Loaders;
namespace Apiato\Foundation;

use Apiato\Foundation\Configuration\ApplicationBuilder;
use Apiato\Foundation\Configuration\Localization;
use Apiato\Foundation\Middlewares\ProcessETag;
use Apiato\Foundation\Middlewares\Profiler;
use Apiato\Foundation\Middlewares\ValidateJsonContent;
use Apiato\Foundation\Support\PathHelper;
use Composer\Autoload\ClassLoader;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Route;
use Symfony\Component\Finder\SplFileInfo;

final class Apiato
{
private static string $basePath;
private static array $providerPaths = [];
private static array $configPaths = [];
private static array $listenerPaths = [];
private static array $commandPaths = [];
private static self $instance;
private array $providerPaths = [];
private array $configPaths = [];
private array $listenerPaths = [];
private array $commandPaths = [];
private array $helperPaths = [];
private Localization $localization;

private function __construct(
private string $basePath,
) {
}

public static function configure(string $basePath): ApplicationBuilder
public static function configure(string|null $basePath = null): ApplicationBuilder
{
self::$basePath = $basePath;
if (isset(self::$instance)) {
return new ApplicationBuilder(self::$instance);
}

$basePath = match (true) {
is_string($basePath) => $basePath,
default => self::inferBasePath(),
};

self::$instance = new self($basePath);

return (new ApplicationBuilder())
return (new ApplicationBuilder(self::$instance))
->withProviders(
$basePath . '/app/Ship/Providers',
...glob($basePath . '/app/Containers/*/*/Providers', GLOB_ONLYDIR | GLOB_NOSORT),
Expand All @@ -36,45 +55,95 @@ public static function configure(string $basePath): ApplicationBuilder
)->withCommands(
$basePath . '/app/Ship/Commands',
...glob($basePath . '/app/Containers/*/*/UI/Console', GLOB_ONLYDIR | GLOB_NOSORT),
);
)->withHelpers(
$basePath . '/app/Ship/Helpers',
)->withTranslations();
}

/**
* Infer the application's base directory from the environment.
*/
public static function inferBasePath(): string
{
return match (true) {
isset($_ENV['APP_BASE_PATH']) => $_ENV['APP_BASE_PATH'],
default => dirname(array_keys(ClassLoader::getRegisteredLoaders())[0]),
};
}

public static function instance(): self
{
return self::$instance;
}

public function withProviders(string ...$path): self
{
$this->providerPaths = $path;

return $this;
}

public function withTranslations(callable|null $callback = null): self
{
$this->localization = (new Localization())
->loadTranslationsFrom(
$this->basePath . '/app/Ship/Languages',
...glob($this->basePath . '/app/Containers/*/*/Languages', GLOB_ONLYDIR | GLOB_NOSORT),
);

if (!is_null($callback)) {
$callback($this->localization);
}

return $this;
}

public function withConfigs(string ...$path): void
{
$this->configPaths = $path;
}

public function withEvents(string ...$path): void
{
$this->listenerPaths = $path;
}

public static function loadProvidersFrom(string ...$path): void
public function withCommands(string ...$path): void
{
self::$providerPaths = $path;
$this->commandPaths = $path;
}

public static function loadConfigsFrom(string ...$path): void
public function withHelpers(string ...$path): void
{
self::$configPaths = $path;
$this->helperPaths = $path;
}

public static function loadEventsFrom(string ...$path): void
public function providerPaths(): array
{
self::$listenerPaths = $path;
return $this->providerPaths;
}

public static function loadCommandsFrom(string ...$path): void
public function configPaths(): array
{
self::$commandPaths = $path;
return $this->configPaths;
}

public static function providerPaths(): array
public function helperPaths(): array
{
return self::$providerPaths;
return $this->helperPaths;
}

public static function configPaths(): array
public function localization(): Localization
{
return self::$configPaths;
return $this->localization;
}

public static function getListeners(): array
public function events(): array
{
return self::$listenerPaths;
return $this->listenerPaths;
}

public static function getApiMiddlewares(): array
public function apiMiddlewares(): array
{
return [
ValidateJsonContent::class,
Expand All @@ -83,9 +152,9 @@ public static function getApiMiddlewares(): array
];
}

public static function getCommands(): array
public function commands(): array
{
return self::$commandPaths;
return $this->commandPaths;
}

// TODO: separate Api and Web route registration
Expand Down
60 changes: 60 additions & 0 deletions src/Foundation/Configuration/ApplicationBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Apiato\Foundation\Configuration;

use Apiato\Foundation\Apiato;

final readonly class ApplicationBuilder
{
public function __construct(
private Apiato $apiato,
) {
}

public function withProviders(string ...$path): self
{
$this->apiato->withProviders(...$path);

return $this;
}

public function withConfigs(string ...$path): self
{
$this->apiato->withConfigs(...$path);

return $this;
}

public function withEvents(string ...$path): self
{
$this->apiato->withEvents(...$path);

return $this;
}

public function withCommands(string ...$path): self
{
$this->apiato->withCommands(...$path);

return $this;
}

public function withHelpers(string ...$path): self
{
$this->apiato->withHelpers(...$path);

return $this;
}

public function withTranslations(callable|null $callback = null): self
{
$this->apiato->withTranslations($callback);

return $this;
}

public function create(): Apiato
{
return $this->apiato;
}
}
52 changes: 52 additions & 0 deletions src/Foundation/Configuration/Localization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Apiato\Foundation\Configuration;

use Apiato\Foundation\Support\PathHelper;
use Illuminate\Support\Str;

final class Localization
{
protected static \Closure $namespaceBuilder;
protected array $paths = [];

public function __construct()
{
$this->buildNamespaceUsing(function ($path): string {
if (Str::contains($path, PathHelper::getSharedDirectoryPath())) {
return Str::camel(PathHelper::getSharedDirectoryName());
}

return Str::of($path)
->after(PathHelper::getContainersDirectoryName() . DIRECTORY_SEPARATOR)
->explode(DIRECTORY_SEPARATOR)
->take(2)
->map(static fn ($part) => Str::camel($part))
->implode('@');
});
}

public function buildNamespaceUsing(callable $callback): self
{
self::$namespaceBuilder = $callback;

return $this;
}

public function paths(): array
{
return $this->paths;
}

public function loadTranslationsFrom(string ...$paths): self
{
$this->paths = $paths;

return $this;
}

public function buildNamespaceFor(string $path): string
{
return app()->call(self::$namespaceBuilder, compact('path'));
}
}
54 changes: 0 additions & 54 deletions src/Foundation/Loaders/ApplicationBuilder.php

This file was deleted.

29 changes: 29 additions & 0 deletions src/Foundation/Loaders/HelperLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Apiato\Foundation\Loaders;

use Apiato\Foundation\Apiato;
use Illuminate\Support\Facades\File;

final readonly class HelperLoader implements Loader
{
private function __construct(private array $paths)
{
}

public static function create(): self
{
return new self(Apiato::instance()->helperPaths());
}

public function load(): void
{
foreach ($this->paths as $path) {
$files = File::files($path);

foreach ($files as $file) {
require_once $file;
}
}
}
}
Loading

0 comments on commit 4155eef

Please sign in to comment.