MyCustomFeature::setAnything()   A
last analyzed

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 0
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 17 and the first side effect is on line 15.

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
 * Advanced usage example index file
4
 */
5
6
use WebinoAppLib\Application\CoreConfig;
7
use WebinoAppLib\Event\AppEvent;
8
use WebinoAppLib\Event\DispatchEvent;
9
use WebinoAppLib\Event\RouteEvent;
10
use WebinoAppLib\Feature as AppFeature;
11
use WebinoAppLib\Listener\AbstractRouteListener;
12
use WebinoAppLib\Router\DefaultRoute;
13
use WebinoConfigLib\Feature as ConfigFeature;
14
15
require 'vendor/autoload.php';
16
17
class MyCustomFeature extends ConfigFeature\AbstractFeature
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...
18
{
19
    public function setAnything()
20
    {
21
        return $this;
22
    }
23
}
24
25
class MyCustomListener extends AbstractRouteListener
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type MyCustomListener has been defined more than once; this definition is ignored, only the first definition in examples/advanced-usage/src/MyCustomListener.php (L6-17) is considered.

This check looks for classes that have been defined more than once.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
26
{
27
    public function init()
28
    {
29
        $this->listen(AppEvent::DISPATCH, function (DispatchEvent $event) {
30
            $event->setResponse($event->getApp()->file()->read('docs/common.html'));
31
        }, AppEvent::BEGIN);
32
33
        $this->listen(AppEvent::DISPATCH, function (DispatchEvent $event) {
34
            $event->setResponse($event->getApp()->file()->read('docs/footer.html'));
35
        }, AppEvent::FINISH);
36
37
        $this->listen(DefaultRoute::class, function (RouteEvent $event) {
38
            $event->setResponse([
39
                '<h1>Webino Application Basic Usage Example</h1>',
40
                $event->getApp()->url('myRoute')->html('My Route Example'),
41
                '<br />',
42
                $event->getApp()->url('myRuntimeRoute')->html('My Runtime Route Example')
43
            ]);
44
        });
45
46
        $this->listen(RouteEvent::MATCH, function (RouteEvent $event) {
47
            $routeName = $event->getRouteMatch()->getMatchedRouteName();
48
            if (DefaultRoute::class === $routeName) {
49
                $routeName = 'default';
50
            }
51
52
            $file = $event->getApp()->file();
53
            $path = 'docs/' . $routeName . '.html';
54
55
            if ($file->has($path)) {
56
                $docs = $file->read($path);
57
                $event->setResponse($docs);
58
            }
59
        }, RouteEvent::FINISH);
60
61
        $this->listen(RouteEvent::MATCH, function (RouteEvent $event) {
62
            if (DefaultRoute::class !== $event->getRouteMatch()->getMatchedRouteName()) {
63
                $event->setResponse($event->getApp()->url(DefaultRoute::class)->html('Go Home'));
64
            }
65
        }, RouteEvent::FINISH);
66
67
        $this->listenRoute('myRoute', function (RouteEvent $event) {
0 ignored issues
show
Bug introduced by
The method listenRoute() does not exist on MyCustomListener. Did you maybe mean listen()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
68
            $event->setResponse('<h1>My Route Example Content</h1>');
69
        });
70
71
        $this->listenRoute('myRuntimeRoute', function (RouteEvent $event) {
0 ignored issues
show
Bug introduced by
The method listenRoute() does not exist on MyCustomListener. Did you maybe mean listen()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
72
            $event->setResponse([
73
                '<h1>My Runtime Route Example Content</h1>',
74
            ]);
75
        });
76
    }
77
}
78
79
80
$config = new CoreConfig([
81
    new ConfigFeature\Log,
82
    new ConfigFeature\FirePhpLog,
83
    new AppFeature\FilesystemCache,
84
85
    (new MyCustomFeature)
86
        ->setAnything(),
87
88
    new AppFeature\Listener(MyCustomListener::class),
89
90
    (new ConfigFeature\Route('myRoute'))->setLiteral('/my-route'),
91
]);
92
93
94
$appCore = Webino::application($config);
95
$app = $appCore->bootstrap();
96
97
// runtime route example
98
$app->route('myRuntimeRoute')->setLiteral('/my-runtime-route');
99
100
require __DIR__ . '/dispatch.php';
101
102
$app->dispatch();
103