From 539246d442d8e2d804042fcd04648eb45c6e21e1 Mon Sep 17 00:00:00 2001 From: Vladimir Kolesnikov Date: Thu, 3 Oct 2013 07:35:16 +0300 Subject: [PATCH 1/3] Implement #907 --- ext/mvc/view.c | 65 ++++++++++++++++++++++++----------------- ext/mvc/view.h | 4 ++- ext/mvc/viewinterface.c | 7 +++++ ext/mvc/viewinterface.h | 1 + 4 files changed, 50 insertions(+), 27 deletions(-) diff --git a/ext/mvc/view.c b/ext/mvc/view.c index 1ca19ffc96f..93ce734576c 100644 --- a/ext/mvc/view.c +++ b/ext/mvc/view.c @@ -77,6 +77,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_View){ zend_declare_property_string(phalcon_mvc_view_ce, SL("_basePath"), "", ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_string(phalcon_mvc_view_ce, SL("_content"), "", ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_long(phalcon_mvc_view_ce, SL("_renderLevel"), 5, ZEND_ACC_PROTECTED TSRMLS_CC); + zend_declare_property_long(phalcon_mvc_view_ce, SL("_currentRenderLevel"), 0, ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_null(phalcon_mvc_view_ce, SL("_disabledLevels"), ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_null(phalcon_mvc_view_ce, SL("_viewParams"), ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_null(phalcon_mvc_view_ce, SL("_layout"), ZEND_ACC_PROTECTED TSRMLS_CC); @@ -240,7 +241,17 @@ PHP_METHOD(Phalcon_Mvc_View, setBasePath){ } /** - * Return the current render level + * Returns the render level for the view + * + * @return int + */ +PHP_METHOD(Phalcon_Mvc_View, getCurrentRenderLevel) { + + RETURN_MEMBER(getThis(), "_currentRenderLevel"); +} + +/** + * Returns the render level for the view * * @return int */ @@ -386,7 +397,7 @@ PHP_METHOD(Phalcon_Mvc_View, setTemplateBefore){ if (Z_TYPE_P(template_before) != IS_ARRAY) { PHALCON_INIT_VAR(array_template); array_init_size(array_template, 1); - phalcon_array_append(&array_template, template_before, PH_SEPARATE); + phalcon_array_append(&array_template, template_before, 0); phalcon_update_property_this(this_ptr, SL("_templatesBefore"), array_template TSRMLS_CC); } else { phalcon_update_property_this(this_ptr, SL("_templatesBefore"), template_before TSRMLS_CC); @@ -424,7 +435,7 @@ PHP_METHOD(Phalcon_Mvc_View, setTemplateAfter){ if (Z_TYPE_P(template_after) != IS_ARRAY) { PHALCON_INIT_VAR(array_template); array_init_size(array_template, 1); - phalcon_array_append(&array_template, template_after, PH_SEPARATE); + phalcon_array_append(&array_template, template_after, 0); phalcon_update_property_this(this_ptr, SL("_templatesAfter"), array_template TSRMLS_CC); } else { phalcon_update_property_this(this_ptr, SL("_templatesAfter"), template_after TSRMLS_CC); @@ -604,12 +615,9 @@ PHP_METHOD(Phalcon_Mvc_View, getParams){ */ PHP_METHOD(Phalcon_Mvc_View, start){ - - PHALCON_MM_GROW(); - phalcon_update_property_null(this_ptr, SL("_content") TSRMLS_CC); phalcon_ob_start(TSRMLS_C); - RETURN_THIS(); + RETURN_THISW(); } /** @@ -652,7 +660,7 @@ PHP_METHOD(Phalcon_Mvc_View, _loadTemplateEngines){ object_init_ex(php_engine, phalcon_mvc_view_engine_php_ce); phalcon_call_method_p2_noret(php_engine, "__construct", this_ptr, dependency_injector); - phalcon_array_update_string(&engines, SL(".phtml"), &php_engine, PH_COPY | PH_SEPARATE); + phalcon_array_update_string(&engines, SL(".phtml"), &php_engine, PH_COPY); } else { if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_view_exception_ce, "A dependency injector container is required to obtain the application services"); @@ -661,8 +669,8 @@ PHP_METHOD(Phalcon_Mvc_View, _loadTemplateEngines){ PHALCON_INIT_VAR(arguments); array_init_size(arguments, 2); - phalcon_array_append(&arguments, this_ptr, PH_SEPARATE); - phalcon_array_append(&arguments, dependency_injector, PH_SEPARATE); + phalcon_array_append(&arguments, this_ptr, 0); + phalcon_array_append(&arguments, dependency_injector, 0); phalcon_is_iterable(registered_engines, &ah0, &hp0, 0, 0); @@ -697,7 +705,7 @@ PHP_METHOD(Phalcon_Mvc_View, _loadTemplateEngines){ return; } } - phalcon_array_update_zval(&engines, extension, &engine_object, PH_COPY | PH_SEPARATE); + phalcon_array_update_zval(&engines, extension, &engine_object, PH_COPY | 0); zend_hash_move_forward_ex(ah0, &hp0); } @@ -972,6 +980,8 @@ PHP_METHOD(Phalcon_Mvc_View, render){ params = PHALCON_GLOBAL(z_null); } + phalcon_update_property_this(this_ptr, SL("_currentRenderLevel"), PHALCON_GLOBAL(z_zero) TSRMLS_CC); + /** * If the view is disabled we simply update the buffer from any output produced in * the controller @@ -1100,6 +1110,7 @@ PHP_METHOD(Phalcon_Mvc_View, render){ */ if (PHALCON_GE_LONG(render_level, 1)) { if (!phalcon_array_isset_long(disabled_levels, 1)) { + phalcon_update_property_long(this_ptr, SL("_currentRenderLevel"), 1 TSRMLS_CC); phalcon_call_method_p5_noret(this_ptr, "_enginerender", engines, render_view, silence, PHALCON_GLOBAL(z_true), cache); } } @@ -1109,6 +1120,7 @@ PHP_METHOD(Phalcon_Mvc_View, render){ */ if (PHALCON_GE_LONG(render_level, 2)) { if (!phalcon_array_isset_long(disabled_levels, 2)) { + phalcon_update_property_long(this_ptr, SL("_currentRenderLevel"), 2 TSRMLS_CC); PHALCON_OBS_VAR(templates_before); phalcon_read_property_this(&templates_before, this_ptr, SL("_templatesBefore"), PH_NOISY_CC); @@ -1144,6 +1156,8 @@ PHP_METHOD(Phalcon_Mvc_View, render){ */ if (PHALCON_GE_LONG(render_level, 3)) { if (!phalcon_array_isset_long(disabled_levels, 3)) { + phalcon_update_property_long(this_ptr, SL("_currentRenderLevel"), 3 TSRMLS_CC); + PHALCON_INIT_NVAR(view_temp_path); PHALCON_CONCAT_VV(view_temp_path, layouts_dir, layout_name); phalcon_call_method_p5_noret(this_ptr, "_enginerender", engines, view_temp_path, silence, PHALCON_GLOBAL(z_true), cache); @@ -1155,6 +1169,7 @@ PHP_METHOD(Phalcon_Mvc_View, render){ */ if (PHALCON_GE_LONG(render_level, 4)) { if (!phalcon_array_isset_long(disabled_levels, 4)) { + phalcon_update_property_long(this_ptr, SL("_currentRenderLevel"), 4 TSRMLS_CC); /** * Templates after must be an array @@ -1190,11 +1205,15 @@ PHP_METHOD(Phalcon_Mvc_View, render){ */ if (PHALCON_GE_LONG(render_level, 5)) { if (!phalcon_array_isset_long(disabled_levels, 5)) { + phalcon_update_property_long(this_ptr, SL("_currentRenderLevel"), 5 TSRMLS_CC); + PHALCON_OBS_VAR(main_view); phalcon_read_property_this(&main_view, this_ptr, SL("_mainView"), PH_NOISY_CC); phalcon_call_method_p5_noret(this_ptr, "_enginerender", engines, main_view, silence, PHALCON_GLOBAL(z_true), cache); } } + + phalcon_update_property_this(this_ptr, SL("_currentRenderLevel"), PHALCON_GLOBAL(z_zero) TSRMLS_CC); /** * Store the data in the cache @@ -1272,10 +1291,10 @@ PHP_METHOD(Phalcon_Mvc_View, pick){ } PHALCON_INIT_NVAR(pick_view); - array_init_size(pick_view, 1); - phalcon_array_append(&pick_view, render_view, PH_SEPARATE); + array_init_size(pick_view, 2); + phalcon_array_append(&pick_view, render_view, 0); if (Z_TYPE_P(layout) != IS_NULL) { - phalcon_array_append(&pick_view, layout, PH_SEPARATE); + phalcon_array_append(&pick_view, layout, 0); } } phalcon_update_property_this(this_ptr, SL("_pickView"), pick_view TSRMLS_CC); @@ -1434,7 +1453,7 @@ PHP_METHOD(Phalcon_Mvc_View, getRender){ if (Z_TYPE_P(config_callback) == IS_OBJECT) { PHALCON_INIT_NVAR(params); array_init_size(params, 1); - phalcon_array_append(¶ms, view, PH_SEPARATE); + phalcon_array_append(¶ms, view, 0); PHALCON_INIT_VAR(status); PHALCON_CALL_USER_FUNC_ARRAY(status, config_callback, params); @@ -1470,11 +1489,8 @@ PHP_METHOD(Phalcon_Mvc_View, getRender){ */ PHP_METHOD(Phalcon_Mvc_View, finish){ - - PHALCON_MM_GROW(); - phalcon_ob_end_clean(TSRMLS_C); - RETURN_THIS(); + RETURN_THISW(); } /** @@ -1820,15 +1836,12 @@ PHP_METHOD(Phalcon_Mvc_View, __isset){ zval *key, *params; - PHALCON_MM_GROW(); - - phalcon_fetch_params(1, 1, 0, &key); + phalcon_fetch_params(0, 1, 0, &key); - PHALCON_OBS_VAR(params); - phalcon_read_property_this(¶ms, this_ptr, SL("_viewParams"), PH_NOISY_CC); + params = phalcon_fetch_nproperty_this(this_ptr, SL("_viewParams"), PH_NOISY_CC); if (phalcon_array_isset(params, key)) { - RETURN_MM_TRUE; + RETURN_TRUE; } - RETURN_MM_FALSE; + RETURN_FALSE; } diff --git a/ext/mvc/view.h b/ext/mvc/view.h index b1124dbd181..81ad6d3020b 100644 --- a/ext/mvc/view.h +++ b/ext/mvc/view.h @@ -29,6 +29,7 @@ PHP_METHOD(Phalcon_Mvc_View, getLayoutsDir); PHP_METHOD(Phalcon_Mvc_View, setPartialsDir); PHP_METHOD(Phalcon_Mvc_View, getPartialsDir); PHP_METHOD(Phalcon_Mvc_View, setBasePath); +PHP_METHOD(Phalcon_Mvc_View, getCurrentRenderLevel); PHP_METHOD(Phalcon_Mvc_View, getRenderLevel); PHP_METHOD(Phalcon_Mvc_View, setRenderLevel); PHP_METHOD(Phalcon_Mvc_View, disableLevel); @@ -187,7 +188,8 @@ PHALCON_INIT_FUNCS(phalcon_mvc_view_method_entry){ PHP_ME(Phalcon_Mvc_View, getLayoutsDir, NULL, ZEND_ACC_PUBLIC) PHP_ME(Phalcon_Mvc_View, setPartialsDir, arginfo_phalcon_mvc_view_setpartialsdir, ZEND_ACC_PUBLIC) PHP_ME(Phalcon_Mvc_View, getPartialsDir, NULL, ZEND_ACC_PUBLIC) - PHP_ME(Phalcon_Mvc_View, setBasePath, arginfo_phalcon_mvc_view_setbasepath, ZEND_ACC_PUBLIC) + PHP_ME(Phalcon_Mvc_View, setBasePath, arginfo_phalcon_mvc_view_setbasepath, ZEND_ACC_PUBLIC) + PHP_ME(Phalcon_Mvc_View, getCurrentRenderLevel, NULL, ZEND_ACC_PUBLIC) PHP_ME(Phalcon_Mvc_View, getRenderLevel, NULL, ZEND_ACC_PUBLIC) PHP_ME(Phalcon_Mvc_View, setRenderLevel, arginfo_phalcon_mvc_view_setrenderlevel, ZEND_ACC_PUBLIC) PHP_ME(Phalcon_Mvc_View, disableLevel, arginfo_phalcon_mvc_view_disablelevel, ZEND_ACC_PUBLIC) diff --git a/ext/mvc/viewinterface.c b/ext/mvc/viewinterface.c index ab27cc69271..8197a48e1e2 100644 --- a/ext/mvc/viewinterface.c +++ b/ext/mvc/viewinterface.c @@ -86,6 +86,13 @@ PHALCON_DOC_METHOD(Phalcon_Mvc_ViewInterface, getPartialsDir); */ PHALCON_DOC_METHOD(Phalcon_Mvc_ViewInterface, setBasePath); +/** + * Gets the current render level + * + * @return string + */ +PHALCON_DOC_METHOD(Phalcon_Mvc_ViewInterface, getCurrentRenderLevel); + /** * Gets the render level for the view * diff --git a/ext/mvc/viewinterface.h b/ext/mvc/viewinterface.h index 4e7a9d6843e..74f1c21f90a 100644 --- a/ext/mvc/viewinterface.h +++ b/ext/mvc/viewinterface.h @@ -101,6 +101,7 @@ PHALCON_INIT_FUNCS(phalcon_mvc_viewinterface_method_entry){ PHP_ABSTRACT_ME(Phalcon_Mvc_ViewInterface, setPartialsDir, arginfo_phalcon_mvc_viewinterface_setpartialsdir) PHP_ABSTRACT_ME(Phalcon_Mvc_ViewInterface, getPartialsDir, NULL) PHP_ABSTRACT_ME(Phalcon_Mvc_ViewInterface, setBasePath, arginfo_phalcon_mvc_viewinterface_setbasepath) + PHP_ABSTRACT_ME(Phalcon_Mvc_ViewInterface, getCurrentRenderLevel, NULL) PHP_ABSTRACT_ME(Phalcon_Mvc_ViewInterface, getRenderLevel, NULL) PHP_ABSTRACT_ME(Phalcon_Mvc_ViewInterface, setRenderLevel, arginfo_phalcon_mvc_viewinterface_setrenderlevel) PHP_ABSTRACT_ME(Phalcon_Mvc_ViewInterface, setMainView, arginfo_phalcon_mvc_viewinterface_setmainview) From 5d417f02242ee1c2fabe57db28f42c92b7ec0827 Mon Sep 17 00:00:00 2001 From: Vladimir Kolesnikov Date: Thu, 3 Oct 2013 08:02:07 +0300 Subject: [PATCH 2/3] Removed executable bit from views --- unit-tests/views/layouts/test.phtml | 0 unit-tests/views/layouts/test3.phtml | 0 unit-tests/views/test2/index.phtml | 0 unit-tests/views/test3/another.phtml | 0 unit-tests/views/test3/other.phtml | 0 5 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 unit-tests/views/layouts/test.phtml mode change 100755 => 100644 unit-tests/views/layouts/test3.phtml mode change 100755 => 100644 unit-tests/views/test2/index.phtml mode change 100755 => 100644 unit-tests/views/test3/another.phtml mode change 100755 => 100644 unit-tests/views/test3/other.phtml diff --git a/unit-tests/views/layouts/test.phtml b/unit-tests/views/layouts/test.phtml old mode 100755 new mode 100644 diff --git a/unit-tests/views/layouts/test3.phtml b/unit-tests/views/layouts/test3.phtml old mode 100755 new mode 100644 diff --git a/unit-tests/views/test2/index.phtml b/unit-tests/views/test2/index.phtml old mode 100755 new mode 100644 diff --git a/unit-tests/views/test3/another.phtml b/unit-tests/views/test3/another.phtml old mode 100755 new mode 100644 diff --git a/unit-tests/views/test3/other.phtml b/unit-tests/views/test3/other.phtml old mode 100755 new mode 100644 From 5b7f9e217f345c6b938b11b5c034330ea9b49f8c Mon Sep 17 00:00:00 2001 From: Vladimir Kolesnikov Date: Thu, 3 Oct 2013 08:00:50 +0300 Subject: [PATCH 3/3] Unit test --- unit-tests/ViewTest.php | 88 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/unit-tests/ViewTest.php b/unit-tests/ViewTest.php index 0da5cc013e8..6f4bca64ea9 100644 --- a/unit-tests/ViewTest.php +++ b/unit-tests/ViewTest.php @@ -20,6 +20,30 @@ use Phalcon\Mvc\View as View; +class ViewAfterRenderListener +{ + private $_levels = array(); + + public function afterRenderView($event, $view) + { + if ('afterRenderView' == $event->getType()) { + $this->_levels[] = $view->getCurrentRenderLevel(); + } + + return true; + } + + public function reset() + { + $this->_levels = array(); + } + + public function getLevels() + { + return join(',', $this->_levels); + } +} + class ViewTest extends PHPUnit_Framework_TestCase { @@ -203,10 +227,72 @@ public function testDisableLevels() View::LEVEL_BEFORE_TEMPLATE => true, View::LEVEL_LAYOUT => true, View::LEVEL_AFTER_TEMPLATE => true, - View::LEVEL_MAIN_LAYOUT => true + View::LEVEL_MAIN_LAYOUT => true )); $this->assertEquals($view->getContent(), '
Action
'); } + public function testIssue907() + { + $view = new \Phalcon\Mvc\View(); + $view->setBasePath(__DIR__.'/../'); + + $view->setViewsDir('unit-tests/views/'); + + $listener = new \ViewAfterRenderListener(); + $eventsManager = new \Phalcon\Events\Manager(); + $eventsManager->attach('view', $listener); + + $view->setEventsManager($eventsManager); + + $view->start(); + $view->render('test3', 'other'); + $view->finish(); + $this->assertEquals($view->getContent(), 'lolhere'.PHP_EOL); + $this->assertEquals('1,3,5', $listener->getLevels()); + $listener->reset(); + + //Templates + $view->setTemplateAfter('test'); + + $view->start(); + $view->render('test3', 'other'); + $view->finish(); + + $this->assertEquals($view->getContent(), 'zuplolhere' . PHP_EOL); + $this->assertEquals('1,3,4,5', $listener->getLevels()); + $listener->reset(); + + $view->cleanTemplateAfter(); + + //Render Levels + $view->setRenderLevel(View::LEVEL_MAIN_LAYOUT); + + $view->start(); + $view->render('test3', 'other'); + $view->finish(); + $this->assertEquals($view->getContent(), 'lolhere' . PHP_EOL); + $this->assertEquals('1,3,5', $listener->getLevels()); + $listener->reset(); + + $view->setRenderLevel(View::LEVEL_LAYOUT); + + $view->start(); + $view->render('test3', 'other'); + $view->finish(); + $this->assertEquals($view->getContent(), 'lolhere'); + $this->assertEquals('1,3', $listener->getLevels()); + $listener->reset(); + + $view->setRenderLevel(View::LEVEL_ACTION_VIEW); + + $view->start(); + $view->render('test3', 'other'); + $view->finish(); + $this->assertEquals($view->getContent(), 'here'); + $this->assertEquals('1', $listener->getLevels()); + $listener->reset(); + + } }