Skip to content

Commit

Permalink
Improve the templates directory handling
Browse files Browse the repository at this point in the history
  • Loading branch information
laruence committed Nov 6, 2012
1 parent 64b3d1c commit fb15c4a
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 100 deletions.
2 changes: 2 additions & 0 deletions package2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</stability>
<license uri="http://www.php.net/license">PHP</license>
<notes>
- Improve the templates directory handling
- Fixed bug #63438 (Strange behavior with nested rendering)
</notes>
<contents>
Expand Down Expand Up @@ -153,6 +154,7 @@
<file name="058.phpt" role="test" />
<file name="059.phpt" role="test" />
<file name="061.phpt" role="test" />
<file name="062.phpt" role="test" />
<file name="build.inc" role="test" />
<file name="bug61493.phpt" role="test" />
<file name="bug63381.phpt" role="test" />
Expand Down
3 changes: 2 additions & 1 deletion php_yaf.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: php_yaf.h 328213 2012-11-01 05:29:28Z laruence $ */
/* $Id: php_yaf.h 328262 2012-11-06 10:31:55Z laruence $ */

#ifndef PHP_YAF_H
#define PHP_YAF_H
Expand Down Expand Up @@ -88,6 +88,7 @@ ZEND_BEGIN_MODULE_GLOBALS(yaf)
char *local_library;
char *local_namespaces;
char *global_library;
char *view_directory;
char *view_ext;
char *default_module;
char *default_controller;
Expand Down
4 changes: 2 additions & 2 deletions tests/059.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ require "build.inc";
shutdown();
?>
--EXPECTF--
string(%d) "%sviews"
string(%d) "%sapplication%cviews"
Yaf_View_Simple Object
(
[_tpl_vars:protected] => Array
(
)

[_tpl_dir:protected] => %sapplication%cviews
[_tpl_dir:protected] =>
[_options:protected] =>
)
dummyfoobar
28 changes: 28 additions & 0 deletions tests/062.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Check for Yaf_View_Simple and application's template directory
--SKIPIF--
<?php if (!extension_loaded("yaf")) print "skip"; ?>
--INI--
yaf.library="/php/global/dir"
--FILE--
<?php
$view = new Yaf_View_Simple(dirname(__FILE__));
try {
$view = new Yaf_View_Simple("");
} catch (Yaf_Exception_TypeError $e) {
var_dump($e->getMessage());
}

$config = array(
"application" => array(
"directory" => dirname(__FILE__),
),
);
$app = new Yaf_Application($config);

$view = Yaf_Dispatcher::getInstance()->initView(array());
var_dump($view->getScriptPath());
?>
--EXPECTF--
string(%d) "Expects an absolute path for templates directory"
NULL
82 changes: 50 additions & 32 deletions views/simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: simple.c 328257 2012-11-06 09:06:38Z laruence $ */
/* $Id: simple.c 328262 2012-11-06 10:31:55Z laruence $ */

#include "main/php_output.h"

Expand Down Expand Up @@ -207,11 +207,17 @@ yaf_view_t * yaf_view_simple_instance(yaf_view_t *view, zval *tpl_dir, zval *opt
zend_update_property(yaf_view_simple_ce, instance, ZEND_STRL(YAF_VIEW_PROPERTY_NAME_TPLVARS), tpl_vars TSRMLS_CC);
zval_ptr_dtor(&tpl_vars);

if (tpl_dir && Z_TYPE_P(tpl_dir) == IS_STRING && IS_ABSOLUTE_PATH(Z_STRVAL_P(tpl_dir), Z_STRLEN_P(tpl_dir))) {
zend_update_property(yaf_view_simple_ce, instance, ZEND_STRL(YAF_VIEW_PROPERTY_NAME_TPLDIR), tpl_dir TSRMLS_CC);
} else {
zend_update_property_stringl(yaf_view_simple_ce, instance, ZEND_STRL(YAF_VIEW_PROPERTY_NAME_TPLDIR), "", 0 TSRMLS_CC);
}
if (tpl_dir && Z_TYPE_P(tpl_dir) == IS_STRING) {
if (IS_ABSOLUTE_PATH(Z_STRVAL_P(tpl_dir), Z_STRLEN_P(tpl_dir))) {
zend_update_property(yaf_view_simple_ce, instance, ZEND_STRL(YAF_VIEW_PROPERTY_NAME_TPLDIR), tpl_dir TSRMLS_CC);
} else {
if (!view) {
zval_ptr_dtor(&instance);
}
yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "Expects an absolute path for templates directory");
return NULL;
}
}

if (options && IS_ARRAY == Z_TYPE_P(options)) {
zend_update_property(yaf_view_simple_ce, instance, ZEND_STRL(YAF_VIEW_PROPERTY_NAME_OPTS), options TSRMLS_CC);
Expand Down Expand Up @@ -294,28 +300,32 @@ int yaf_view_simple_render(yaf_view_t *view, zval *tpl, zval * vars, zval *ret T
} else {
zval *tpl_dir = zend_read_property(yaf_view_simple_ce, view, ZEND_STRL(YAF_VIEW_PROPERTY_NAME_TPLDIR), 0 TSRMLS_CC);

if (ZVAL_IS_NULL(tpl_dir)) {
if (IS_STRING != Z_TYPE_P(tpl_dir)) {
if (YAF_G(view_directory)) {
len = spprintf(&script, 0, "%s%c%s", YAF_G(view_directory), DEFAULT_SLASH, Z_STRVAL_P(tpl));
} else {
#if ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4))
YAF_RESTORE_OUTPUT_BUFFER(buffer);
CG(short_tags) = short_open_tag;
YAF_RESTORE_OUTPUT_BUFFER(buffer);
CG(short_tags) = short_open_tag;
#else
php_output_end(TSRMLS_C);
php_output_end(TSRMLS_C);
#endif

if (calling_symbol_table) {
zend_hash_destroy(EG(active_symbol_table));
FREE_HASHTABLE(EG(active_symbol_table));
EG(active_symbol_table) = calling_symbol_table;
}
if (calling_symbol_table) {
zend_hash_destroy(EG(active_symbol_table));
FREE_HASHTABLE(EG(active_symbol_table));
EG(active_symbol_table) = calling_symbol_table;
}

yaf_trigger_error(YAF_ERR_NOTFOUND_VIEW TSRMLS_CC,
"Could not determine the view script path, you should call %s::setScriptPath to specific it",
yaf_view_simple_ce->name);
return 0;
yaf_trigger_error(YAF_ERR_NOTFOUND_VIEW TSRMLS_CC,
"Could not determine the view script path, you should call %s::setScriptPath to specific it",
yaf_view_simple_ce->name);
return 0;
}
} else {
len = spprintf(&script, 0, "%s%c%s", Z_STRVAL_P(tpl_dir), DEFAULT_SLASH, Z_STRVAL_P(tpl));
}

len = spprintf(&script, 0, "%s%c%s", Z_STRVAL_P(tpl_dir), DEFAULT_SLASH, Z_STRVAL_P(tpl));

if (yaf_loader_import(script, len + 1, 0 TSRMLS_CC) == 0) {
#if ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4))
YAF_RESTORE_OUTPUT_BUFFER(buffer);
Expand Down Expand Up @@ -429,22 +439,27 @@ int yaf_view_simple_display(yaf_view_t *view, zval *tpl, zval *vars, zval *ret T
} else {
zval *tpl_dir = zend_read_property(yaf_view_simple_ce, view, ZEND_STRL(YAF_VIEW_PROPERTY_NAME_TPLDIR), 0 TSRMLS_CC);

if (ZVAL_IS_NULL(tpl_dir)) {
yaf_trigger_error(YAF_ERR_NOTFOUND_VIEW TSRMLS_CC,
"Could not determine the view script path, you should call %s::setScriptPath to specific it", yaf_view_simple_ce->name);
if (IS_STRING != Z_TYPE_P(tpl_dir)) {
if (YAF_G(view_directory)) {
len = spprintf(&script, 0, "%s%c%s", YAF_G(view_directory), DEFAULT_SLASH, Z_STRVAL_P(tpl));
} else {
yaf_trigger_error(YAF_ERR_NOTFOUND_VIEW TSRMLS_CC,
"Could not determine the view script path, you should call %s::setScriptPath to specific it", yaf_view_simple_ce->name);
#if ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4))
CG(short_tags) = short_open_tag;
CG(short_tags) = short_open_tag;
#endif
EG(scope) = old_scope;
if (calling_symbol_table) {
zend_hash_destroy(EG(active_symbol_table));
FREE_HASHTABLE(EG(active_symbol_table));
EG(active_symbol_table) = calling_symbol_table;
EG(scope) = old_scope;
if (calling_symbol_table) {
zend_hash_destroy(EG(active_symbol_table));
FREE_HASHTABLE(EG(active_symbol_table));
EG(active_symbol_table) = calling_symbol_table;
}
return 0;
}
return 0;
} else {
len = spprintf(&script, 0, "%s%c%s", Z_STRVAL_P(tpl_dir), DEFAULT_SLASH, Z_STRVAL_P(tpl));
}

len = spprintf(&script, 0, "%s%c%s", Z_STRVAL_P(tpl_dir), DEFAULT_SLASH, Z_STRVAL_P(tpl));
if (yaf_loader_import(script, len + 1, 0 TSRMLS_CC) == 0) {
yaf_trigger_error(YAF_ERR_NOTFOUND_VIEW TSRMLS_CC, "Failed opening template %s: %s", script, strerror(errno));
#if ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4))
Expand Down Expand Up @@ -687,6 +702,9 @@ PHP_METHOD(yaf_view_simple, setScriptPath) {
*/
PHP_METHOD(yaf_view_simple, getScriptPath) {
zval *tpl_dir = zend_read_property(yaf_view_simple_ce, getThis(), ZEND_STRL(YAF_VIEW_PROPERTY_NAME_TPLDIR), 0 TSRMLS_CC);
if (IS_STRING != Z_TYPE_P(tpl_dir) && YAF_G(view_directory)) {
RETURN_STRING(YAF_G(view_directory), 1);
}
RETURN_ZVAL(tpl_dir, 1, 0);
}
/* }}} */
Expand Down
36 changes: 20 additions & 16 deletions yaf.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: yaf.c 327715 2012-09-20 15:24:26Z laruence $ */
/* $Id: yaf.c 328262 2012-11-06 10:31:55Z laruence $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
Expand Down Expand Up @@ -91,8 +91,8 @@ PHP_GINIT_FUNCTION(yaf)
yaf_globals->autoload_started = 0;
yaf_globals->configs = NULL;
yaf_globals->directory = NULL;
yaf_globals->local_library = NULL;
yaf_globals->ext = YAF_DEFAULT_EXT;
yaf_globals->local_library = NULL;
yaf_globals->ext = YAF_DEFAULT_EXT;
yaf_globals->view_ext = YAF_DEFAULT_VIEW_EXT;
yaf_globals->default_module = YAF_ROUTER_DEFAULT_MODULE;
yaf_globals->default_controller = YAF_ROUTER_DEFAULT_CONTROLLER;
Expand Down Expand Up @@ -190,20 +190,21 @@ PHP_MSHUTDOWN_FUNCTION(yaf)
*/
PHP_RINIT_FUNCTION(yaf)
{
YAF_G(running) = 0;
YAF_G(in_exception) = 0;
YAF_G(throw_exception) = 1;
YAF_G(catch_exception) = 0;
YAF_G(directory) = NULL;
YAF_G(bootstrap) = NULL;
YAF_G(local_library) = NULL;
YAF_G(local_namespaces) = NULL;
YAF_G(modules) = NULL;
YAF_G(base_uri) = NULL;
YAF_G(running) = 0;
YAF_G(in_exception) = 0;
YAF_G(throw_exception) = 1;
YAF_G(catch_exception) = 0;
YAF_G(directory) = NULL;
YAF_G(bootstrap) = NULL;
YAF_G(local_library) = NULL;
YAF_G(local_namespaces) = NULL;
YAF_G(modules) = NULL;
YAF_G(base_uri) = NULL;
YAF_G(view_directory) = NULL;
#if ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4))
YAF_G(buffer) = NULL;
YAF_G(owrite_handler) = NULL;
YAF_G(buf_nesting) = 0;
YAF_G(buffer) = NULL;
YAF_G(owrite_handler) = NULL;
YAF_G(buf_nesting) = 0;
#endif

return SUCCESS;
Expand Down Expand Up @@ -232,6 +233,9 @@ PHP_RSHUTDOWN_FUNCTION(yaf)
if (YAF_G(base_uri)) {
efree(YAF_G(base_uri));
}
if (YAF_G(view_directory)) {
efree(YAF_G(view_directory));
}
YAF_G(default_route) = NULL;

return SUCCESS;
Expand Down
5 changes: 4 additions & 1 deletion yaf_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: yaf_controller.c 327816 2012-09-27 10:10:04Z laruence $ */
/* $Id: yaf_controller.c 328262 2012-11-06 10:31:55Z laruence $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
Expand Down Expand Up @@ -372,6 +372,9 @@ PHP_METHOD(yaf_controller, getViewpath) {
zval *view = zend_read_property(yaf_controller_ce, getThis(), ZEND_STRL(YAF_CONTROLLER_PROPERTY_NAME_VIEW), 1 TSRMLS_CC);
if ((view_ce = Z_OBJCE_P(view)) == yaf_view_simple_ce) {
zval *tpl_dir = zend_read_property(view_ce, view, ZEND_STRL(YAF_VIEW_PROPERTY_NAME_TPLDIR), 1 TSRMLS_CC);
if (IS_STRING != Z_TYPE_P(tpl_dir) && YAF_G(view_directory)) {
RETURN_STRING(YAF_G(view_directory), 1);
}
RETURN_ZVAL(tpl_dir, 1, 0);
} else {
zval *ret;
Expand Down
Loading

0 comments on commit fb15c4a

Please sign in to comment.