Skip to content

Commit

Permalink
Merge pull request #122 from FriendsOfCake/plugin-class
Browse files Browse the repository at this point in the history
Move plugin bootstrapping and routes setting within Plugin class
  • Loading branch information
ADmad authored Dec 21, 2019
2 parents f1d8565 + b9eb3b6 commit 500bfa2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
31 changes: 0 additions & 31 deletions config/bootstrap.php

This file was deleted.

3 changes: 0 additions & 3 deletions config/routes.php

This file was deleted.

3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
<exclude>
<file>src/Plugin.php</file>
</exclude>
</whitelist>
</filter>

Expand Down
47 changes: 47 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
namespace CsvView;

use Cake\Core\BasePlugin;
use Cake\Core\PluginApplicationInterface;
use Cake\Event\EventInterface;
use Cake\Event\EventManager;
use Cake\Http\ServerRequest;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;

class Plugin extends BasePlugin
{
Expand All @@ -13,4 +19,45 @@ class Plugin extends BasePlugin
* @var string
*/
protected $name = 'CsvView';

/**
* @inheritDoc
*/
public function bootstrap(PluginApplicationInterface $app): void
{
/**
* Add CsvView to View class map through RequestHandler, if available, on Controller initialisation
*
* @link https://book.cakephp.org/4/en/controllers/components/request-handling.html#using-custom-viewclasses
*/
EventManager::instance()->on('Controller.initialize', function (EventInterface $event) {
$controller = $event->getSubject();
if ($controller->components()->has('RequestHandler')) {
$controller->RequestHandler->setConfig('viewClassMap.csv', 'CsvView.Csv');
}
});

/**
* Add a request detector named "csv" to check whether the request was for a CSV,
* either through accept header or file extension
*
* @link https://book.cakephp.org/4/en/controllers/request-response.html#checking-request-conditions
*/
ServerRequest::addDetector(
'csv',
[
'accept' => ['text/csv'],
'param' => '_ext',
'value' => 'csv',
]
);
}

/**
* @inheritDoc
*/
public function routes(RouteBuilder $routes): void
{
Router::extensions('csv');
}
}

0 comments on commit 500bfa2

Please sign in to comment.