Skip to content

Commit

Permalink
Merge pull request #639 from Phally/v5-querylog
Browse files Browse the repository at this point in the history
Adds option to control which connections to log (backport to v5/master)
  • Loading branch information
ADmad committed May 29, 2020
2 parents eb936ec + 9d86829 commit a58c8b7
Show file tree
Hide file tree
Showing 30 changed files with 413 additions and 251 deletions.
2 changes: 1 addition & 1 deletion .stickler.yml
@@ -1,7 +1,7 @@
---
linters:
phpcs:
standard: CakePHP
standard: CakePHP3
extensions: 'php,ctp'
fixer: true

Expand Down
22 changes: 21 additions & 1 deletion docs/listeners/api-query-log.rst
Expand Up @@ -29,7 +29,7 @@ you want to attach it only to specific controllers and actions
}
Attach it using components array, this is recommended if you want to
Attach it in :code:`AppController::initialize()`, this is recommended if you want to
attach it to all controllers, application wide


Expand Down Expand Up @@ -88,3 +88,23 @@ Paginated results will include a
}
}
}
Configuration
-------------

By default this listener will log all defined connections.

If you need to select specific connections to log, you can use the :code:`connections` configuration:

.. code-block:: php
$this->loadComponent('Crud.Crud', [
'listeners' => [
'Crud.Api',
'ApiQueryLog' => [
'className' => 'Crud.ApiQueryLog',
'connections' => ['default', 'elastic']
]
]
]);
7 changes: 4 additions & 3 deletions src/Action/BaseAction.php
Expand Up @@ -8,6 +8,7 @@
use Cake\Utility\Text;
use Crud\Core\BaseObject;
use Crud\Event\Subject;
use Exception;

/**
* Base Crud class
Expand Down Expand Up @@ -129,7 +130,7 @@ public function disable()
public function message($type, array $replacements = [])
{
if (empty($type)) {
throw new \Exception('Missing message type');
throw new Exception('Missing message type');
}

$crud = $this->_crud();
Expand All @@ -138,7 +139,7 @@ public function message($type, array $replacements = [])
if (empty($config)) {
$config = $crud->getConfig('messages.' . $type);
if (empty($config)) {
throw new \Exception(sprintf('Invalid message type "%s"', $type));
throw new Exception(sprintf('Invalid message type "%s"', $type));
}
}

Expand All @@ -155,7 +156,7 @@ public function message($type, array $replacements = [])
], $config);

if (!isset($config['text'])) {
throw new \Exception(sprintf('Invalid message config for "%s" no text key found', $type));
throw new Exception(sprintf('Invalid message config for "%s" no text key found', $type));
}

$config['params']['original'] = ucfirst(str_replace('{name}', $config['name'], $config['text']));
Expand Down
3 changes: 2 additions & 1 deletion src/Controller/ControllerTrait.php
Expand Up @@ -2,6 +2,7 @@
namespace Crud\Controller;

use Cake\Controller\Exception\MissingActionException;
use LogicException;

/**
* Enable Crud to catch MissingActionException and attempt to generate response
Expand Down Expand Up @@ -47,7 +48,7 @@ public function invokeAction()
{
$request = $this->request;
if (!isset($request)) {
throw new \LogicException('No Request object configured. Cannot invoke action');
throw new LogicException('No Request object configured. Cannot invoke action');
}
if (!$this->isAction($request->getParam('action'))) {
throw new MissingActionException([
Expand Down
4 changes: 3 additions & 1 deletion src/Error/Exception/CrudException.php
@@ -1,6 +1,8 @@
<?php
namespace Crud\Error\Exception;

class CrudException extends \Exception
use Exception;

class CrudException extends Exception
{
}
4 changes: 3 additions & 1 deletion src/Event/Subject.php
@@ -1,6 +1,8 @@
<?php
namespace Crud\Event;

use Exception;

/**
* Crud subject
*
Expand Down Expand Up @@ -104,7 +106,7 @@ public function shouldProcess($mode, $actions = [])
return !in_array($this->action, $actions);

default:
throw new \Exception('Invalid mode');
throw new Exception('Invalid mode');
}
}
}
13 changes: 12 additions & 1 deletion src/Listener/ApiQueryLogListener.php
Expand Up @@ -21,6 +21,15 @@
class ApiQueryLogListener extends BaseListener
{

/**
* {@inheritDoc}
*
* `connections` List of connection names to log. Empty means all defined connections.
*/
protected $_defaultConfig = [
'connections' => [],
];

