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

Add Phalcon\Mvc\Router\Route::getGroup() #1715

Merged
merged 4 commits into from Dec 20, 2013
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
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@
- Phalcon\Mvc\Model\Validator::getOption() returns null if option does not exists (#1531)
- Added Phalcon\Mvc\Model::selectWriteConnection() (#1519)
- Added Phalcon\Mvc\Router\Group::convert()/getConverters() (#1555, #1572)
- Added Phalcon\Mvc\Router\Route::getGroup() (#1682)
- Faster Phalcon\Mvc\Model\Row (#1606)
- Optimized Phalcon\Mvc\Router\Group (#1682)
- Bug fix: no arguments were passed to beforeMatch handler in Phalcon\Mvc\Router (#1556)
- Added diagnostics when a validator returns false but does not call appendMessage() (#1664)
- Fixed handling of numeric namespaces/modules/controllers/actions (#1688)
Expand Down
171 changes: 43 additions & 128 deletions ext/mvc/router/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@

#include "kernel/main.h"
#include "kernel/memory.h"

#include "kernel/string.h"
#include "kernel/object.h"
#include "kernel/fcall.h"
#include "kernel/concat.h"
#include "kernel/array.h"

#include "interned-strings.h"

/**
* Phalcon\Mvc\Router\Group
*
Expand Down Expand Up @@ -103,26 +105,19 @@ PHP_METHOD(Phalcon_Mvc_Router_Group, __construct){

zval *paths = NULL;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 0, 1, &paths);
phalcon_fetch_params(0, 0, 1, &paths);

if (!paths) {
paths = PHALCON_GLOBAL(z_null);
}

if (Z_TYPE_P(paths) == IS_ARRAY) {
if (Z_TYPE_P(paths) == IS_ARRAY || Z_TYPE_P(paths) == IS_STRING) {
phalcon_update_property_this(this_ptr, SL("_paths"), paths TSRMLS_CC);
} else {
if (Z_TYPE_P(paths) == IS_STRING) {
phalcon_update_property_this(this_ptr, SL("_paths"), paths TSRMLS_CC);
}
}

if (phalcon_method_exists_ex(this_ptr, SS("initialize") TSRMLS_CC) == SUCCESS) {
phalcon_call_method_p1_noret(this_ptr, "initialize", paths);
phalcon_call_method_params(NULL, NULL, this_ptr, SL("initialize"), zend_inline_hash_func(SS("initialize")) TSRMLS_CC, 1, paths);
}

PHALCON_MM_RESTORE();
}

/**
Expand Down Expand Up @@ -278,31 +273,27 @@ PHP_METHOD(Phalcon_Mvc_Router_Group, _addRoute){
PHALCON_INIT_VAR(prefix_pattern);
PHALCON_CONCAT_VV(prefix_pattern, prefix, pattern);

PHALCON_OBS_VAR(default_paths);
phalcon_read_property_this(&default_paths, this_ptr, SL("_paths"), PH_NOISY_CC);
default_paths = phalcon_fetch_nproperty_this(this_ptr, SL("_paths"), PH_NOISY_CC);

/**
* Check if the paths need to be merged with current paths
*/
if (Z_TYPE_P(default_paths) == IS_ARRAY) {
if (Z_TYPE_P(paths) == IS_ARRAY) {
/**
* Merge the paths with the default paths
*/
PHALCON_INIT_VAR(merged_paths);
phalcon_fast_array_merge(merged_paths, &default_paths, &paths TSRMLS_CC);
} else {
PHALCON_CPY_WRT(merged_paths, default_paths);
}
if (Z_TYPE_P(default_paths) == IS_ARRAY && Z_TYPE_P(paths) == IS_ARRAY) {
/**
* Merge the paths with the default paths
*/
PHALCON_INIT_VAR(merged_paths);
phalcon_fast_array_merge(merged_paths, &default_paths, &paths TSRMLS_CC);
} else {
PHALCON_CPY_WRT(merged_paths, paths);
merged_paths = paths;
}

/**
* Every route is internally stored as a Phalcon\Mvc\Router\Route
*/
object_init_ex(return_value, phalcon_mvc_router_route_ce);
phalcon_call_method_p3_noret(return_value, "__construct", prefix_pattern, merged_paths, http_methods);
phalcon_call_method_p1_noret(return_value, "setgroup", this_ptr);

phalcon_update_property_array_append(this_ptr, SL("_routes"), return_value TSRMLS_CC);

Expand Down Expand Up @@ -341,6 +332,24 @@ PHP_METHOD(Phalcon_Mvc_Router_Group, add){
RETURN_MM();
}

static void phalcon_mvc_router_group_add_helper(INTERNAL_FUNCTION_PARAMETERS, const char *method)
{
zval *pattern, *paths = NULL, *http_method;

phalcon_fetch_params(0, 1, 1, &pattern, &paths);

if (!paths) {
paths = PHALCON_GLOBAL(z_null);
}

PHALCON_ALLOC_GHOST_ZVAL(http_method);
PHALCON_ZVAL_MAYBE_INTERNED_STRING(http_method, method);
phalcon_call_method_params(return_value, return_value_ptr, getThis(), SL("_addroute"), zend_inline_hash_func(SS("_addroute")) TSRMLS_CC, 3, pattern, paths, http_method);
if (return_value_ptr && EG(exception)) {
ALLOC_INIT_ZVAL(*return_value_ptr);
}
}

/**
* Adds a route to the router that only match if the HTTP method is GET
*
Expand All @@ -350,20 +359,7 @@ PHP_METHOD(Phalcon_Mvc_Router_Group, add){
*/
PHP_METHOD(Phalcon_Mvc_Router_Group, addGet){

zval *pattern, *paths = NULL, *method;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 1, &pattern, &paths);

if (!paths) {
paths = PHALCON_GLOBAL(z_null);
}

PHALCON_INIT_VAR(method);
ZVAL_STRING(method, "GET", 1);
phalcon_call_method_p3(return_value, this_ptr, "_addroute", pattern, paths, method);
RETURN_MM();
phalcon_mvc_router_group_add_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, phalcon_interned_GET);
}

/**
Expand All @@ -375,20 +371,7 @@ PHP_METHOD(Phalcon_Mvc_Router_Group, addGet){
*/
PHP_METHOD(Phalcon_Mvc_Router_Group, addPost){

zval *pattern, *paths = NULL, *method;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 1, &pattern, &paths);

if (!paths) {
paths = PHALCON_GLOBAL(z_null);
}

PHALCON_INIT_VAR(method);
ZVAL_STRING(method, "POST", 1);
phalcon_call_method_p3(return_value, this_ptr, "_addroute", pattern, paths, method);
RETURN_MM();
phalcon_mvc_router_group_add_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, phalcon_interned_POST);
}

/**
Expand All @@ -400,20 +383,7 @@ PHP_METHOD(Phalcon_Mvc_Router_Group, addPost){
*/
PHP_METHOD(Phalcon_Mvc_Router_Group, addPut){

zval *pattern, *paths = NULL, *method;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 1, &pattern, &paths);

if (!paths) {
paths = PHALCON_GLOBAL(z_null);
}

PHALCON_INIT_VAR(method);
ZVAL_STRING(method, "PUT", 1);
phalcon_call_method_p3(return_value, this_ptr, "_addroute", pattern, paths, method);
RETURN_MM();
phalcon_mvc_router_group_add_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, phalcon_interned_PUT);
}

/**
Expand All @@ -425,20 +395,7 @@ PHP_METHOD(Phalcon_Mvc_Router_Group, addPut){
*/
PHP_METHOD(Phalcon_Mvc_Router_Group, addPatch){

zval *pattern, *paths = NULL, *method;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 1, &pattern, &paths);

if (!paths) {
paths = PHALCON_GLOBAL(z_null);
}

PHALCON_INIT_VAR(method);
ZVAL_STRING(method, "PATCH", 1);
phalcon_call_method_p3(return_value, this_ptr, "_addroute", pattern, paths, method);
RETURN_MM();
phalcon_mvc_router_group_add_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, phalcon_interned_PATCH);
}

/**
Expand All @@ -450,20 +407,7 @@ PHP_METHOD(Phalcon_Mvc_Router_Group, addPatch){
*/
PHP_METHOD(Phalcon_Mvc_Router_Group, addDelete){

zval *pattern, *paths = NULL, *method;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 1, &pattern, &paths);

if (!paths) {
paths = PHALCON_GLOBAL(z_null);
}

PHALCON_INIT_VAR(method);
ZVAL_STRING(method, "DELETE", 1);
phalcon_call_method_p3(return_value, this_ptr, "_addroute", pattern, paths, method);
RETURN_MM();
phalcon_mvc_router_group_add_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, phalcon_interned_DELETE);
}

