Skip to content
This repository was archived by the owner on Sep 5, 2019. It is now read-only.
Open

V2 #69

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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
/humbuglog.txt
/infection.log
/.idea
.phpunit.result.cache
58 changes: 44 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ That's it.

This is a tiny example:

$nf = new NodeFactory('utf-8');
$nf = new NodeFactory(new DefaultEscaper('utf-8'), new UriValidator());

function getItems use ($nf)()
{
Expand Down Expand Up @@ -110,7 +110,8 @@ by default.

The NodeFactory class is the pivot of NoTeePHP.

$nf = new NodeFactory('utf-8'); // using the right encoding is security relevant
// using the right encoding is security relevant
$nf = new NodeFactory(new DefaultEscaper('utf-8'), new UriValidator());

### Node creation

Expand All @@ -129,24 +130,53 @@ The NodeFactory class is the pivot of NoTeePHP.

### Events

You can register events globally. Lets imagine you want to add an xsrf-token to every form:
If you need to modify the node tree in some cases, you can can register a subscriber to the node factory:

$nf->onTag('form', function(array $attributes, array $children) use ($nf) {
$children[] = $nf->input(['type' => 'hidden', 'name' => 'xsrf-token', 'value' => '12345']);
return [$attributes, $children];
});

The following events are available:

- onTag
- onAttr
- onClass
$nf = new NodeFactory(new DefaultEscaper('utf-8'), new UriValidator());

class CsrfTokenAdder implements SubscriberInterface
{
public function notify(NodeFactory $nodeFactory, DefaultNode $node): DefaultNode
{
if ($node->getTagName() !== 'form') return;
$attributes = $node->getAttributes();
$method = strtolower($attributes['method'] ?? 'get');
$children = $node->getChildren();
if (!isSecure($method)) {
$children[] = $nodeFactory->input(['type' => 'hidden', 'name' => 'csrf_token', 'value' => getCsrfToken()])
}
return new DefaultNode(
$node->getTagName(),
$node->getEscaper(),
$attributes,
$children
);
}
}

$nf->subscribe(new CsrfTokenAdder());

### Debugging

If you need to know, where a specific node is coming from, enable debug mode for the NodeFactory:

$nf = new NodeFactory('utf-8', true);
$nf = new NodeFactory(new DefaultEscaper('utf-8'), new UriValidator(), true);

Now every for every generated html node, an attribute "data-source" will contain the source file and line of the node.
You should disable debug mode in production.

### Short Syntax

You can also use a shorter syntax that relies on global state. As global state is ugly and not recommended in normal
cases, this feature is not enabled by default. To enable it, you need to manually include the file `global.php`.

global $noTeePHP; // this global variable is require for including global.php
$noTeePHP = new NodeFactory(new DefaultEscaper('utf-8'), new UriValidator());
require '{pathToVendorDir}/mschop/NoTeePHP/global.php';

Now you can create nodes by simply calling function (which is shorter to write):

_div(
['id' => 'some-id'],
_a(['href' => 'https://packagist.org/mschop/NoTeePHP', 'Link to the best lib on the world'])
)
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"keywords": ["html", "template", "dom", "query", "api"],
"license": "MIT",
"require": {
"php": ">=7.0",
"php": "~7.4",
"vdb/uri": "~0.2"
},
"require-dev": {
"satooshi/php-coveralls": "~2.0",
"phpunit/phpunit": "~4.8",
"humbug/humbug": "dev-master"
"php-coveralls/php-coveralls": "~2.0",
"infection/infection": "^0.13.4",
"phpunit/phpunit": "^8.3"
},
"authors": [
{
Expand Down
Loading