Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial PR for POC in Move to a Twig Loader concept #278 #280

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea

build
composer.lock
vendor
Expand Down
16 changes: 9 additions & 7 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ public function getFileExtension()
public function addFolder($name, $directory, $fallback = false)
{
$this->folders->add($name, $directory, $fallback);

if (method_exists($this->getResolveTemplatePath(), 'addPath')) {
$this->getResolveTemplatePath()->addPath($directory, $name);
}
return $this;
}

Expand Down Expand Up @@ -258,26 +260,26 @@ public function loadExtensions(array $extensions = array())

/**
* Get a template path.
* @param string $name
* @param string|Name $name
* @return string
*/
public function path($name)
{
$name = new Name($this, $name);
$name = $name instanceof Name ? $name : new Name($this, $name);

return $name->getPath();
return $this->getResolveTemplatePath()->resolvePath($name);
}

/**
* Check if a template exists.
* @param string $name
* @param string|Name $name
* @return boolean
*/
public function exists($name)
{
$name = new Name($this, $name);
$name = $name instanceof Name ? $name : new Name($this, $name);

return $name->doesPathExist();
return $this->getResolveTemplatePath()->exists($name);
}

/**
Expand Down
109 changes: 49 additions & 60 deletions src/Template/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
*/
class Name
{
const NAMESPACE_DELIMITER = '::';

/**
* Hint path delimiter value.
*
* @var string
*/
const HINT_PATH_DELIMITER = '::';

/**
* Instance of the template engine.
* @var Engine
Expand All @@ -23,16 +32,17 @@ class Name
protected $name;

/**
* The parsed template folder.
* @var Folder
* The parsed namespace
*/
protected $namespace;

protected $folder;

/**
* The parsed template filename.
* The parsed template path.
* @var string
*/
protected $file;
protected $path;

/**
* Create a new Name instance.
Expand Down Expand Up @@ -75,13 +85,13 @@ public function setName($name)
{
$this->name = $name;

$parts = explode('::', $this->name);
$parts = explode(static::NAMESPACE_DELIMITER, $this->name);

if (count($parts) === 1) {
$this->setFile($parts[0]);
$this->setPath($parts[0]);
} elseif (count($parts) === 2) {
$this->setFolder($parts[0]);
$this->setFile($parts[1]);
$this->setNamespace($parts[0]);
$this->setPath($parts[1]);
} else {
throw new LogicException(
'The template name "' . $this->name . '" is not valid. ' .
Expand All @@ -103,12 +113,12 @@ public function getName()

/**
* Set the parsed template folder.
* @param string $folder
* @param string $namespace
* @return Name
*/
public function setFolder($folder)
public function setNamespace($namespace)
{
$this->folder = $this->engine->getFolders()->get($folder);
$this->namespace = $namespace;

return $this;
}
Expand All @@ -117,64 +127,61 @@ public function setFolder($folder)
* Get the parsed template folder.
* @return string
*/
public function getNamespace()
{
return $this->namespace;
}

/**
* @deprecated
*/
public function getFolder()
{
return $this->folder;
if ($this->getNamespace()) {
return $this->getEngine()->getFolders()->get($this->getNamespace());
}
return null;
}

/**
* Set the parsed template file.
* @param string $file
* @param string $path
* @return Name
*/
public function setFile($file)
public function setPath($path)
{
if ($file === '') {
if ($path === '') {
throw new LogicException(
'The template name "' . $this->name . '" is not valid. ' .
'The template name cannot be empty.'
);
}

$this->file = $file;

if (!is_null($this->engine->getFileExtension())) {
$this->file .= '.' . $this->engine->getFileExtension();
}
$this->path = $path;

return $this;
}

/**
* Get the parsed template file.
* @return string
*/
public function getFile()
{
return $this->file;
$file = $this->path;
if (!is_null($this->getEngine()->getFileExtension())) {
$file .= '.'.$this->getEngine()->getFileExtension();
}
return $file;
}

/**
* Resolve template path.
* Resolve template path or
* Get the parsed template file.
* @return string
*/
public function getPath()
public function getPath($resolve = true )
{
if (is_null($this->folder)) {
return "{$this->getDefaultDirectory()}/{$this->file}";
}

$path = "{$this->folder->getPath()}/{$this->file}";

if (
!is_file($path)
&& $this->folder->getFallback()
&& is_file("{$this->getDefaultDirectory()}/{$this->file}")
) {
$path = "{$this->getDefaultDirectory()}/{$this->file}";
if ($resolve) {
return $this->engine->path($this);
}

return $path;
return $this->path;
}

/**
Expand All @@ -183,24 +190,6 @@ public function getPath()
*/
public function doesPathExist()
{
return is_file($this->getPath());
}

/**
* Get the default templates directory.
* @return string
*/
protected function getDefaultDirectory()
{
$directory = $this->engine->getDirectory();

if (is_null($directory)) {
throw new LogicException(
'The template name "' . $this->name . '" is not valid. '.
'The default directory has not been defined.'
);
}

return $directory;
return $this->engine->exists($this);
}
}
3 changes: 3 additions & 0 deletions src/Template/ResolveTemplatePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ interface ResolveTemplatePath
* @throws TemplateNotFound if the template could not be properly resolved to a file path
*/
public function __invoke(Name $name): string;


public function exists(Name $name): bool;
}
Loading