Completed
Push — master ( 8d3a7c...22bc57 )
by
unknown
33:07 queued 19:19
created

src/Extension/Data/DataExtension.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace League\Plates\Extension\Data;
4
5
use League\Plates;
6
7
/** The DataExtension adds the ability to hydrate data into a template before it gets rendered. */
8
final class DataExtension implements Plates\Extension
9
{
10 16
    public function register(Plates\Engine $plates) {
11 16
        $c = $plates->getContainer();
12 16
        $c->add('data.globals', []);
13 16
        $c->add('data.template_data', []);
14 16
15
        $plates->defineConfig(['merge_parent_data' => true]);
16 16
        $plates->pushComposers(function($c) {
17 16
            return array_filter([
18 16
                'data.addGlobals' => $c->get('data.globals') ? addGlobalsCompose($c->get('data.globals')) : null,
19 16
                'data.mergeParentData' => $c->get('config')['merge_parent_data'] ? mergeParentDataCompose() : null,
20 16
                'data.perTemplateData' => $c->get('data.template_data') ? perTemplateDataCompose($c->get('data.template_data')) : null,
21
            ]);
22 16
        });
23
24 16
        $plates->addMethods([
25 16
            'addGlobals' => function(Plates\Engine $e, array $data) {
26 4
                $c = $e->getContainer();
27 4
                $c->merge('data.globals', $data);
28 16
            },
29 16
            'addGlobal' => function(Plates\Engine $e, $name, $value) {
30
                $e->getContainer()->merge('data.globals', [$name => $value]);
31 16
            },
32 16
            'addData' => function(Plates\Engine $e, $data, $name = null) {
33 4
                if (!$name) {
34 4
                    return $e->addGlobals($data);
0 ignored issues
show
Documentation Bug introduced by
The method addGlobals does not exist on object<League\Plates\Engine>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
35 4
                }
36
37 4
                $template_data = $e->getContainer()->get('data.template_data');
38 4
                if (!isset($template_data[$name])) {
39 16
                    $template_data[$name] = [];
40
                }
41 16
                $template_data[$name] = array_merge($template_data[$name], $data);
42
                $e->getContainer()->add('data.template_data', $template_data);
43
            }
44
        ]);
45
    }
46
}
47