Skip to content

Commit

Permalink
Last minutes changes
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienlucas committed Aug 26, 2020
1 parent 486a880 commit 9b61307
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 37 deletions.
12 changes: 4 additions & 8 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@
*/
class Plugin implements PluginEntryPointInterface
{
/**
* @var string|null
*/
public static $twig_cache_path;

/**
* @return string[]
*/
Expand Down Expand Up @@ -88,16 +83,17 @@ public function __invoke(RegistrationInterface $api, SimpleXMLElement $config =
}

if (isset($config->twigCachePath)) {
static::$twig_cache_path = getcwd().DIRECTORY_SEPARATOR.ltrim((string) $config->twigCachePath, DIRECTORY_SEPARATOR);
if (!is_dir(static::$twig_cache_path) || !is_readable(static::$twig_cache_path)) {
throw new ConfigException(sprintf('The twig directory %s is missing or not readable.', static::$twig_cache_path));
$twig_cache_path = getcwd().DIRECTORY_SEPARATOR.ltrim((string) $config->twigCachePath, DIRECTORY_SEPARATOR);
if (!is_dir($twig_cache_path) || !is_readable($twig_cache_path)) {
throw new ConfigException(sprintf('The twig directory %s is missing or not readable.', $twig_cache_path));
}

require_once __DIR__.'/Twig/CachedTemplatesTainter.php';
$api->registerHooksFromClass(CachedTemplatesTainter::class);

require_once __DIR__.'/Twig/CachedTemplatesMapping.php';
$api->registerHooksFromClass(CachedTemplatesMapping::class);
CachedTemplatesMapping::setCachePath($twig_cache_path);
}

require_once __DIR__.'/Twig/AnalyzedTemplatesTainter.php';
Expand Down
12 changes: 0 additions & 12 deletions src/Stubs/Response.stubphp

This file was deleted.

28 changes: 15 additions & 13 deletions src/Test/CodeceptionModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@ class CodeceptionModule extends BaseModule
'default_dir' => 'tests/_run/',
];

public const TWIG_TEMPLATES_DIR = 'templates';

/**
* @Given I have the following :templateName template :code
*/
public function haveTheFollowingTemplate(string $templateName, string $code): void
{
$rootDirectory = rtrim($this->config['default_dir'], '/');
$templateRootDirectory = $rootDirectory.'/templates';
$rootDirectory = rtrim($this->config['default_dir'], DIRECTORY_SEPARATOR);
$templateRootDirectory = $rootDirectory.DIRECTORY_SEPARATOR.self::TWIG_TEMPLATES_DIR;
if (!file_exists($templateRootDirectory)) {
mkdir($templateRootDirectory);
}

file_put_contents(
$templateRootDirectory.'/'.$templateName,
$templateRootDirectory.DIRECTORY_SEPARATOR.$templateName,
$code
);
}
Expand All @@ -49,10 +51,10 @@ public function haveTheFollowingTemplate(string $templateName, string $code): vo
*/
public function haveTheTemplateCompiled(string $templateName, string $cacheDirectory): void
{
$rootDirectory = rtrim($this->config['default_dir'], '/');
$cacheDirectory = $rootDirectory.'/'.ltrim($cacheDirectory, '/');
$rootDirectory = rtrim($this->config['default_dir'], DIRECTORY_SEPARATOR);
$cacheDirectory = $rootDirectory.DIRECTORY_SEPARATOR.ltrim($cacheDirectory, DIRECTORY_SEPARATOR);
if (!file_exists($cacheDirectory)) {
mkdir($cacheDirectory, 0777, true);
mkdir($cacheDirectory, 0755, true);
}

$twigEnvironment = self::getEnvironment($rootDirectory, $cacheDirectory);
Expand Down Expand Up @@ -86,15 +88,13 @@ public function haveADependencySatisfied(string $package, string $versionConstra
}
}

public const TEMPLATE_DIR = 'templates';

public static function getEnvironment(string $rootDirectory, string $cacheDirectory): Environment
private static function getEnvironment(string $rootDirectory, string $cacheDirectory): Environment
{
if (!file_exists($rootDirectory.'/'.self::TEMPLATE_DIR)) {
mkdir($rootDirectory.'/'.self::TEMPLATE_DIR);
if (!file_exists($rootDirectory.DIRECTORY_SEPARATOR.self::TWIG_TEMPLATES_DIR)) {
mkdir($rootDirectory.DIRECTORY_SEPARATOR.self::TWIG_TEMPLATES_DIR);
}

$loader = new FilesystemLoader(self::TEMPLATE_DIR, $rootDirectory);
$loader = new FilesystemLoader(self::TWIG_TEMPLATES_DIR, $rootDirectory);

if (!is_dir($cacheDirectory)) {
throw new InvalidArgumentException(sprintf('The %s twig cache directory does not exist or is not readable.', $cacheDirectory));
Expand All @@ -109,7 +109,9 @@ public static function getEnvironment(string $rootDirectory, string $cacheDirect
'strict_variables' => false,
]);

// the following is a trick to force the twig env to change
// The following is a trick to have a different twig cache hash everytime, preventing collisions from one test to another :
// the extension construction has to be evaled so the class name will change each time,
// making the env calculate a different Twig\Environment::$optionHash (which is partly based on the extension names).
/** @var AbstractExtension $ext */
$ext = eval('use Twig\Extension\AbstractExtension; return new class() extends AbstractExtension {};');
$twigEnvironment->addExtension($ext);
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/AnalyzedTemplatesTainter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Twig\Environment;

/**
* This hook add paths from all taint sources of a `Twig\Environment::render()` call to all taint sinks of the corresponding template.
* This hook adds paths from all taint sources going to a `Twig\Environment::render()` call to all taint sinks of the corresponding template.
* The TemplateFileAnalyzer should be declared in configuration.
*/
class AnalyzedTemplatesTainter implements AfterMethodCallAnalysisInterface
Expand Down
14 changes: 11 additions & 3 deletions src/Twig/CachedTemplatesMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Psalm\Plugin\Hook\AfterFileAnalysisInterface;
use Psalm\StatementsSource;
use Psalm\Storage\FileStorage;
use Psalm\SymfonyPsalmPlugin\Plugin;
use RuntimeException;

/**
Expand All @@ -22,15 +21,19 @@ class CachedTemplatesMapping implements AfterFileAnalysisInterface
*/
private const CACHED_TEMPLATE_HEADER_PATTERN = 'use Twig\\\\Template;\n\n\/\* (@?.+\.twig) \*\/\nclass __TwigTemplate';

/**
* @var string|null
*/
public static $cache_path;

/**
* @var array<string, string>
*/
private static $mapping = [];

public static function afterAnalyzeFile(StatementsSource $statements_source, Context $file_context, FileStorage $file_storage, Codebase $codebase): void
{
$basePath = Plugin::$twig_cache_path;
if (null === $basePath || 0 !== strpos($file_storage->file_path, $basePath)) {
if (null === self::$cache_path || 0 !== strpos($file_storage->file_path, self::$cache_path)) {
return;
}

Expand All @@ -48,6 +51,11 @@ public static function afterAnalyzeFile(StatementsSource $statements_source, Con
self::registerNewCache($cacheClassName, $matchingParts[1]);
}

public static function setCachePath(string $cache_path): void
{
static::$cache_path = $cache_path;
}

private static function registerNewCache(string $cacheClassName, string $templateName): void
{
static::$mapping[$templateName] = $cacheClassName;
Expand Down

0 comments on commit 9b61307

Please sign in to comment.