I got Slim Di Bridge working with flash and the dummy interfaces as displayed in the sample code posted earlier in this post. I don’t think I’m using the bridge to its full extend. Was looking for a fresh pair of eyes to brows the code.
namespace App;
use App\Auth\CsrfGuard;
use App\Basket\Basket;
use App\Database\CapsuleDatabase;
use App\View;
use Carbon\Carbon;
use DI\ContainerBuilder;
use Interop\Container\ContainerInterface;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\UidProcessor;
use Noodlehaus\ConfigInterface;
use Psr\Log\LoggerInterface;
use Slim\Exception\NotFoundException;
use Slim\Interfaces\RouterInterface;
use Slim\Views\TwigExtension;
//Handlers
use App\Handlers\ErrorCustom;
use App\Handlers\NotFoundCustom;
//Helpers
use App\Helpers\Flash;
use App\Helpers\Session;
use App\Helpers\SEmail;
//Interfaces
use App\Interfaces\Auth\CsrfGuardInterface;
use App\Interfaces\Auth\ValidationErrorsMiddleware;
use App\Interfaces\Auth\OldInputMiddleware;
use App\Interfaces\Auth\GuestMiddlewareInterface;
use App\Interfaces\Database\DatabaseInterface;
use App\Interfaces\Helpers\FlashInterface;
use App\Interfaces\Helpers\SessionInterface;
use App\Interfaces\ViewInterface;
//Models
use App\Models\Address;
use App\Models\Customer;
use App\Models\Order;
use App\Models\Payment;
use App\Models\Product;
//Support
use App\Support\Storage\Contracts\StorageInterface;
use App\Support\Storage\SessionStorage;
//Validation
use App\Validation\Contracts\ValidatorInterface;
use App\Validation\Validator;
class App extends \DI\Bridge\Slim\App {
private $config;
private $session;
public function __construct(
ConfigInterface $config
) {
$this->config = $config;
$this->session = new Session;
if (!$this->session->isset1('csrfToken')) {
$this->session->set('csrfToken', password_hash(time(), PASSWORD_BCRYPT));
}
$containerBuilder = new ContainerBuilder;
$this->configureContainer($containerBuilder);
$container = $containerBuilder->build();
parent::__construct($container);
}
protected function configureContainer(ContainerBuilder $builder) {
$definitions = [
'settings.httpVersion' => $this->config
->get('slimConfig.settings.httpVersion'),
'settings.responseChunkSize' => $this->config
->get('slimConfig.settings.responseChunkSize'),
'settings.outputBuffering' => $this->config
->get('slimConfig.settings.outputBuffering'),
'settings.determineRouteBeforeAppMiddleware' => $this->config
->get('slimConfig.settings.determineRouteBeforeAppMiddleware'),
'settings.displayErrorDetails' => $this->config
->get('slimConfig.settings.displayErrorDetails'),
ViewInterface::class => function (ContainerInterface $container) {
$view = new View($container->get('config')->get('twig.path'), [
'cache' => $container->get('config')->get('twig.cache'),
'debug' => $container->get('config')->get('twig.debug'),
]);
$view->addExtension(new TwigExtension(
$container->get('router'), $container->get('request')->getUri()
));
$view->addExtension(new \Twig_Extension_Debug());
$view->addExtension(new \Twig_Extensions_Extension_Intl());
$thisHost = $container->get('request')->getUri()->getScheme() . '://' .
$container->get('request')->getUri()->getHost();
$thisBasePath = $container->get('request')->getUri()->getBasePath();
$thisPath = $container->get('request')->getUri()->getPath();
$thisUri = (string) $container->get('request')->getUri();
$view->getEnvironment()->addGlobal(
'globals', [
'host' => $thisHost,
'basePath' => $thisBasePath,
'path' => $thisPath,
'thisUri' => $thisUri,
'cdn' => $container->get('config')
->get('app.cdn'),
'css' => $container->get('config')
->get('app.css'),
'js' => $container->get('config')
->get('app.js'),
'images' => $container->get('config')
->get('app.images'),
'csrfToken' => $this->session->get('csrfToken'),
]
);
$view->getEnvironment()->addGlobal('flash', $container->get('flash'));
$view->getEnvironment()->addGlobal('basket', $container->get(Basket::class));
return $view;
},
'view' => \DI\get(ViewInterface::class),
'errorHandler' => function (
ViewInterface $view,
ContainerInterface $container,
LoggerInterface $logger
) {
return new ErrorCustom(
$container->get('config'), $view, $logger
);
},
'notFoundHandler' => function (
ViewInterface $view,
ContainerInterface $container,
LoggerInterface $logger
) {
return new NotFoundCustom(
$container->get('config'), $view, $logger
);
},
LoggerInterface::class => function (ContainerInterface $container) {
$logger = new Logger(
$container->get('config')->get('logger.name')
);
$logger->pushProcessor(new UidProcessor());
$streamHandler = new StreamHandler(
$container->get('config')->get('logger.path'), Logger::DEBUG
);
$logger->pushHandler($streamHandler);
return $logger;
},
'logger' => \DI\get(LoggerInterface::class),
RouterInterface::class => function (ContainerInterface $container) {
return $container->get('router');
},
// FlashInterface::class => \DI\object(Flash::class),
FlashInterface::class => function () {
return new Flash();
},
'flash' => \DI\get(FlashInterface::class),
DatabaseInterface::class => function () {
return new CapsuleDatabase();
},
'db' => \DI\get(DatabaseInterface::class),
ConfigInterface::class => function () {
return $this->config;
},
'config' => \DI\get(ConfigInterface::class),
SessionInterface::class => function () {
return $this->session;
},
'session' => \DI\get(SessionInterface::class),
CsrfGuardInterface::class => function () {
$null = null;
return new \App\Auth\CsrfGuard('csrf', $null, function ($request, $response, $next) {
$route = $request->getUri()->getPath();
$parsedBody = $request->getParsedBody();
if (
isset($parsedBody['csrfToken']) && $parsedBody['csrfToken'] === $this->session->get('csrfToken') && in_array($route, [
'ajax/receive', // a route that receives ajax
]
)
) {
return $next($request, $response);
}
throw new \Exception('CSRF Problems on route: ' . $route);
}
);
},
'csrf' => \DI\get(CsrfGuardInterface::class),
ValidatorInterface::class => function (ContainerInterface $container) {
return new Validator;
},
StorageInterface::class => function (ContainerInterface $container) {
return new SessionStorage('cart');
},
OldInputMiddlewareInterface::class => function (ContainerInterface $container) {
return new OldInputMiddleware();
},
ValidationErrorsMiddlewareInterface::class => function (ContainerInterface $container) {
return new ValidationErrorsMiddleware();
},
GuestMiddlewareInterface::class => function (ContainerInterface $container) {
return new GuestMiddleware();
},
Product::class => function (ContainerInterface $container) {
return new Product;
},
Order::class => function (ContainerInterface $container) {
return new Order;
},
Customer::class => function (ContainerInterface $container) {
return new Customer;
},
Address::class => function (ContainerInterface $container) {
return new Address;
},
Payment::class => function (ContainerInterface $container) {
return new Payment;
},
SEmail::class => function (ContainerInterface $container) {
return new SEmail();
},
Basket::class => function (ContainerInterface $container) {
return new Basket(
$container->get(SessionStorage::class), $container->get(Product::class)
);
},
];
$builder->addDefinitions($definitions);
}
}