/**
Expand All @@ -475,20 +419,7 @@ PHP_METHOD(Phalcon_Mvc_Router_Group, addDelete){
*/
PHP_METHOD(Phalcon_Mvc_Router_Group, addOptions){

zval *pattern, *paths = NULL, *method;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 1, &pattern, &paths);

if (!paths) {
paths = PHALCON_GLOBAL(z_null);
}

PHALCON_INIT_VAR(method);
ZVAL_STRING(method, "OPTIONS", 1);
phalcon_call_method_p3(return_value, this_ptr, "_addroute", pattern, paths, method);
RETURN_MM();
phalcon_mvc_router_group_add_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, phalcon_interned_OPTIONS);
}

/**
Expand All @@ -500,20 +431,7 @@ PHP_METHOD(Phalcon_Mvc_Router_Group, addOptions){
*/
PHP_METHOD(Phalcon_Mvc_Router_Group, addHead){

zval *pattern, *paths = NULL, *method;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 1, &pattern, &paths);

if (!paths) {
paths = PHALCON_GLOBAL(z_null);
}

PHALCON_INIT_VAR(method);
ZVAL_STRING(method, "HEAD", 1);
phalcon_call_method_p3(return_value, this_ptr, "_addroute", pattern, paths, method);
RETURN_MM();
phalcon_mvc_router_group_add_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, phalcon_interned_HEAD);
}

