Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotations router custom action pre-format callback #14819

Merged
merged 7 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG-4.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Added `preload` for Volt, which will send a HTTP/2 preload header [#13128](https://github.com/phalcon/cphalcon/issues/13128)
- Added `Phalcon\Helper\Arr::blackList()` to exclude elements of an array by the keys obtained from the elements of a blacklist [#14801](https://github.com/phalcon/cphalcon/issues/14801) [@TimurFlush](https://github.com/TimurFlush)
- Added `Phalcon\Debug::renderHtml()` to get a HTML representation of the exception [#14794](https://github.com/phalcon/cphalcon/issues/14794) [@TimurFlush](https://github.com/TimurFlush)
- Added `Phalcon\Mvc\Router\Annotations->setActionPreformatCallback($callback)` to set a callback which pre-formats actions to custom pattern [#14819](https://github.com/phalcon/cphalcon/pull/14819)

## Changed
- Added service checks for the session. Now cookies will be saved in the session only when the `session` service is defined [#11770](https://github.com/phalcon/cphalcon/issues/11770), [#14649](https://github.com/phalcon/cphalcon/pull/14649)
Expand All @@ -26,4 +27,4 @@
- Removed `Phalcon\Http\Cookie` binding to session [#11770](https://github.com/phalcon/cphalcon/issues/11770)
- `Phalcon\Http\Cookie` no longer depends on the session service and data will not be duplicated in the session. This made it difficult to use cookies in stateless applications (SPA).

`
`
57 changes: 55 additions & 2 deletions phalcon/Mvc/Router/Annotations.zep
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Annotations extends Router
{
protected actionSuffix = "Action";

protected actionPreformatCallback;

protected controllerSuffix = "Controller";

protected handlers = [];
Expand Down Expand Up @@ -240,7 +242,7 @@ class Annotations extends Router
public function processActionAnnotation(string! module, string! namespaceName, string! controller, string! action,
<Annotation> annotation)
{
var name, actionName, routePrefix, paths, value, uri, route, methods,
var name, proxyActionName, actionName, routePrefix, paths, value, uri, route, methods,
converts, param, convert, converterParam, routeName, beforeMatch;
bool isRoute;

Expand Down Expand Up @@ -271,9 +273,15 @@ class Annotations extends Router
return;
}

let actionName = strtolower(str_replace(this->actionSuffix, "", action)),
let proxyActionName = str_replace(this->actionSuffix, "", action),
routePrefix = this->routePrefix;

if this->actionPreformatCallback !== null {
let proxyActionName = call_user_func(this->actionPreformatCallback, proxyActionName);
}

let actionName = strtolower(proxyActionName);

/**
* Check for existing paths in the annotation
*/
Expand Down Expand Up @@ -396,6 +404,51 @@ class Annotations extends Router
let this->actionSuffix = actionSuffix;
}

/**
* Sets the action preformat callback
* $action here already without suffix 'Action'
*
* ```php
* // Array as callback
* $annotationRouter->setActionPreformatCallback([Text::class, 'uncamelize']);
*
* // Function as callback
* $annotationRouter->setActionPreformatCallback(function(action){
* return action;
* });
*
* // String as callback
* $annotationRouter->setActionPreformatCallback('strtolower');
*
* // If empty method constructor called [null], sets uncamelize with - delimiter
* $annotationRouter->setActionPreformatCallback();
* ```
*
* @param callable|string|null $callback
*/
public function setActionPreformatCallback(var callback = null)
{
if likely is_callable(callback) {
let this->actionPreformatCallback = callback;
} elseif callback === null {
let this->actionPreformatCallback = function (action) {
return uncamelize(action, "-");
};
} else {
throw new Exception(
"The 'callback' parameter must be either a callable or NULL."
);
}
}

/**
* @return callable|string|null
*/
public function getActionPreformatCallback()
{
return this->actionPreformatCallback;
}

/**
* Changes the controller class suffix
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Test\Integration\Mvc\Router\Annotations;

use IntegrationTester;
use Phalcon\Mvc\Router\Annotations;
use Phalcon\Text;

/**
* Class SetActionPreformatCallbackCest
*/
class SetActionPreformatCallbackCest
{
/**
* Tests Phalcon\Mvc\Router\Annotations :: setActionPreformatCallback()
*
* @author Phalcon Team <[email protected]>
* @since 2020-02-03
*/
public function mvcRouterAnnotationsSetActionPreformatCallback(IntegrationTester $I)
{
$I->wantToTest('Mvc\Router\Annotations - setDefaultAction()');

$router = new Annotations(false);
$callback = [Text::class, 'uncamelize'];
$router->setActionPreformatCallback($callback);
$attachedCallback = $router->getActionPreformatCallback();

$I->assertEquals(
$callback,
$attachedCallback
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public function mvcModelResultsetSimpleToArray(IntegrationTester $I)
* Re-create DB table with data
*/
$db = $this->getService('db');
(new InvoicesMigration())($db);

$migration = new InvoicesMigration($db);
$migration->create();

$data = [
[1, 0, 'Title 1', 10.51, date('Y-m-d H:i:s')],
[123, 1, 'Title 2', 5.2, date('Y-m-d H:i:s')],
Expand Down