Skip to content

Commit d5b5cb3

Browse files
committed
Introduce LayoutFactory to better instantiate customized layouts
1 parent f0da217 commit d5b5cb3

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

src/WebApp/Application.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Application {
1414
public $vault;
1515
public $database;
1616
public $dataModel;
17+
public $layoutFactory;
1718
public $serviceFactory;
1819
public $authentication;
1920
public $authorization;
@@ -28,6 +29,7 @@ public function __construct($config) {
2829
$this->request = $this->initRequest();
2930
$this->principal = NULL;
3031
I18N::addI18nFile(__DIR__.'/../i18n.php');
32+
$this->layoutFactory = $this->createLayoutFactory();
3133
}
3234

3335
protected function initRequest() {
@@ -206,6 +208,14 @@ protected function initMailQueue() {
206208
}
207209
}
208210

211+
protected function createLayoutFactory() {
212+
return new LayoutFactory($this);
213+
}
214+
215+
public function getLayoutFactory() {
216+
return $this->layoutFactory;
217+
}
218+
209219
public function getTheme() {
210220
return $this->config->get('theme');
211221
}

src/WebApp/LayoutFactory.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace WebApp;
4+
5+
class LayoutFactory {
6+
7+
protected $app;
8+
protected $layouts;
9+
10+
public function __construct($app) {
11+
$this->app = $app;
12+
$this->layouts = array();
13+
}
14+
15+
public function getLayout($theme, $page) {
16+
$name = $page->getLayoutName();
17+
if ($name == NULL) {
18+
$name = $theme->getLayoutName();
19+
}
20+
if ($name == NULL) {
21+
$name = 'default';
22+
}
23+
24+
if (!isset($this->layouts[$name])) {
25+
$this->layouts[$name] = $this->createLayout($name, $theme, $page);
26+
}
27+
return $this->layouts[$name];
28+
}
29+
30+
/** Descendants shall override this method and fallback on parent implementation */
31+
protected function createLayout($name, $theme, $page) {
32+
$class = new \ReflectionClass($theme);
33+
$className = strpos($name, '\\') === FALSE ? $class->getNamespaceName().'\\'.ucfirst($name).'Layout' : $name;
34+
if (class_exists($className)) {
35+
$className = '\\'.$className;
36+
return new $className($theme, $page);
37+
}
38+
$className = $class->getNamespaceName().'\\DefaultLayout';
39+
if (class_exists($className)) {
40+
$className = '\\'.$className;
41+
return new $className($theme, $page);
42+
}
43+
return NULL;
44+
}
45+
}
46+

src/WebApp/Theme.php

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,8 @@ public function __construct($app, $useCache = TRUE) {
3535
public function render($page) {
3636
$this->page = $page;
3737

38-
// Get the layout of the page
39-
$layoutName = $page->getLayoutName();
40-
41-
// Fallback to default theme layout
42-
if ($layoutName == NULL) {
43-
$layoutName = $this->getLayoutName();
44-
}
45-
46-
// get the layout
47-
$layout = $this->getLayout($layoutName);
38+
// Ask the layout factory
39+
$layout = $this->app->getLayoutFactory()->getLayout($this, $page);
4840

4941
// Fallback to this object
5042
if ($layout == NULL) {
@@ -64,29 +56,10 @@ public function render($page) {
6456
* depending on the current page object.
6557
* @return name of layout (this method returns 'default')
6658
*/
67-
protected function getLayoutName() {
59+
public function getLayoutName() {
6860
return 'default';
6961
}
7062

71-
/**
72-
* Return a layout object for the given name.
73-
* @return the layout object (can be NULL)
74-
*/
75-
protected function getLayout($name) {
76-
if ($name == NULL) $name = 'default';
77-
$className = strpos($name, '\\') === FALSE ? $this->class->getNamespaceName().'\\'.ucfirst($name).'Layout' : $name;
78-
if (class_exists($className)) {
79-
$className = '\\'.$className;
80-
return new $className($this, $this->page);
81-
}
82-
$className = $this->class->getNamespaceName().'\\DefaultLayout';
83-
if (class_exists($className)) {
84-
$className = '\\'.$className;
85-
return new $className($this, $this->page);
86-
}
87-
return NULL;
88-
}
89-
9063
/**
9164
* A very basic rendering page.
9265
* @param object $page - the page to be rendered.

0 commit comments

Comments
 (0)