/**
Expand All @@ -523,13 +441,10 @@ PHP_METHOD(Phalcon_Mvc_Router_Group, clear){

zval *empty_routes;

PHALCON_MM_GROW();

PHALCON_INIT_VAR(empty_routes);
MAKE_STD_ZVAL(empty_routes);
array_init(empty_routes);
phalcon_update_property_this(this_ptr, SL("_routes"), empty_routes TSRMLS_CC);

PHALCON_MM_RESTORE();
zval_ptr_dtor(&empty_routes);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions ext/mvc/router/route.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Router_Route){
zend_declare_property_null(phalcon_mvc_router_route_ce, SL("_id"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_mvc_router_route_ce, SL("_name"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_mvc_router_route_ce, SL("_beforeMatch"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_mvc_router_route_ce, SL("_group"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_mvc_router_route_ce, SL("_uniqueId"), ZEND_ACC_STATIC|ZEND_ACC_PROTECTED TSRMLS_CC);

zend_class_implements(phalcon_mvc_router_route_ce TSRMLS_CC, 1, phalcon_mvc_router_routeinterface_ce);
Expand Down Expand Up @@ -671,6 +672,33 @@ PHP_METHOD(Phalcon_Mvc_Router_Route, getHostname){
RETURN_MEMBER(this_ptr, "_hostname");
}

/**
* Sets the group associated with the route
*
* @param Phalcon\Mvc\Router\Group $group
* @return Phalcon\Mvc\RouteInterface
*/
PHP_METHOD(Phalcon_Mvc_Router_Route, setGroup) {

zval *group;

phalcon_fetch_params(0, 1, 0, &group);
PHALCON_VERIFY_CLASS_EX(group, phalcon_mvc_router_group_ce, phalcon_mvc_router_exception_ce, 0);

phalcon_update_property_this(getThis(), SL("_group"), group TSRMLS_CC);
RETURN_THISW();
}

/**
* Returns the group associated with the route
*
* @return Phalcon\Mvc\Router\Group|null
*/
PHP_METHOD(Phalcon_Mvc_Router_Route, getGroup) {

RETURN_MEMBER(this_ptr, "_group");
}

/**
* Adds a converter to perform an additional transformation for certain parameter
*
Expand Down
8 changes: 8 additions & 0 deletions ext/mvc/router/route.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ PHP_METHOD(Phalcon_Mvc_Router_Route, setHttpMethods);
PHP_METHOD(Phalcon_Mvc_Router_Route, getHttpMethods);
PHP_METHOD(Phalcon_Mvc_Router_Route, setHostname);
PHP_METHOD(Phalcon_Mvc_Router_Route, getHostname);
PHP_METHOD(Phalcon_Mvc_Router_Route, setGroup);
PHP_METHOD(Phalcon_Mvc_Router_Route, getGroup);
PHP_METHOD(Phalcon_Mvc_Router_Route, convert);
PHP_METHOD(Phalcon_Mvc_Router_Route, getConverters);
PHP_METHOD(Phalcon_Mvc_Router_Route, reset);
Expand Down Expand Up @@ -65,6 +67,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_router_route_setname, 0, 0, 1)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_router_route_setgroup, 0, 0, 1)
ZEND_ARG_INFO(0, group)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_router_route_beforematch, 0, 0, 1)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -100,6 +106,8 @@ PHALCON_INIT_FUNCS(phalcon_mvc_router_route_method_entry){
PHP_ME(Phalcon_Mvc_Router_Route, getHttpMethods, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Router_Route, setHostname, arginfo_phalcon_mvc_router_route_sethostname, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Router_Route, getHostname, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Router_Route, setGroup, arginfo_phalcon_mvc_router_route_setgroup, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Router_Route, getGroup, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Router_Route, convert, arginfo_phalcon_mvc_router_route_convert, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Router_Route, getConverters, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Router_Route, reset, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
Expand Down
1 change: 1 addition & 0 deletions unit-tests/RouterMvcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ public function testGroups()
$this->assertEquals($paths['module'], $router->getModuleName());
$this->assertEquals($paths['controller'], $router->getControllerName());
$this->assertEquals($paths['action'], $router->getActionName());
$this->assertEquals($blog, $router->getMatchedRoute()->getGroup());
}
}

Expand Down