• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mixerapi / mixerapi-dev / 12019816840

25 Nov 2024 10:19PM UTC coverage: 87.451% (-0.1%) from 87.598%
12019816840

Pull #155

github

web-flow
Merge fd1b81212 into 25e980db2
Pull Request #155: CakePHP 5.1 deprecations

5 of 6 new or added lines in 2 files covered. (83.33%)

1 existing line in 1 file now uncovered.

899 of 1028 relevant lines covered (87.45%)

3.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.42
/plugins/crud/src/Plugin.php
1
<?php
2
declare(strict_types=1);
3

4
namespace MixerApi\Crud;
5

6
use Cake\Core\BasePlugin;
7
use Cake\Core\ContainerInterface;
8
use Cake\Core\PluginApplicationInterface;
9
use Cake\Event\Event;
10
use Cake\Event\EventManager;
11
use Cake\Http\Exception\MethodNotAllowedException;
12

13
/**
14
 * Plugin for Crud
15
 *
16
 * @experimental
17
 */
18
class Plugin extends BasePlugin
19
{
20
    protected ?string $name = 'MixerApi/Crud';
21

22
    /**
23
     * Console middleware
24
     *
25
     * @var bool
26
     */
27
    protected bool $consoleEnabled = false;
28

29
    /**
30
     * Enable middleware
31
     *
32
     * @var bool
33
     */
34
    protected bool $middlewareEnabled = false;
35

36
    /**
37
     * Load routes or not
38
     *
39
     * @var bool
40
     */
41
    protected bool $routesEnabled = false;
42

43
    /**
44
     * Enforce request->allowMethod() for the follow action/http-method pair.
45
     *
46
     * To alter: Set $options['allowedMethods'] to the mapping of your choice
47
     * To disable: Set $options['allowedMethods'] to an empty array to turn this functionality off.
48
     *
49
     * @var array
50
     */
51
    private array $allowedMethods = [
52
        'add' => ['post'],
53
        'index' => ['get'],
54
        'view' => ['get'],
55
        'edit' => ['patch','put','patch'],
56
        'delete' => ['delete'],
57
    ];
58

59
    /**
60
     * Should viewVars be serialized automatically? Defaults to true, set to false to disable.
61
     *
62
     * @var bool
63
     */
64
    private bool $doSerialize = true;
65

66
    /**
67
     * See this class for allowed $options
68
     *
69
     * @param array $options options
70
     */
71
    public function __construct(array $options = [])
72
    {
73
        parent::__construct($options);
8✔
74
        $this->allowedMethods = $options['allowedMethods'] ?? $this->allowedMethods;
8✔
75
        $this->doSerialize = $options['doSerialize'] ?? $this->doSerialize;
8✔
76
    }
77

78
    /**
79
     * Load all the plugin configuration and bootstrap logic.
80
     *
81
     * The host application is provided as an argument. This allows you to load
82
     * additional plugin dependencies, or attach events.
83
     *
84
     * @param \Cake\Core\PluginApplicationInterface $app The host application
85
     * @return void
86
     */
87
    public function bootstrap(PluginApplicationInterface $app): void
88
    {
89
        $this->allowedMethodsEvent();
8✔
90
        $this->serializeEvent();
8✔
91
    }
92

93
    /**
94
     * Register application container services.
95
     *
96
     * @param \Cake\Core\ContainerInterface $container The Container to update.
97
     * @return void
98
     * @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection
99
     */
100
    public function services(ContainerInterface $container): void
101
    {
102
        /** @var \League\Container\Container $container */
103
        $container->addServiceProvider(new CrudServiceProvider());
8✔
104
    }
105

106
    /**
107
     * Registers listener to enforce allowed methods
108
     *
109
     * @return void
110
     */
111
    private function allowedMethodsEvent(): void
112
    {
113
        if (empty($this->allowedMethods)) {
8✔
114
            return;
×
115
        }
116

117
        EventManager::instance()->on('Controller.initialize', function (Event $event) {
8✔
118
            /** @var \Cake\Controller\Controller $controller */
119
            $controller = $event->getSubject();
8✔
120
            $action = $controller->getRequest()->getParam('action');
8✔
121

122
            if (is_array($this->allowedMethods) && isset($this->allowedMethods[$action])) {
8✔
123
                try {
124
                    $controller->getRequest()->allowMethod($this->allowedMethods[$action]);
8✔
125
                } catch (MethodNotAllowedException $e) {
×
126
                    throw new MethodNotAllowedException(
×
127
                        'Method Not Allowed. Must be one of: ' . implode($this->allowedMethods[$action])
×
128
                    );
×
129
                }
130
            }
131
        });
8✔
132
    }
133

134
    /**
135
     * Register listener for automatic serialization on all responses with a status code in the 200-299 range.
136
     *
137
     * @return void
138
     */
139
    private function serializeEvent(): void
140
    {
141
        if (!$this->doSerialize) {
8✔
142
            return;
×
143
        }
144

145
        EventManager::instance()->on('Controller.beforeRender', function (Event $event) {
8✔
146
            /** @var \Cake\Controller\Controller $controller */
147
            $controller = $event->getSubject();
7✔
148
            $action = $controller->getRequest()->getParam('action');
7✔
149
            if (!in_array($action, ['index', 'view', 'add', 'edit', 'delete'])) {
7✔
UNCOV
150
                return;
×
151
            }
152

153
            if ($controller->getResponse()->getStatusCode() >= 300) {
7✔
154
                return;
2✔
155
            }
156

157
            $keys = array_keys($controller->viewBuilder()->getVars());
5✔
158
            if (!empty($keys)) {
5✔
159
                $controller->viewBuilder()->setOption('serialize', reset($keys));
5✔
160
            }
161
        });
8✔
162
    }
163
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc