Skip to content

Commit

Permalink
Load simple service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
puleeno committed Oct 22, 2023
1 parent c77a8ca commit 534ac7c
Show file tree
Hide file tree
Showing 15 changed files with 356 additions and 90 deletions.
4 changes: 3 additions & 1 deletion app/Console/Application.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

namespace App\Console;

use Slim\Console\App;

class Application extends App {
class Application extends App
{
}
8 changes: 7 additions & 1 deletion app/Constracts/ApplicationConstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace App\Constracts;

interface ApplicationConstract
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Interfaces\RouteCollectorProxyInterface;

interface ApplicationConstract extends RouteCollectorProxyInterface, RequestHandlerInterface
{
public function booted();

public function isBooted();
}
146 changes: 142 additions & 4 deletions app/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@

namespace App\Core;

use App\Constracts\ApplicationConstract;
use App\Core\Routing\RouteCollector;
use App\Http\ResponseEmitter\ResponseEmitter;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use PuleenoCMS\Exceptions\InvalidApplicationException;
use Tightenco\Collect\Support\Arr;
use Slim\App;
use Slim\CallableResolver;
use Slim\Factory\ServerRequestCreatorFactory;
Expand All @@ -33,6 +30,12 @@
use Slim\Middleware\RoutingMiddleware;
use Slim\Routing\RouteResolver;
use Slim\Routing\RouteRunner;
use App\Constracts\ApplicationConstract;
use App\Core\Routing\RouteCollector;
use App\Http\ResponseEmitter\ResponseEmitter;
use App\Providers\ServiceProvider;
use App\Core\Log\LogServiceProvider;
use PuleenoCMS\Exceptions\InvalidApplicationException;

use function strtoupper;

Expand All @@ -51,6 +54,19 @@ class Application extends App implements RequestHandlerInterface, ApplicationCon

protected static $instance;

protected $isBooted = false;


/**
* @var \App\Providers\ServiceProvider[]
*/
protected $serviceProviders = [];

/**
* @var boolean[]
*/
protected $loadedProviders = [];

public function __construct(
ResponseFactoryInterface $responseFactory,
?ContainerInterface $container = null,
Expand Down Expand Up @@ -83,6 +99,19 @@ public function __construct(
if (is_null(static::$instance)) {
static::$instance = &$this;
}

// Register base providers
$this->registerBaseServiceProviders();
}

public function booted()
{
$this->isBooted = true;
}

public function isBooted()
{
return $this->isBooted;
}

/**
Expand Down Expand Up @@ -235,4 +264,113 @@ public static function getInstance()
}
return static::$instance;
}


/**
* Get the registered service provider instances if any exist.
*
* @param \App\Providers\ServiceProvider|string $provider
* @return array
*/
public function getProviders($provider)
{
$name = is_string($provider) ? $provider : get_class($provider);

return Arr::where($this->serviceProviders, function ($value) use ($name) {
return $value instanceof $name;
});
}

/**
* Resolve a service provider instance from the class name.
*
* @param string $provider
* @return \App\Providers\ServiceProvider
*/
public function resolveProvider($provider)
{
return new $provider($this);
}

/**
* Mark the given provider as registered.
*
* @param \App\Providers\ServiceProvider $provider
* @return void
*/
protected function markAsRegistered($provider)
{
$this->serviceProviders[] = $provider;

$this->loadedProviders[get_class($provider)] = true;
}

/**
* Get the registered service provider instance if it exists.
*
* @param \App\Providers\ServiceProvider|string $provider
* @return \App\Providers\ServiceProvider|null
*/
public function getProvider($provider)
{
return array_values($this->getProviders($provider))[0] ?? null;
}


/**
* Boot the given service provider.
*
* @param \App\Providers\ServiceProvider $provider
* @return mixed
*/
protected function bootProvider(ServiceProvider $provider)
{
if (method_exists($provider, 'boot')) {
return call_user_func([$provider, 'boot']);
}
}

/**
* Register a service provider with the application.
*
* @param \App\Providers\ServiceProvider|string $provider
* @param bool $force
* @return \App\Providers\ServiceProvider
*/
public function register($provider, $force = false)
{
if (($registered = $this->getProvider($provider)) && ! $force) {
return $registered;
}

// If the given "provider" is a string, we will resolve it, passing in the
// application instance automatically for the developer. This is simply
// a more convenient way of specifying your service provider classes.
if (is_string($provider)) {
$provider = $this->resolveProvider($provider);
}

$provider->register();

$this->markAsRegistered($provider);

// If the application has already booted, we will call this boot method on
// the provider class so it has an opportunity to do its boot logic and
// will be ready for any usage by this developer's application logic.
if ($this->isBooted()) {
$this->bootProvider($provider);
}

return $provider;
}

/**
* Register all of the base service providers.
*
* @return void
*/
protected function registerBaseServiceProviders()
{
$this->register(new LogServiceProvider($this));
}
}
12 changes: 12 additions & 0 deletions app/Core/Cookies/CookieServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Core\Cookies;

use App\Providers\ServiceProvider;

class CookieServiceProvider extends ServiceProvider
{
public function register()
{
}
}
88 changes: 88 additions & 0 deletions app/Core/Log/LogServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace App\Core\Log;

use App\Core\Helper;
use App\Core\Settings\SettingsInterface;
use App\Http\Handlers\HttpErrorHandler;
use App\Http\Handlers\ShutdownHandler;
use App\Providers\ServiceProvider;
use Psr\Container\ContainerInterface;
use ReflectionClass;
use Slim\Factory\ServerRequestCreatorFactory;

class LogServiceProvider extends ServiceProvider
{
protected function setupDashboardEnvironment($isDashboard)
{
$helperRelf = new ReflectionClass(Helper::class);
$isDashboardProperty = $helperRelf->getProperty('isDashboard');
$isDashboardProperty->setAccessible(true);
$isDashboardProperty->setValue($isDashboardProperty, $isDashboard);
$isDashboardProperty->setAccessible(false);
}

protected function setupHttpErrorHandle()
{
/**
* @var ContainerInterface
*/
$container = $this->app->getContainer();

/** @var SettingsInterface $settings */
$settings = $container->get(SettingsInterface::class);

$displayErrorDetails = $settings->get('displayErrorDetails');

// Create Request object from globals
$serverRequestCreator = ServerRequestCreatorFactory::create();
$request = $serverRequestCreator->createServerRequestFromGlobals();

$requestPath = $request->getUri() != null ? $request->getUri()->getPath() : '/';
$isDashboard = $requestPath === $settings->get('admin_prefix', '/dashboard')
|| strpos($requestPath, $settings->get('admin_prefix', '/dashboard') . '/') === 0;

$container->set('is_dashboard', $isDashboard);

$this->setupDashboardEnvironment($isDashboard);

$responseFactory = $this->app->getResponseFactory();
$callableResolver = $this->app->getCallableResolver();
$errorHandler = new HttpErrorHandler($callableResolver, $responseFactory);

// Create Shutdown Handler
$shutdownHandler = new ShutdownHandler($request, $errorHandler, $displayErrorDetails);
register_shutdown_function($shutdownHandler);

return $errorHandler;
}

protected function writeErrorLogs(HttpErrorHandler $errorHandler)
{
/**
* @var ContainerInterface
*/
$container = $this->app->getContainer();
$settings = $container->get(SettingsInterface::class);

$displayErrorDetails = $settings->get('displayErrorDetails');
$logError = $settings->get('logError');
$logErrorDetails = $settings->get('logErrorDetails');

// Add Error Middleware
$errorMiddleware = $this->app->addErrorMiddleware($displayErrorDetails, $logError, $logErrorDetails);
$errorMiddleware->setDefaultErrorHandler($errorHandler);
}

public function register()
{
$this->writeErrorLogs(
$this->setupHttpErrorHandle()
);
}

public function boot()
{
die('zo');
}
}
3 changes: 2 additions & 1 deletion app/Facades/UserServiceFacade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace App\Facades;

use Psr\Container\ContainerInterface;

class UserServiceFacade extends Facade
Expand All @@ -18,4 +19,4 @@ public function __get($name)
{
return $this->container->get(self::FACADE_ACCESSOR);
}
}
}
5 changes: 3 additions & 2 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class Kernel
{
/**
* @var App
* @var \App\Constracts\ApplicationConstract
*/
private $app;

Expand All @@ -31,6 +31,8 @@ public function __construct(App $app)
*/
public function configure()
{
$this->app->booted();

$this->app->addRoutingMiddleware();
$this->app->addErrorMiddleware(true, true, true);

Expand All @@ -51,4 +53,3 @@ private function mapRoutes()
});
}
}

2 changes: 1 addition & 1 deletion app/Http/Middleware/AuthGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res

return $next($request, $response);
}
}
}
20 changes: 0 additions & 20 deletions app/Providers/AbstractServiceProvider.php

This file was deleted.

Loading

0 comments on commit 534ac7c

Please sign in to comment.