Skip to content

Commit

Permalink
Add a test for mixin call dump with an outer node
Browse files Browse the repository at this point in the history
split: b754ddb6648cd7dce85d24e5bcfb69e3a8c4bb6d
  • Loading branch information
kylekatarnls committed Nov 25, 2019
1 parent 1a59469 commit 7888794
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 11 deletions.
46 changes: 40 additions & 6 deletions Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
// Nodes
use Phug\Compiler\NodeCompiler\YieldNodeCompiler;
use Phug\Compiler\NodeCompilerInterface;
use Phug\Compiler\NormalizerInterface;
use Phug\Compiler\Util\YieldHandlerTrait;
use Phug\Compiler\WithUpperLocatorInterface;
use Phug\Formatter\AbstractElement;
use Phug\Formatter\Element\TextElement;
use Phug\Formatter\ElementInterface;
Expand Down Expand Up @@ -75,7 +77,7 @@
use Phug\Util\Partial\ModuleContainerTrait;
use Phug\Util\SourceLocation;

class Compiler implements ModuleContainerInterface, CompilerInterface
class Compiler implements ModuleContainerInterface, CompilerInterface, WithUpperLocatorInterface
{
use ModuleContainerTrait;
use YieldHandlerTrait;
Expand All @@ -95,6 +97,11 @@ class Compiler implements ModuleContainerInterface, CompilerInterface
*/
private $locator;

/**
* @var LocatorInterface|null
*/
private $upperLocator;

/**
* @var string
*/
Expand Down Expand Up @@ -293,6 +300,16 @@ public function setParentCompiler(CompilerInterface $compiler)
return $this;
}

/**
* Set a master locator to use before the internal one.
*
* @param LocatorInterface|null $upperLocator
*/
public function setUpperLocator($upperLocator)
{
$this->upperLocator = $upperLocator;
}

/**
* Locate a file for a given path. Returns null if
* not found.
Expand All @@ -305,12 +322,13 @@ public function setParentCompiler(CompilerInterface $compiler)
public function locate($path, $paths = null)
{
$paths = $paths ?: $this->getOption('paths');
$extensions = $this->getOption('extensions');

return $this->locator->locate(
$path,
$paths,
$this->getOption('extensions')
);
$upperPath = $this->upperLocator
? $this->upperLocator->locate($path, $paths, $extensions)
: null;

return $upperPath ?: $this->locator->locate($path, $paths, $extensions);
}

/**
Expand Down Expand Up @@ -341,6 +359,22 @@ public function resolve($path, $paths = null)
return $resolvePath;
}

/**
* Normalize the string of a relative or absolute path.
*
* @param string $path the path to normalize.
*
* @return string
*/
public function normalizePath($path)
{
if ($this->locator instanceof NormalizerInterface) {
return $this->locator->normalize($path);
}

return $path;
}

/**
* Return the contents for a given file path.
*
Expand Down
36 changes: 31 additions & 5 deletions Compiler/Locator/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,43 @@
namespace Phug\Compiler\Locator;

use Phug\Compiler\LocatorInterface;
use Phug\Compiler\NormalizerInterface;

class FileLocator implements LocatorInterface
class FileLocator implements LocatorInterface, NormalizerInterface
{
private function normalize($path)
public function normalize($path)
{
return rtrim(str_replace('\\', '/', $path), '/');
$path = implode('/', func_get_args());
$paths = explode('/', rtrim(preg_replace('`[\\\\/]+`', '/', $path), '/'));
$chunks = [];

foreach ($this->getConsistentPaths($paths) as $path) {
if ($path === '..' && ($count = count($chunks)) && $chunks[$count - 1] !== '..') {
array_pop($chunks);

continue;
}

$chunks[] = $path;
}

return implode('/', $chunks);
}

private function getConsistentPaths($paths)
{
foreach ($paths as $path) {
if ($path === '.') {
continue;
}

yield $path;
}
}

private function getFullPath($location, $path, $extension)
{
$fullPath = "$location/$path$extension";
$fullPath = $this->normalize($location, $path.$extension);

if (@is_file($fullPath) && is_readable($fullPath)) {
return realpath($fullPath);
Expand All @@ -22,7 +48,7 @@ private function getFullPath($location, $path, $extension)
$length = strlen($extension);

if ($length && substr($path, -$length) === $extension &&
@is_file($fullPath = "$location/$path") && is_readable($fullPath)
@is_file($fullPath = $this->normalize($location, $path)) && is_readable($fullPath)
) {
return realpath($fullPath);
}
Expand Down
20 changes: 20 additions & 0 deletions Compiler/NormalizerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Phug\Compiler;

/**
* Interface NormalizerInterface.
*
* An interface for paths normalization.
*/
interface NormalizerInterface
{
/**
* Normalize the string of a relative or absolute path.
*
* @param string $path the path to normalize.
*
* @return string
*/
public function normalize($path);
}
18 changes: 18 additions & 0 deletions Compiler/WithUpperLocatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Phug\Compiler;

/**
* Interface WithUpperLocatorInterface.
*
* An interface for object than can have an upper locator.
*/
interface WithUpperLocatorInterface
{
/**
* Set a master locator to use before the internal one.
*
* @param LocatorInterface|null $upperLocator
*/
public function setUpperLocator($upperLocator);
}

0 comments on commit 7888794

Please sign in to comment.