Completed
Push — prototype ( 3bfdd7...aec713 )
by Peter
07:47
created

CreditsComponent::onRender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 21.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Webino Credits
4
 * Kudos to all of you
5
 */
6
7
use WebinoAppLib\Event\DispatchEvent;
8
use WebinoAppLib\Event\RouteEvent;
9
use WebinoAppLib\Response\ViewResponse;
10
use WebinoAppLib\Router\DefaultRoute;
11
use WebinoAppLib\Service\Credits;
12
use WebinoAppLib\View\SourcePreviewComponent;
13
use WebinoDomLib\Event\RenderEvent;
14
use WebinoHtmlLib\Html;
15
use WebinoViewLib\Component\AbstractViewComponent;
16
use WebinoViewLib\Component\OnDispatchInterface;
17
use WebinoViewLib\Component\Stylesheet;
18
use WebinoViewLib\Feature\CommonView;
19
use WebinoViewLib\Feature\NodeView;
20
21
require __DIR__ . '/../../vendor/autoload.php';
22
23
class CreditsComponent extends AbstractViewComponent implements OnDispatchInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    /**
26
     * Vendor dir
27
     */
28
    const DIR = '../../../../vendor';
29
30
    /**
31
     * Credits HTML
32
     *
33
     * @var string
34
     */
35
    private $html;
36
37
    /**
38
     * @param NodeView $node
39
     */
40
    public function configure(NodeView $node)
41
    {
42
        $node
43
            ->setLocator('credits')
44
            ->setRename('div')
45
            ->setAddClass('list-group text-center');
46
    }
47
48
    /**
49
     * @param DispatchEvent $event
50
     */
51
    public function onDispatch(DispatchEvent $event)
52
    {
53
        if (!is_dir($this::DIR)) {
54
            return;
55
        }
56
57
        /** @var Credits $credits */
58
        $credits = $event->getApp()->get(Credits::class);
59
        if (!$credits) {
60
            return;
61
        }
62
63
        $html = '';
64
        foreach ($credits->getCredits($this::DIR) as $item) {
65
66
            $html .= (new Html\Block('0 1'))
67
                ->setClass('list-group-item')
68
                ->format([
69
                    new Html\Text(new Html\Strong($item[0])),
70
                    new Html\Text($item[1])
71
                ]);
72
        }
73
74
        $this->html = $html;
75
    }
76
77
    /**
78
     * @param RenderEvent $event
79
     */
80
    public function onRender(RenderEvent $event)
81
    {
82
        $event->getNode()->setHtml($this->html);
83
    }
84
}
85
86
$config = Webino::config([
87
88
    new CommonView([
89
        new Stylesheet\BootstrapV3,
90
        new SourcePreviewComponent(__FILE__),
91
92
        (new NodeView('content'))
93
            ->setLocator('body')
94
            ->setHtml('<div class="container"><intro/><credits/><source-preview/></div>'),
95
96
        (new NodeView('intro'))
97
            ->setLocator('intro')
98
            ->setRename('div')
99
            ->setHtml('<div class="page-header text-center"><h1>Webino Credits</h1><copyright/></div><note/>')
100
            ->setView([
101
                (new NodeView('copyright'))
102
                    ->setLocator('copyright')
103
                    ->setRename('div')
104
                    ->setHtml([
105
                        (new Html\Text(':vendor (:link)'))
106
                            ->format([
107
                                ':vendor' => Credits::VENDOR_COPYRIGHT,
108
                                ':link'   => new Html\Url(Credits::VENDOR_URL),
109
                            ]),
110
111
                        new Html\Text(
112
                            (new Html\Html('<strong>Author:</strong> :name (:link)'))
113
                                ->format([
114
                                    ':name' => Credits::AUTHOR_NAME,
115
                                    ':link' => new Html\Url(Credits::AUTHOR_URL),
116
                                ])
117
                        ),
118
                    ]),
119
120
                (new NodeView('note'))
121
                    ->setLocator('note')
122
                    ->setRename('p')
123
                    ->setAddClass('alert alert-info text-center')
124
                    ->setValue([
125
                        'Webino™ is brought to you thanks to authors and contributors ',
126
                        'of following third-party libraries also.',
127
                    ])
128
            ]),
129
130
        new CreditsComponent,
131
    ]),
132
]);
133
134
$debugger = Webino::debugger(Webino::debuggerOptions()->setDevMode()->setBar());
135
$app = Webino::application($config, $debugger)->bootstrap();
136
137
$app->bind(DefaultRoute::class, function (RouteEvent $event) {
138
    $event->setResponse(new ViewResponse);
139
});
140
141
$app->dispatch();
142