/**
* Returns a list of all events that will fire in the controller during its lifecycle.
* You can override this function to add you own listener callbacks
Expand Down Expand Up @@ -49,7 +58,9 @@ public function implementedEvents()
*/
public function setupLogging(Event $event)
{
foreach ($this->_getSources() as $connectionName) {
$connections = $this->getConfig('connections') ?: $this->_getSources();

foreach ($connections as $connectionName) {
try {
$connection = $this->_getSource($connectionName);

Expand Down
4 changes: 2 additions & 2 deletions src/Template/Bake/Controller/controller.ctp
Expand Up @@ -4,9 +4,9 @@ use Cake\Utility\Inflector;
$defaultModel = $name;
%>
<?php
namespace <%= $namespace %>\Controller<%= $prefix %>;
namespace <%= $namespace %>Controller<%= $prefix %>;

use <%= $namespace %>\Controller\AppController;
use use const Controller\AppController;<%= $namespace %>AppController;

/**
* <%= $name %> Controller
Expand Down
4 changes: 3 additions & 1 deletion tests/App/Model/Entity/Blog.php
@@ -1,7 +1,9 @@
<?php
namespace Crud\Test\App\Model\Entity;

class Blog extends \Cake\ORM\Entity
use Cake\ORM\Entity;

class Blog extends Entity
{

}
4 changes: 3 additions & 1 deletion tests/App/Model/Entity/User.php
@@ -1,7 +1,9 @@
<?php
namespace Crud\Test\App\Model\Entity;

class User extends \Cake\ORM\Entity
use Cake\ORM\Entity;

class User extends Entity
{

}
3 changes: 2 additions & 1 deletion tests/App/Model/Table/BlogsTable.php
Expand Up @@ -2,8 +2,9 @@
namespace Crud\Test\App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\Table;

class BlogsTable extends \Cake\ORM\Table
class BlogsTable extends Table
{
public $customOptions;

Expand Down
3 changes: 2 additions & 1 deletion tests/App/Model/Table/CrudExamplesTable.php
Expand Up @@ -2,14 +2,15 @@
namespace Crud\Test\App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\Table;

/**
* Crud Example Model
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
*/
class CrudExamplesTable extends \Cake\ORM\Table
class CrudExamplesTable extends Table
{

public $alias = 'CrudExamples';
Expand Down
4 changes: 3 additions & 1 deletion tests/App/Model/Table/UsersTable.php
@@ -1,7 +1,9 @@
<?php
namespace Crud\Test\App\Model\Table;

class UsersTable extends \Cake\ORM\Table
use Cake\ORM\Table;

class UsersTable extends Table
{

}
5 changes: 4 additions & 1 deletion tests/App/Template/Blogs/view.ctp
@@ -1,6 +1,9 @@
<?php

use Cake\Utility\Inflector;

foreach (${$viewVar}->toArray() as $k => $v) {
echo "<dt>" . \Cake\Utility\Inflector::humanize($k) . "</dt>";
echo "<dt>" . Inflector::humanize($k) . "</dt>";
echo "<dd>";
echo $v;
echo "</dd>";
Expand Down
21 changes: 11 additions & 10 deletions tests/TestCase/Action/AddActionTest.php
@@ -1,6 +1,7 @@
<?php
namespace Crud\Test\TestCase\Action;

use Cake\Controller\Component\FlashComponent;
use Cake\Core\Plugin;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
Expand Down Expand Up @@ -102,7 +103,7 @@ public function testActionPost()
'Controller.initialize',
['priority' => 11],
function ($event) {
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
$this->_controller->Flash = $this->getMockBuilder(FlashComponent::class)
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -147,7 +148,7 @@ public function testActionPostWithAddRedirect()
'Controller.initialize',
['priority' => 11],
function ($event) {
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
$this->_controller->Flash = $this->getMockBuilder(FlashComponent::class)
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -192,7 +193,7 @@ public function testActionPostWithEditRedirect()
'Controller.initialize',
['priority' => 11],
function ($event) {
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
$this->_controller->Flash = $this->getMockBuilder(FlashComponent::class)
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -236,7 +237,7 @@ public function testActionPostErrorSave()
'Controller.initialize',
['priority' => 11],
function ($event) {
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
$this->_controller->Flash = $this->getMockBuilder(FlashComponent::class)
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -289,7 +290,7 @@ public function testActionPostValidationErrors()
'Controller.initialize',
['priority' => 11],
function ($event) {
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
$this->_controller->Flash = $this->getMockBuilder(FlashComponent::class)
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -397,7 +398,7 @@ public function testApiCreate($method)
'Controller.initialize',
['priority' => 11],
function ($event) {
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
$this->_controller->Flash = $this->getMockBuilder(FlashComponent::class)
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -444,7 +445,7 @@ function ($event) {
return;
}

$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
$this->_controller->Flash = $this->getMockBuilder(FlashComponent::class)
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -496,7 +497,7 @@ function ($event) {
return;
}

$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
$this->_controller->Flash = $this->getMockBuilder(FlashComponent::class)
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -542,7 +543,7 @@ public function testStopAddWithDefaultSubjectSuccess()
'Controller.initialize',
['priority' => 11],
function ($event) {
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
$this->_controller->Flash = $this->getMockBuilder(FlashComponent::class)
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -596,7 +597,7 @@ public function testStopAddWithManuallySetSubjectSuccess()
'Controller.initialize',
['priority' => 11],
function ($event) {
$this->_controller->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
$this->_controller->Flash = $this->getMockBuilder(FlashComponent::class)
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();
Expand Down

0 comments on commit a58c8b7

Please sign in to comment.