Move factory classes to a subfolder
This commit is contained in:
parent
123a6c5ad9
commit
0a220d4d8e
9 changed files with 21 additions and 17 deletions
45
classes/Factory/ConfigFactory.php
Normal file
45
classes/Factory/ConfigFactory.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Alltube\Factory;
|
||||
|
||||
use Alltube\Config;
|
||||
use Alltube\Exception\ConfigException;
|
||||
use Alltube\UglyRouter;
|
||||
use Slim\Container;
|
||||
use Symfony\Component\ErrorHandler\Debug;
|
||||
|
||||
/**
|
||||
* Class ConfigFactory
|
||||
* @package Alltube
|
||||
*/
|
||||
class ConfigFactory
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Container $container
|
||||
* @return Config
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public static function create(Container $container)
|
||||
{
|
||||
$configPath = __DIR__ . '/../../config/config.yml';
|
||||
if (is_file($configPath)) {
|
||||
$config = Config::fromFile($configPath);
|
||||
} else {
|
||||
$config = new Config();
|
||||
}
|
||||
if ($config->uglyUrls) {
|
||||
$container['router'] = new UglyRouter();
|
||||
}
|
||||
if ($config->debug) {
|
||||
/*
|
||||
We want to enable this as soon as possible,
|
||||
in order to catch errors that are thrown
|
||||
before the Slim error handler is ready.
|
||||
*/
|
||||
Debug::enable();
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
}
|
27
classes/Factory/LocaleManagerFactory.php
Normal file
27
classes/Factory/LocaleManagerFactory.php
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Alltube\Factory;
|
||||
|
||||
use Alltube\Exception\DependencyException;
|
||||
use Alltube\LocaleManager;
|
||||
|
||||
/**
|
||||
* Class LocaleManagerFactory
|
||||
* @package Alltube
|
||||
*/
|
||||
class LocaleManagerFactory
|
||||
{
|
||||
|
||||
/**
|
||||
* @return LocaleManager|null
|
||||
* @throws DependencyException
|
||||
*/
|
||||
public static function create()
|
||||
{
|
||||
if (!class_exists('Locale')) {
|
||||
throw new DependencyException('You need to install the intl extension for PHP.');
|
||||
}
|
||||
|
||||
return new LocaleManager();
|
||||
}
|
||||
}
|
35
classes/Factory/LoggerFactory.php
Normal file
35
classes/Factory/LoggerFactory.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Alltube\Factory;
|
||||
|
||||
use Consolidation\Log\Logger;
|
||||
use Consolidation\Log\LogOutputStyler;
|
||||
use Slim\Container;
|
||||
use Symfony\Component\Console\Output\ConsoleOutput;
|
||||
|
||||
/**
|
||||
* Class LoggerFactory
|
||||
* @package Alltube
|
||||
*/
|
||||
class LoggerFactory
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Container $container
|
||||
* @return Logger
|
||||
*/
|
||||
public static function create(Container $container)
|
||||
{
|
||||
$config = $container->get('config');
|
||||
if ($config->debug) {
|
||||
$verbosity = ConsoleOutput::VERBOSITY_DEBUG;
|
||||
} else {
|
||||
$verbosity = ConsoleOutput::VERBOSITY_NORMAL;
|
||||
}
|
||||
|
||||
$logger = new Logger(new ConsoleOutput($verbosity));
|
||||
$logger->setLogOutputStyler(new LogOutputStyler());
|
||||
|
||||
return $logger;
|
||||
}
|
||||
}
|
68
classes/Factory/ViewFactory.php
Normal file
68
classes/Factory/ViewFactory.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* ViewFactory class.
|
||||
*/
|
||||
|
||||
namespace Alltube\Factory;
|
||||
|
||||
use Alltube\LocaleManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Uri;
|
||||
use Slim\Views\Smarty;
|
||||
use Slim\Views\SmartyPlugins;
|
||||
use SmartyException;
|
||||
|
||||
/**
|
||||
* Create Smarty view object.
|
||||
*/
|
||||
class ViewFactory
|
||||
{
|
||||
/**
|
||||
* Create Smarty view object.
|
||||
*
|
||||
* @param ContainerInterface $container Slim dependency container
|
||||
* @param Request|null $request PSR-7 request
|
||||
*
|
||||
* @return Smarty
|
||||
* @throws SmartyException
|
||||
*/
|
||||
public static function create(ContainerInterface $container, Request $request = null)
|
||||
{
|
||||
if (!isset($request)) {
|
||||
$request = $container->get('request');
|
||||
}
|
||||
|
||||
$view = new Smarty(__DIR__ . '/../../templates/');
|
||||
|
||||
/** @var Uri $uri */
|
||||
$uri = $request->getUri();
|
||||
if (in_array('https', $request->getHeader('X-Forwarded-Proto'))) {
|
||||
$uri = $uri->withScheme('https')->withPort(443);
|
||||
}
|
||||
|
||||
// set values from X-Forwarded-* headers
|
||||
if ($host = current($request->getHeader('X-Forwarded-Host'))) {
|
||||
$uri = $uri->withHost($host);
|
||||
}
|
||||
|
||||
if ($port = current($request->getHeader('X-Forwarded-Port'))) {
|
||||
$uri = $uri->withPort(intVal($port));
|
||||
}
|
||||
|
||||
if ($path = current($request->getHeader('X-Forwarded-Path'))) {
|
||||
$uri = $uri->withBasePath($path);
|
||||
}
|
||||
|
||||
/** @var LocaleManager $localeManager */
|
||||
$localeManager = $container->get('locale');
|
||||
|
||||
$smartyPlugins = new SmartyPlugins($container->get('router'), $uri->withUserInfo(''));
|
||||
$view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
|
||||
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
|
||||
$view->registerPlugin('block', 't', [$localeManager, 'smartyTranslate']);
|
||||
|
||||
return $view;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue