function __construct(Config $config, Context $context) { error_reporting(E_ALL); ini_set("display_errors", "1"); $this->context = $context; $this->registerExceptionHandler(); $context->bind(DebugBar::class, $this->debugBar = new StandardDebugBar()); $this->debugBar->setStorage(new FileStorage('/tmp/debugbar_storage')); $context->bind(Config::class, $config); $this->init(); $config->parseDir(path('root')); if (isHttp() && !implicitDev()) { die('Unauthorized for dev!'); } }
function initTwig($file = null) { $dirs = $this->getDirs(); /** * We need to duplicate every dir for proper relative includes ... * * */ if ($file) { $tempDirs = $dirs; foreach ($dirs as $dir) { $partDir = realpath($dir) . path('ds'); $tempDir = $partDir . substr(str_replace('\\', path('ds'), $file), 0, strrpos($file, path('ds'))); if (is_dir($tempDir)) { $tempDirs[] = $tempDir; } } $dirs = array_unique($tempDirs); } $this->twig = new TwigEnv(new Twig_Loader_Chain([new Twig_Loader_Filesystem($dirs), new \Twig_Loader_String()]), ['debug' => dev()]); $this->twig->addExtension(new Twig_Extension_StringLoader()); /** * This should be added to Dev environment Provider. */ $this->twig->addExtension(new Twig_Extension_Debug()); /** * This should be added to Framework/Inter Provider. */ $this->twig->addFunction(new Twig_SimpleFunction('__', function ($key, $data = [], $lang = null) { return __($key, $data, $lang); }, ['is_safe' => ['html']])); /** * This should be added to Framework provider. */ $this->twig->addFunction(new Twig_SimpleFunction('config', function ($text, $default = null) { return config($text, $default); })); /** * This should be added to Framework provider. */ $this->twig->addFunction(new Twig_SimpleFunction('flash', function ($key, $delete = true) { return context()->getOrCreate(Flash::class)->get($key, $delete); })); /** * This should be added to Framework provider. */ $this->twig->addFunction(new Twig_SimpleFunction('url', function ($url, $params = [], $absolute = false) { return context()->get(Router::class)->make($url, $params, $absolute); })); $this->twig->addFunction(new Twig_SimpleFunction('dev', function () { return dev(); })); $this->twig->addFunction(new Twig_SimpleFunction('implicitDev', function () { return implicitDev(); })); $this->twig->addFunction(new Twig_SimpleFunction('prod', function () { return prod(); })); /** * This should be added to Framework provider. */ $this->twig->addFunction(new Twig_SimpleFunction('media', function ($file, $path = null, $relative = true, $base = null) { return media($file, $path, $relative, $base); })); /** * This should be added to Framework provider. */ $this->twig->addFunction(new Twig_SimpleFunction('relativePath', function ($key) { return relativePath($key); })); /** * This should be added to Framework provider. */ $this->twig->addFunction(new Twig_SimpleFunction('select', function ($options, $attributes = [], $valueKey = null) { $select = new Select(); $select->setAttributes($attributes ?? []); foreach ($options as $key => $option) { $select->addOption($valueKey ? $option->id : $key, $valueKey ? $option->{$valueKey} : $option); } return $select; })); /** * This should be added to Framework provider. */ $this->twig->addFilter(new Twig_SimpleFilter('price', function ($price) { if (is_null($price)) { $price = 0.0; } $localeManager = resolve(Locale::class); return number_format($price, 2, $localeManager->getDecimalPoint(), $localeManager->getThousandSeparator()) . ' €'; })); $this->twig->addFilter(new Twig_SimpleFilter('roundPrice', function ($price) { if (is_null($price)) { $price = 0.0; } $localeManager = resolve(Locale::class); return trim((string) number_format($price, 2, $localeManager->getDecimalPoint(), $localeManager->getThousandSeparator()), '0') . ' €'; })); $this->twig->addFilter(new Twig_SimpleFilter('datetime', function ($date) { return (new Carbon($date))->format(resolve(Locale::class)->getDatetimeFormat()); })); $this->twig->addFilter(new Twig_SimpleFilter('date', function ($date) { return (new Carbon($date))->format(resolve(Locale::class)->getDateFormat()); })); $this->twig->getExtension('core')->setDateFormat(resolve(Locale::class)->getDateFormat(), '%d days'); }