Skip to content
This repository has been archived by the owner on Dec 27, 2018. It is now read-only.

Commit

Permalink
[WIP] Refactoring of the internal templates' logic (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Nov 8, 2018
1 parent 58abd54 commit e660fc2
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.8.1
2.8.2
14 changes: 9 additions & 5 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ class Config
'layouts' => [
'dir' => 'layouts',
'internal' => [
'redirect.html' => '',
'robots.txt' => '',
'sitemap.xml' => '',
'googleanalytics.js' => 'includes/',
'rss.xml' => '',
'dir' => 'res/layouts',
],
],
'output' => [
Expand Down Expand Up @@ -320,6 +316,14 @@ public function getLayoutsPath()
return $this->getSourceDir().'/'.$this->get('layouts.dir');
}

/**
* @return string
*/
public function getInternalLayoutsPath()
{
return __DIR__.'/../'.$this->get('layouts.internal.dir');
}

/**
* @return string
*/
Expand Down
20 changes: 10 additions & 10 deletions src/Renderer/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ class Layout
*/
public function finder(Page $page, Config $config)
{
// internal layouts
if (!empty($page->getLayout()) && array_key_exists($page->getLayout(), $config->get('layouts.internal'))) {
return $page->getLayout().'.twig';
}

$layout = 'unknown';

// what layouts could be use for the page?
$layouts = self::fallback($page);

// is layout exists in local layout dir?
// take the first available layout
foreach ($layouts as $layout) {
// is it in layouts dir?
if (Util::getFS()->exists($config->getLayoutsPath().'/'.$layout)) {
return $layout;
}
}
// is layout exists in layout theme dir?
if ($config->hasTheme()) {
foreach ($layouts as $layout) {
// is it in theme dir?
if ($config->hasTheme()) {
if (Util::getFS()->exists($config->getThemePath($config->get('theme')).'/'.$layout)) {
return $layout;
}
}
// is it in internal dir?
if (Util::getFS()->exists($config->getInternalLayoutsPath().'/'.$layout)) {
return $layout;
}
}

throw new Exception(sprintf("Layout '%s' not found for page '%s'!", $layout, $page->getId()));
Expand Down
13 changes: 1 addition & 12 deletions src/Renderer/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,8 @@ class Twig implements RendererInterface
*/
public function __construct($templatesPath, $config)
{
// internal layouts
$internalLoader = [];
if ($internalLayouts = $config->get('layouts.internal')) {
foreach ($internalLayouts as $layout => $path) {
$layoutContent = file_get_contents(sprintf(__DIR__.'/../../res/layouts/%s.twig', $layout));
$internalLoader[sprintf('%s%s.twig', (($path) ? $path : ''), $layout)] = $layoutContent;
}
}
$loaderArray = new \Twig_Loader_Array($internalLoader);
// project layouts
$loaderFS = new \Twig_Loader_Filesystem($templatesPath);
// load layouts
$loader = new \Twig_Loader_Chain([$loaderFS, $loaderArray]);
$loader = new \Twig_Loader_Filesystem($templatesPath);
// Twig
$this->twig = new \Twig_Environment($loader, [
'debug' => true,
Expand Down
12 changes: 9 additions & 3 deletions src/Step/RenderPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,21 @@ public function init($options)
public function process()
{
// prepares renderer
$this->phpoole->setRenderer(new Twig($this->getLayoutsPaths(), $this->config));
$this->phpoole->setRenderer(new Twig($this->getAllLayoutsPaths(), $this->config));

// add globals variables
$this->addGlobals();

// start rendering
call_user_func_array($this->phpoole->getMessageCb(), ['RENDER', 'Rendering pages']);

// collect published pages
/* @var $page Page */
$filteredPages = $this->phpoole->getPages()->filter(function (Page $page) {
return !empty($page->getVariable('published'));
});
$max = count($filteredPages);

// render each page
$count = 0;
foreach ($filteredPages as $page) {
$count++;
Expand All @@ -78,17 +79,22 @@ public function process()
*
* @return array Layouts directory
*/
protected function getLayoutsPaths()
protected function getAllLayoutsPaths()
{
$paths = [];

// website
if (is_dir($this->config->getLayoutsPath())) {
$paths[] = $this->config->getLayoutsPath();
}
// theme
if ($this->config->hasTheme()) {
$paths[] = $this->config->getThemePath($this->config->get('theme'));
}
// internal
if (is_dir($this->config->getInternalLayoutsPath())) {
$paths[] = $this->config->getInternalLayoutsPath();
}

return $paths;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/website/layouts/robots.txt.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Disallow: /
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

{% block content %}
{{ page.content }}
<quote>Page from theme > layouts > project > page.html.twig</quote>
{% if page.categories is defined %}
<ul>
{% for category in page.categories %}
<a href="{{ url('categories/' ~ category) }}">#{{ category }}</a>&nbsp;
<li><a href="{{ url('categories/' ~ category) }}">#{{ category }}</a></li>
{% endfor %}
</ul>
{% endif %}
<p>layout (themes/theme): {{ _self.getTemplateName() }}</p>
{% endblock %}

0 comments on commit e660fc2

Please sign in to comment.