install symfony demo with installer
This commit is contained in:
65
app/src/Twig/AppExtension.php
Normal file
65
app/src/Twig/AppExtension.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use Symfony\Component\Intl\Locales;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
/**
|
||||
* See https://symfony.com/doc/current/templating/twig_extension.html.
|
||||
*
|
||||
* @author Ryan Weaver <weaverryan@gmail.com>
|
||||
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
|
||||
* @author Julien ITARD <julienitard@gmail.com>
|
||||
*/
|
||||
class AppExtension extends AbstractExtension
|
||||
{
|
||||
private $localeCodes;
|
||||
private $locales;
|
||||
|
||||
public function __construct(string $locales)
|
||||
{
|
||||
$localeCodes = explode('|', $locales);
|
||||
sort($localeCodes);
|
||||
$this->localeCodes = $localeCodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('locales', [$this, 'getLocales']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the list of codes of the locales (languages) enabled in the
|
||||
* application and returns an array with the name of each locale written
|
||||
* in its own language (e.g. English, Français, Español, etc.).
|
||||
*/
|
||||
public function getLocales(): array
|
||||
{
|
||||
if (null !== $this->locales) {
|
||||
return $this->locales;
|
||||
}
|
||||
|
||||
$this->locales = [];
|
||||
foreach ($this->localeCodes as $localeCode) {
|
||||
$this->locales[] = ['code' => $localeCode, 'name' => Locales::getName($localeCode, $localeCode)];
|
||||
}
|
||||
|
||||
return $this->locales;
|
||||
}
|
||||
}
|
137
app/src/Twig/SourceCodeExtension.php
Normal file
137
app/src/Twig/SourceCodeExtension.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use function Symfony\Component\String\u;
|
||||
use Twig\Environment;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\Template;
|
||||
use Twig\TemplateWrapper;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
/**
|
||||
* CAUTION: this is an extremely advanced Twig extension. It's used to get the
|
||||
* source code of the controller and the template used to render the current
|
||||
* page. If you are starting with Symfony, don't look at this code and consider
|
||||
* studying instead the code of the src/App/Twig/AppExtension.php extension.
|
||||
*
|
||||
* @author Ryan Weaver <weaverryan@gmail.com>
|
||||
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
|
||||
*/
|
||||
class SourceCodeExtension extends AbstractExtension
|
||||
{
|
||||
private $controller;
|
||||
|
||||
public function setController(?callable $controller)
|
||||
{
|
||||
$this->controller = $controller;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('show_source_code', [$this, 'showSourceCode'], ['is_safe' => ['html'], 'needs_environment' => true]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|TemplateWrapper|array $template
|
||||
*/
|
||||
public function showSourceCode(Environment $twig, $template): string
|
||||
{
|
||||
return $twig->render('debug/source_code.html.twig', [
|
||||
'controller' => $this->getController(),
|
||||
'template' => $this->getTemplateSource($twig->resolveTemplate($template)),
|
||||
]);
|
||||
}
|
||||
|
||||
private function getController(): ?array
|
||||
{
|
||||
// this happens for example for exceptions (404 errors, etc.)
|
||||
if (null === $this->controller) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$method = $this->getCallableReflector($this->controller);
|
||||
|
||||
$classCode = file($method->getFileName());
|
||||
$methodCode = \array_slice($classCode, $method->getStartLine() - 1, $method->getEndLine() - $method->getStartLine() + 1);
|
||||
$controllerCode = ' '.$method->getDocComment()."\n".implode('', $methodCode);
|
||||
|
||||
return [
|
||||
'file_path' => $method->getFileName(),
|
||||
'starting_line' => $method->getStartLine(),
|
||||
'source_code' => $this->unindentCode($controllerCode),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a reflector for a callable.
|
||||
*
|
||||
* This logic is copied from Symfony\Component\HttpKernel\Controller\ControllerResolver::getArguments
|
||||
*/
|
||||
private function getCallableReflector(callable $callable): \ReflectionFunctionAbstract
|
||||
{
|
||||
if (\is_array($callable)) {
|
||||
return new \ReflectionMethod($callable[0], $callable[1]);
|
||||
}
|
||||
|
||||
if (\is_object($callable) && !$callable instanceof \Closure) {
|
||||
$r = new \ReflectionObject($callable);
|
||||
|
||||
return $r->getMethod('__invoke');
|
||||
}
|
||||
|
||||
return new \ReflectionFunction($callable);
|
||||
}
|
||||
|
||||
private function getTemplateSource(TemplateWrapper $template): array
|
||||
{
|
||||
$templateSource = $template->getSourceContext();
|
||||
|
||||
return [
|
||||
// Twig templates are not always stored in files (they can be stored
|
||||
// in a database for example). However, for the needs of the Symfony
|
||||
// Demo app, we consider that all templates are stored in files and
|
||||
// that their file paths can be obtained through the source context.
|
||||
'file_path' => $templateSource->getPath(),
|
||||
'starting_line' => 1,
|
||||
'source_code' => $templateSource->getCode(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method that "unindents" the given $code when all its lines start
|
||||
* with a tabulation of four white spaces.
|
||||
*/
|
||||
private function unindentCode(string $code): string
|
||||
{
|
||||
$codeLines = u($code)->split("\n");
|
||||
|
||||
$indentedOrBlankLines = array_filter($codeLines, function ($lineOfCode) {
|
||||
return u($lineOfCode)->isEmpty() || u($lineOfCode)->startsWith(' ');
|
||||
});
|
||||
|
||||
$codeIsIndented = \count($indentedOrBlankLines) === \count($codeLines);
|
||||
if ($codeIsIndented) {
|
||||
$unindentedLines = array_map(function ($lineOfCode) {
|
||||
return u($lineOfCode)->after(' ');
|
||||
}, $codeLines);
|
||||
$code = u("\n")->join($unindentedLines);
|
||||
}
|
||||
|
||||
return $code;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user