Skip to content

Commit 1bf3b41

Browse files
authored
Merge pull request #1 from dipcode-software/feat/twig-integration
Integrated twig template-engine
2 parents 1df155e + 0b1cb2e commit 1bf3b41

Some content is hidden

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

70 files changed

+542
-352
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: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,30 @@ 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 `Renderer` interface;
11+
- Added `TwigRenderer` that integrates `twig/twig`;
12+
- Added fallback template loading support.
13+
- Template packs to facilitate customization and extensibility of templates:
14+
- Added template pack `default` and defined as fallback;
15+
- Added template pack `bootstrap4` that integrates custom elements of Bootstrap v4.0.0-beta.2.
16+
- Added extra arg `label` on method `getContext` of `Widget` class;
17+
- Support to configure renderer and template pack through `Config` singleton class;
18+
19+
### Changed
20+
- Class name `PHPFormConfig` to `Config` and moved to `src/` directory;
21+
- `BoundField` attribute name `choices` changed to `options`;
22+
- `BoundField` attribute `options` now return an array instead of formated string;
23+
- `Widgets`, `labelTag` and `ErrorList` now render through default renderer instead of formatter `fleshgrinder/format`;
24+
- `CheckboxSelectMultiple` and `RadioSelect` widget wrapped in an unordered list tag instead of previous `div`;
25+
- Method name `getSubWidgets` to `getOptions` in `Widgets` class;
26+
27+
### Removed:
28+
- Method `asUL` from `ErrorList` class;
29+
- `config.php` and `templates.php` files;
30+
- Static method `flatatt` from `Attributes` class;
931

1032
## [1.0.1] - 2017-12-07
1133
### Added
@@ -14,3 +36,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1436
### Fixed
1537
- Fix formatValue method on ChoiceWidget when value is empty or null;
1638
- Fix validate on ChoiceField when value if empty and not required.
39+
40+
## [1.0.0] - 2017-12-07
41+
- 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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
namespace PHPForm;
3+
4+
use PHPForm\Renderers\TwigRenderer;
5+
6+
class Config extends Singleton
7+
{
8+
/**
9+
* @var string Directory with templates
10+
*/
11+
protected $templates_dir = __DIR__ . '/templates/';
12+
13+
/**
14+
* @var string Default fallback template pack
15+
*/
16+
protected $fallback_template_pack = "default";
17+
18+
/**
19+
* @var string Default template pack defined. If null, fallback is used.
20+
*/
21+
protected $template_pack = null;
22+
23+
/**
24+
* @var string Renderer class used to render html content
25+
*/
26+
protected $renderer_class = TwigRenderer::class;
27+
28+
/**
29+
* @var PHPForm\Renderers\Renderer Renderer instance based on $renderer_class.
30+
*/
31+
private $renderer;
32+
33+
/**
34+
* Templates path accordingly with defined template pack.
35+
*
36+
* @return string
37+
*/
38+
private function buildPath($template_pack)
39+
{
40+
$path = $this->templates_dir . $template_pack . "/";
41+
42+
if (!file_exists($path)) {
43+
trigger_error("Template pack dir '$path' don't exists.", E_USER_ERROR);
44+
}
45+
46+
return $path;
47+
}
48+
49+
/**
50+
* Return renderer class instantiated
51+
*
52+
* @return PHPForm\Renderers\Renderer
53+
*/
54+
public function getRenderer()
55+
{
56+
if (is_null($this->renderer)) {
57+
$fallback_pack_dir = $this->buildPath($this->fallback_template_pack);
58+
$pack_dir = !is_null($this->template_pack) ? $this->buildPath($this->template_pack) : null;
59+
60+
$this->renderer = new $this->renderer_class($fallback_pack_dir, $pack_dir);
61+
}
62+
63+
return $this->renderer;
64+
}
65+
66+
/**
67+
* Define template pack
68+
*
69+
* @param string
70+
*/
71+
public function setTemplatePack($template_pack)
72+
{
73+
$this->template_pack = $template_pack;
74+
}
75+
}

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: 16 additions & 23 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())
@@ -79,31 +77,26 @@ protected function asWidget($widget = null, array $attrs = array())
7977

8078
$attrs = $this->buildWidgetAttrs($attrs);
8179

82-
return $widget->render($this->html_name, $this->getValue(), $attrs);
80+
return $widget->render($this->html_name, $this->getValue(), $this->label, $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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* Abstract class for renderers definition
4+
*/
5+
namespace PHPForm\Renderers;
6+
7+
interface Renderer
8+
{
9+
public function __construct(string $fallback_templates_dir, string $templates_dir);
10+
public function getTemplate(string $template_name);
11+
public function render(string $template_name, array $context);
12+
}

0 commit comments

Comments
 (0)