From b9eb3b6ccb17a9890148bf8cc5d3df1fa773ef39 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 22 Dec 2019 01:36:50 +0530 Subject: [PATCH] Move plugin bootstrapping and routes setting within Plugin class --- config/bootstrap.php | 31 ----------------------------- config/routes.php | 3 --- phpunit.xml.dist | 3 +++ src/Plugin.php | 47 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 34 deletions(-) delete mode 100644 config/bootstrap.php delete mode 100644 config/routes.php diff --git a/config/bootstrap.php b/config/bootstrap.php deleted file mode 100644 index 3bd142d..0000000 --- a/config/bootstrap.php +++ /dev/null @@ -1,31 +0,0 @@ -on('Controller.initialize', function (Event $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', - ] -); diff --git a/config/routes.php b/config/routes.php deleted file mode 100644 index 54a41e7..0000000 --- a/config/routes.php +++ /dev/null @@ -1,3 +0,0 @@ - src/ + + src/Plugin.php + diff --git a/src/Plugin.php b/src/Plugin.php index e43d643..75c5b71 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -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 { @@ -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'); + } }