Skip to content

Instantly share code, notes, and snippets.

@BoShurik
Created December 15, 2017 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BoShurik/6039d85acce522ba3972efd0fc305bd8 to your computer and use it in GitHub Desktop.
Save BoShurik/6039d85acce522ba3972efd0fc305bd8 to your computer and use it in GitHub Desktop.
<?php
namespace App;
use App\DependencyInjection\Configuration;
use App\DependencyInjection\FeedbackLoader;
use App\DependencyInjection\PagesLoader;
use App\DependencyInjection\YamlFileLoader;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
const CONFIG_EXTS = '.{php,xml,yaml,yml}';
public function getCacheDir()
{
return $this->getProjectDir().'/var/cache/'.$this->environment;
}
public function getLogDir()
{
return $this->getProjectDir().'/var/log';
}
public function registerBundles()
{
$contents = require $this->getProjectDir().'/config/bundles.php';
foreach ($contents as $class => $envs) {
if (isset($envs['all']) || isset($envs[$this->environment])) {
yield new $class();
}
}
}
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
{
$container->setParameter('container.autowiring.strict_mode', true);
$container->setParameter('container.dumper.inline_class_loader', true);
$confDir = $this->getProjectDir().'/config';
$loader->load($confDir.'/packages/*'.self::CONFIG_EXTS, 'glob');
if (is_dir($confDir.'/packages/'.$this->environment)) {
$loader->load($confDir.'/packages/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
}
$loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/services/**/*'.self::CONFIG_EXTS, 'glob');
$this->processConfiguration($container);
}
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$confDir = $this->getProjectDir().'/config';
if (is_dir($confDir.'/routes/')) {
$routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, '/', 'glob');
}
if (is_dir($confDir.'/routes/'.$this->environment)) {
$routes->import($confDir.'/routes/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
}
$routes->import($confDir.'/routes'.self::CONFIG_EXTS, '/', 'glob');
}
protected function processConfiguration(ContainerBuilder $container)
{
$config = $this->parseConfig();
$this->processPages($container, $config['pages']);
$this->processFeedback($container, $config['feedback']);
}
protected function processPages(ContainerBuilder $container, array $config)
{
$loader = new PagesLoader();
$loader->load($container, $config);
}
protected function processFeedback(ContainerBuilder $container, array $config)
{
$loader = new FeedbackLoader();
$loader->load($container, $config);
}
protected function parseConfig()
{
$processor = new Processor();
$configuration = new Configuration();
return $processor->processConfiguration($configuration, $this->getAppConfig());
}
protected function getAppConfig()
{
$file = $this->getProjectDir().'/config/config.yaml';
if (!stream_is_local($file)) {
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
}
if (!file_exists($file)) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
}
$parser = new Parser();
try {
return $parser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS);
} catch (ParseException $e) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment