Skip to content

Commit 318b4c0

Browse files
committed
Integrated twig template-engine
1 parent 1df155e commit 318b4c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+482
-341
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ trim_trailing_whitespace = true
1616
[*.json]
1717
indent_size = 2
1818
insert_final_newline = ignore
19+
20+
[*.html]
21+
insert_final_newline = ignore

CHANGELOG.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7-
## [1.0.0] - 2017-12-07
8-
- First release;
7+
## [1.1.0] - XXXX-XX-XX
8+
### Added
9+
- Renderers to facilitate integrations of template-engines:
10+
- Added integration to `twig/twig` through `TwigRenderer` class renderer (defined as default);
11+
- Template packs to facilitate customization and extensibility of templates:
12+
- Added template pack `default` and `bootstrap4`.
13+
- Support to configure default renderer and default template pack through `Config` singleton class;
14+
15+
### Changed
16+
- Class name `PHPFormConfig` to `Config` and moved to `src/` directory;
17+
- `BoundField` attribute name `choices` changed to `options`;
18+
- `BoundField` attribute `options` now return an array instead of formated string;
19+
- `Widgets`, `labelTag` and `ErrorList` now render through default renderer instead of formatter `fleshgrinder/format`;
20+
- `CheckboxSelectMultiple` and `RadioSelect` widget wrapped in an unordered list tag instead of previous `div`;
21+
- Method name `getSubWidgets` to `getOptions` in `Widgets` class;
22+
23+
### Removed:
24+
- Method `asUL` from `ErrorList` class;
25+
- `config.php` and `templates.php` files;
26+
- Static method `flatatt` from `Attributes` class;
927

1028
## [1.0.1] - 2017-12-07
1129
### Added
@@ -14,3 +32,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1432
### Fixed
1533
- Fix formatValue method on ChoiceWidget when value is empty or null;
1634
- Fix validate on ChoiceField when value if empty and not required.
35+
36+
## [1.0.0] - 2017-12-07
37+
- First release;

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
},
1616
"require": {
1717
"php": ">=7.0.0",
18-
"fleshgrinder/format": "^1.1"
18+
"fleshgrinder/format": "^1.1",
19+
"twig/twig": "^2.4"
1920
},
2021
"require-dev": {
2122
"phpunit/phpunit": "~6.0",
@@ -38,5 +39,9 @@
3839
"phpcs --standard=phpcs.xml",
3940
"phpunit -c phpunit.xml"
4041
]
42+
},
43+
"config": {
44+
"sort-packages": true,
45+
"optimize-autoloader": true
4146
}
4247
}

config.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
class PHPFormConfig
55
{
66
const MESSAGES_FILE_PATH = 'src/messages.php';
7-
const TEMPLATES_FILE_PATH = 'src/templates.php';
87

98
private static $config = null;
109

@@ -14,7 +13,6 @@ class PHPFormConfig
1413
private function __construct()
1514
{
1615
$this->messages = include static::MESSAGES_FILE_PATH;
17-
$this->templates = include static::TEMPLATES_FILE_PATH;
1816
}
1917

2018
public static function getInstance()

src/Config.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
namespace PHPForm;
3+
4+
use PHPForm\Renderers\TwigRenderer;
5+
6+
class Config extends Singleton
7+
{
8+
/**
9+
* Template packs available
10+
*/
11+
const TEMPLATE_PACKS = array("default", "bootstrap4");
12+
13+
/**
14+
* Directory with templates
15+
* @var string
16+
*/
17+
protected $templates_dir = __DIR__ . '/templates/';
18+
19+
/**
20+
* Template pack defined
21+
* @var string
22+
*/
23+
protected $template_pack = "default";
24+
25+
/**
26+
* Renderer class used to render html content
27+
* @var string
28+
*/
29+
protected $renderer_class = TwigRenderer::class;
30+
31+
/**
32+
* Renderer instance based on $renderer_class.
33+
* @var PHPForm\Renderers\Renderer
34+
*/
35+
private $renderer;
36+
37+
/**
38+
* Templates path accordingly with defined template pack.
39+
* @return string
40+
*/
41+
private function getTemplatesPath()
42+
{
43+
$path = $this->templates_dir . $this->template_pack . "/";
44+
45+
if (!file_exists($path)) {
46+
trigger_error("Template dir '$path' don't exists.", E_USER_ERROR);
47+
}
48+
49+
return $path;
50+
}
51+
52+
/**
53+
* Return renderer class instantiated
54+
* @return PHPForm\Renderers\Renderer
55+
*/
56+
public function getRenderer()
57+
{
58+
if (is_null($this->renderer)) {
59+
$this->renderer = new $this->renderer_class($this->getTemplatesPath());
60+
}
61+
62+
return $this->renderer;
63+
}
64+
65+
/**
66+
* Define template pack
67+
* @param string
68+
*/
69+
public function setTemplatePack($template_pack)
70+
{
71+
if (!in_array($template_pack, self::TEMPLATE_PACKS)) {
72+
$packs = implode(", ", self::TEMPLATE_PACKS);
73+
trigger_error("Template pack '$template_pack' not valid. Available packs are: $packs", E_USER_ERROR);
74+
}
75+
76+
$this->template_pack = $template_pack;
77+
}
78+
}

src/Errors/ErrorList.php

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,27 @@
33

44
use ArrayObject;
55

6-
use Fleshgrinder\Core\Formatter;
7-
8-
use PHPForm\PHPFormConfig;
6+
use PHPForm\Config;
97

108
class ErrorList extends ArrayObject
119
{
10+
const TEMPLATE = "error_list.html";
11+
1212
/**
1313
* Returns the error list rendered as HTML.
1414
*
1515
* @return string
1616
*/
1717
public function __toString()
18-
{
19-
return $this->asUL();
20-
}
21-
22-
/**
23-
* Returns an error dictionary as an unordered list in HTML.
24-
*
25-
* @return string
26-
*/
27-
public function asUL()
2818
{
2919
if (!count($this)) {
3020
return '';
3121
}
3222

33-
$items = [];
34-
$list_item_template = PHPFormConfig::getITemplate("ERRORLIST_ITEM");
35-
36-
foreach ($this as $error) {
37-
$items[] = Formatter::format($list_item_template, array("content" => $error));
38-
}
39-
40-
$list_template = PHPFormConfig::getITemplate("ERRORLIST");
23+
$renderer = Config::getInstance()->getRenderer();
4124

42-
return Formatter::format($list_template, array("items" => implode($items)));
25+
return $renderer->render(self::TEMPLATE, array(
26+
"errors" => $this,
27+
));
4328
}
4429
}

src/Fields/BoundField.php

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
<?php
22
namespace PHPForm\Fields;
33

4-
use Fleshgrinder\Core\Formatter;
5-
6-
use PHPForm\PHPFormConfig;
7-
use PHPForm\Utils\Attributes;
4+
use PHPForm\Config;
85

96
class BoundField
107
{
8+
const TEMPLATE_LABEL = "label.html";
9+
1110
private $form;
1211
private $field;
1312
private $name;
14-
private $subwidgets_cache;
13+
private $options_cache;
1514

1615
public $html_name;
1716
public $help_text;
@@ -55,22 +54,21 @@ public function __get($name)
5554
return $this->getValue();
5655
}
5756

58-
if ($name == 'choices') {
59-
if (!isset($subwidgets_cache)) {
60-
$subwidgets_cache = $this->getSubWidgets();
57+
if ($name == 'options') {
58+
if (!isset($options_cache)) {
59+
$options_cache = $this->getOptions();
6160
}
62-
return $subwidgets_cache;
61+
return $options_cache;
6362
}
6463

65-
return parent::__get($name);
64+
return null;
6665
}
6766

68-
private function getSubWidgets(array $attrs = array())
67+
private function getOptions(array $attrs = array())
6968
{
70-
$widget = $this->field->getWidget();
7169
$attrs = $this->buildWidgetAttrs($attrs);
7270

73-
return $widget->getSubWidgets($this->html_name, $this->getValue(), $attrs);
71+
return $this->field->getWidget()->getOptions($this->html_name, $this->getValue(), $attrs);
7472
}
7573

7674
protected function asWidget($widget = null, array $attrs = array())
@@ -82,28 +80,23 @@ protected function asWidget($widget = null, array $attrs = array())
8280
return $widget->render($this->html_name, $this->getValue(), $attrs);
8381
}
8482

85-
public function labelTag($contents = null, array $attrs = null)
83+
public function labelTag($contents = null, array $attrs = array())
8684
{
8785
$contents = is_null($contents) ? $this->label : $contents;
8886

8987
if (empty($contents)) {
9088
return "";
9189
}
9290

93-
if (!is_null($attrs)) {
94-
$attrs = Attributes::flatatt($attrs);
95-
}
96-
9791
$widget = $this->field->getWidget();
9892

99-
$label_tpl = PHPFormConfig::getITemplate("LABEL");
100-
$label_required_tpl = PHPFormConfig::getITemplate("LABEL_REQUIRED");
93+
$renderer = Config::getInstance()->getRenderer();
10194

102-
return Formatter::format($label_tpl, array(
95+
return $renderer->render(self::TEMPLATE_LABEL, array(
10396
"for" => $widget->buildAutoId($this->html_name),
10497
"attrs" => $attrs,
10598
"contents" => $contents,
106-
"required" => $this->field->isRequired() ? $label_required_tpl : null
99+
"required" => $this->field->isRequired()
107100
));
108101
}
109102

src/Fields/MultipleChoiceField.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
*/
55
namespace PHPForm\Fields;
66

7-
use Fleshgrinder\Core\Formatter;
8-
97
use PHPForm\Exceptions\ValidationError;
108
use PHPForm\PHPFormConfig;
119
use PHPForm\Widgets\SelectMultiple;

src/Forms/Form.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
abstract class Form implements ArrayAccess, Iterator, Countable
1818
{
19-
const PREFIX_TEMPLATE = '{prefix}-{field_name}';
19+
const PREFIX_FORMAT = '%s-%s';
2020
const NON_FIELD_ERRORS = '__all__';
2121

2222
/**
@@ -145,10 +145,7 @@ public function __get(string $name)
145145
public function addPrefix(string $field_name)
146146
{
147147
if (!is_null($this->prefix)) {
148-
return Formatter::format(self::PREFIX_TEMPLATE, array(
149-
"prefix" => $this->prefix,
150-
"field_name" => $field_name
151-
));
148+
return sprintf(static::PREFIX_FORMAT, $this->prefix, $field_name);
152149
}
153150

154151
return $field_name;

src/Renderers/Renderer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Abstract class for renderers definition
4+
*/
5+
namespace PHPForm\Renderers;
6+
7+
abstract class Renderer
8+
{
9+
abstract public function getTemplate($template_name);
10+
abstract public function render($template_name, $context);
11+
}

0 commit comments

Comments
 (0)