Commit b01c5ebd authored by Martin Schophaus's avatar Martin Schophaus
Browse files

Merge branch 'refactoring' into 'master'

Refactor class structure

See merge request !3
parents afdef809 427557bd
Loading
Loading
Loading
Loading
Loading
+336 −254

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ The syntax is just a little different:

    <?php
    
    $nf->div(); // method call
    $nodeFactory->div(); // method call
    // vs.
    _div(); // global function
    
+2 −17
Original line number Diff line number Diff line
@@ -3,21 +3,6 @@ attribute contains the source file and line, the html node is coming from:

`<a href="" data-source="/var/www/template/index.html.php:99">Link</a>`

To enable debug mode, you need to add an additional flag when creating a new Instance of `NoTee\NodeFactory`:
To enable debug mode, you need to pass the `$debug` parameter to NoTee instance creation with value true.

    <?php
    
    require 'vendor/autoload.php';
    
    use NoTee\NodeFactory;
    use NoTee\DefaultEscapingStrategy;
    use NoTee\UriValidator;
    
    $nf = new NodeFactory(
         new DefaultEscapingStrategy('utf-8'),
         new UriValidator(),
         new BlockManager(),
         true, // this tells the NodeFactory to use debug mode
     );
     
     // now all created nodes contain the additional debug attribute
 No newline at end of file
    $noTee = NoTee\NoTee::create('utf-8', [], [], true);
 No newline at end of file
+12 −35
Original line number Diff line number Diff line
@@ -5,43 +5,20 @@ that adds in hidden csrf token to every insecure form.
```
<?php

require 'vendor/autoload.php';

namespace YourNamespace;

use NoTee\NodeFactory;
use NoTee\DefaultEscapingStrategy;
use NoTee\UriValidator;
use NoTee\BlockManager;
use NoTee\SubscriberInterface;

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

$nf->subscribe(new class() implements SubscriberInterface {
    public function notify(NodeFactory $nodeFactory, DefaultNode $node): DefaultNode;
$noTee = NoTee\NoTee::create();
$noTee->getNodeFactory()->subscribe(new class() implements SubscriberInterface {
    public function notify(CreateTagEvent $event): DefaultNode;
    {
        if ($node->getTagName() === 'form' && $this->isMethodSecure($node->getAttributes()['method'] ?? 'GET')) {
            return new DefaultNode(
                $node->getTagName(),
                $nodeFactory->getEscaper(),
                $node->getAttributes(),
                array_merge(
                    $node->getChildren(),
                    [$nodeFactory->input(['type' => 'hidden', 'name' => 'csrf_token', 'value' => 'mytoken'])]
                )
            );
        } else {
            return $node;
        $node = $event->getNode();
        if ($node->getTagName() === 'form') {
            $csrfHiddenInput = $event->getNodeFactory()->input([
                'type' => 'hidden',
                'name' => 'csrf_token',
                'value' => 'secure_token_1234566',
            ]);
            $node = $node->appendChild($csrfHiddenInput);
        }
    }

    private function isMethodSecure(string $method)
    {
        return in_array($method, ['GET', 'HEAD']); // pseudo implementation. do not use this in production.
        return $node;
    }
});
```
 No newline at end of file
+39 −94
Original line number Diff line number Diff line
The only officially supported installation method is through composer:
# Setup

The only officially supported installation method is through Composer:

    composer require mschop/notee
    
If you don't have composer available, follow the composer installation instructions under:
If you don't have Composer available, follow the Composer installation instructions under:
[Composer Installation](https://getcomposer.org/doc/00-intro.md)

## Basic Usage

Using NoTee is straight forward. You just need to create an instance of the class NodeFactory:
Next you need to include the file `vendor/autoload.php` provided by Composer.

    <?php
    require_once '--PATH_TO_ROOT--/vendor/autoload.php';
    
    require 'vendor/autoload.php';
    
    namespace YourNamespace;
    
    use NoTee\NodeFactory;
    use NoTee\DefaultEscapingStrategy;
    use NoTee\UriValidator;
    use NoTee\BlockManager;
    
    $nf = new NodeFactory(
        new DefaultEscapingStrategy('utf-8'),
        new UriValidator(),
        new BlockManager()
    );
    
    // now you can already build html:
Now you can create an instance of the class NoTee\NoTee:

    echo $nf->document(
        $nf->html(
            $nf->head(
                ...
            ),
            $nf->body(
                ...
            ),
        )
    );
    <?php
    $charset = 'utf-8';
    $templateDirs = [...]; // optional array of directories, used for the optional template functionality
    $defaultContext = [...] // optional array of default context variables, used for all files in template functionality
    $debug = false;
    $noTee = NoTee\NoTee::create($charset, $templateDirs, $defaultContext, $debug);

## Use global functions (recommended)
(optional) NoTee provides global functions, which make the template code much easier to read. Please note, that only one instance of the
NoTee class can be registered globally. Whether you use this functionality is a matter of taste and application architecture.

You can make use of the global functions. Whether one should use the global functions is a religious question, because
this feature relies on global state. Using the global functions enables less verbose code:
    $noTee->enableGlobal();

    <?php
# Usage

This guide assumes, that you called `$noTee->enableGlobal()`. In this case you can use the global functions. The coding style is slightly
different:

    require 'vendor/autoload.php';
Without global functions:

    namespace YourNamespace;
    $nf = $noTee->getNodeFactory();
    echo $nf->div("Hello World");
    
    use NoTee\NodeFactory;
    use NoTee\DefaultEscapingStrategy;
    use NoTee\UriValidator;
    use NoTee\BlockManager;
With global functions:

    global $noTee;
    $noTee = new NodeFactory(
         new DefaultEscapingStrategy('utf-8'),
         new UriValidator(),
         new BlockManager()
     );
     require 'vendor/mschop/notee/global.php';
    $noTee->enableGlobal();
    echo _div('Hello World');
    
     // Now you can use global functions. All functions are prefixed with _ for reducing the probability for naming 
     // collisions
Simple example:

    echo _document(
        _html(
@@ -74,41 +50,10 @@ this feature relies on global state. Using the global functions enables less ver
            ),
            _body(
                ...
            ),
        )
        )     
     );
    
## Use Template-Feature

If you need extensibility (block system and template hierarchy) you should use the template feature.

    <?php
    
        
    require 'vendor/autoload.php';
    
    namespace YourNamespace;
    
    use NoTee\NodeFactory;
    use NoTee\DefaultEscapingStrategy;
    use NoTee\UriValidator;
    use NoTee\BlockManager;
    use NoTee\Template;
        
    global $noTee;
    $noTee = new NodeFactory(
         new DefaultEscapingStrategy('utf-8'),
         new UriValidator(),
         new BlockManager()
    );
     require 'vendor/mschop/notee/global.php';
    
     $directories = [
        'templates/base',
        'templates/child',
     ];
     $template = new Template($directories, $noTee);
     $noTee->setTemplate($template);
Example using the template system:

A details explanation of the template system can be found in the
[Template Documentation](/#extensibility/#template-functionality).
 No newline at end of file
    echo $noTee->getTemplate()->render('index.html.php');
Loading