Skip to content

Commit

Permalink
Adding onConstruct/afterFetch to Mvc\Model/Mvc\Collection, improvemen…
Browse files Browse the repository at this point in the history
…ts to assets filtering
  • Loading branch information
phalcon committed Jun 17, 2013
1 parent 5222054 commit ff26d57
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 1,512 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,4 @@ ext/mvc/model/metadata/strategy/introspection.lo
ext/mvc/model/relationinterface.lo
ext/mvc/router/annotations.lo
ext/mvc/router/group.lo
unit-tests/annotations/cache/*.php
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
- Phalcon\Mvc\View now have a chaineable API
- Phalcon\Mvc\Micro now can optionally receive the DI in its constructor
- Now if a method receives an incorrect number of parameters an exception BadMethodCallException is thrown instead of a warning
- Phalcon\Mvc\Model/Phalcon\Mvc\Collection now checks for a method 'onConstruct' allowing the developer to execute initialization stuff every time a model/collection instance is created
- Phalcon\Mvc\Model/Phalcon\Mvc\Collection now checks for a method 'afterFetch' allowing the developer to execute initialization stuff every time a model/collection instance is created
in a findFirst/find operation is created
- Added Phalcon\Mvc\Collection::summatory easing the generation of this type of aggregation

1.1.0
- Improvements to the query builder allowing to define bound parameters in the "where" methods
Expand Down
44 changes: 44 additions & 0 deletions ext/assets/collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "kernel/object.h"
#include "kernel/fcall.h"
#include "kernel/array.h"
#include "kernel/concat.h"
#include "kernel/file.h"

/**
* Phalcon\Assets\Collection
Expand Down Expand Up @@ -112,6 +114,7 @@ PHP_METHOD(Phalcon_Assets_Collection, addCss){

if (!filter) {
PHALCON_INIT_VAR(filter);
ZVAL_BOOL(filter, 1);
}

if (!attributes) {
Expand Down Expand Up @@ -164,6 +167,7 @@ PHP_METHOD(Phalcon_Assets_Collection, addJs){

if (!filter) {
PHALCON_INIT_VAR(filter);
ZVAL_BOOL(filter, 1);
}

if (!attributes) {
Expand Down Expand Up @@ -555,3 +559,43 @@ PHP_METHOD(Phalcon_Assets_Collection, getJoin){
RETURN_MEMBER(this_ptr, "_join");
}

/**
* Returns the complete location where the joined/filtered collection must be written
*
* @param string $basePath
* @return string
*/
PHP_METHOD(Phalcon_Assets_Collection, getRealTargetPath){

zval *base_path = NULL, *target_path, *complete_path;
zval *real_complete_path;

PHALCON_MM_GROW();

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

if (!base_path) {
PHALCON_INIT_VAR(base_path);
}

PHALCON_OBS_VAR(target_path);
phalcon_read_property_this(&target_path, this_ptr, SL("_targetPath"), PH_NOISY_CC);

/**
* A base path for resources can be set in the assets manager
*/
PHALCON_INIT_VAR(complete_path);
PHALCON_CONCAT_VV(complete_path, base_path, target_path);

/**
* Get the real template path, the target path can optionally don't exist
*/
if (phalcon_file_exists(complete_path TSRMLS_CC) == SUCCESS) {
PHALCON_INIT_VAR(real_complete_path);
phalcon_call_func_p1(real_complete_path, "realpath", complete_path);
RETURN_CCTOR(real_complete_path);
}

RETURN_CTOR(complete_path);
}

6 changes: 6 additions & 0 deletions ext/assets/collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ PHP_METHOD(Phalcon_Assets_Collection, setFilters);
PHP_METHOD(Phalcon_Assets_Collection, getFilters);
PHP_METHOD(Phalcon_Assets_Collection, join);
PHP_METHOD(Phalcon_Assets_Collection, getJoin);
PHP_METHOD(Phalcon_Assets_Collection, getRealTargetPath);

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_assets_collection_add, 0, 0, 1)
ZEND_ARG_INFO(0, resource)
Expand Down Expand Up @@ -103,6 +104,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_assets_collection_join, 0, 0, 1)
ZEND_ARG_INFO(0, join)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_assets_collection_getrealtargetpath, 0, 0, 0)
ZEND_ARG_INFO(0, basePath)
ZEND_END_ARG_INFO()

PHALCON_INIT_FUNCS(phalcon_assets_collection_method_entry){
PHP_ME(Phalcon_Assets_Collection, add, arginfo_phalcon_assets_collection_add, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Assets_Collection, addCss, arginfo_phalcon_assets_collection_addcss, ZEND_ACC_PUBLIC)
Expand Down Expand Up @@ -131,6 +136,7 @@ PHALCON_INIT_FUNCS(phalcon_assets_collection_method_entry){
PHP_ME(Phalcon_Assets_Collection, getFilters, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Assets_Collection, join, arginfo_phalcon_assets_collection_join, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Assets_Collection, getJoin, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Assets_Collection, getRealTargetPath, arginfo_phalcon_assets_collection_getrealtargetpath, ZEND_ACC_PUBLIC)
PHP_FE_END
};

111 changes: 107 additions & 4 deletions ext/assets/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ PHP_METHOD(Phalcon_Assets_Manager, addCss){

if (!filter) {
PHALCON_INIT_VAR(filter);
ZVAL_BOOL(filter, 1);
}

if (!attributes) {
Expand Down Expand Up @@ -207,6 +208,7 @@ PHP_METHOD(Phalcon_Assets_Manager, addJs){

if (!filter) {
PHALCON_INIT_VAR(filter);
ZVAL_BOOL(filter, 1);
}

if (!attributes) {
Expand Down Expand Up @@ -466,6 +468,7 @@ PHP_METHOD(Phalcon_Assets_Manager, output){
zval *attributes = NULL, *parameters = NULL, *html = NULL, *content = NULL;
zval *must_filter = NULL, *filter = NULL, *filtered_content = NULL;
zval *source_path = NULL, *exception_message = NULL, *target_path = NULL;
zval *is_directory, *target_uri;
HashTable *ah0, *ah1;
HashPosition hp0, hp1;
zval **hd;
Expand Down Expand Up @@ -658,7 +661,7 @@ PHP_METHOD(Phalcon_Assets_Manager, output){
/**
* Only filter the resource if it's marked as 'filterable'
*/
if (!zend_is_true(must_filter)) {
if (zend_is_true(must_filter)) {

if (!phalcon_is_iterable(filters, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
return;
Expand Down Expand Up @@ -687,9 +690,10 @@ PHP_METHOD(Phalcon_Assets_Manager, output){
*/
if (zend_is_true(join)) {
if (Z_TYPE_P(filtered_joined_content) == IS_NULL) {
PHALCON_CPY_WRT(filtered_joined_content, filtered_content);
PHALCON_INIT_NVAR(filtered_joined_content);
PHALCON_CONCAT_VS(filtered_joined_content, filtered_content, ";");
} else {
phalcon_concat_self(&filtered_joined_content, filtered_content TSRMLS_CC);
PHALCON_SCONCAT_VS(filtered_joined_content, filtered_content, ";");
}
}

Expand All @@ -706,6 +710,8 @@ PHP_METHOD(Phalcon_Assets_Manager, output){
} else {
phalcon_concat_self(&filtered_joined_content, content TSRMLS_CC);
}
} else {
PHALCON_CPY_WRT(filtered_content, content);
}
}

Expand Down Expand Up @@ -751,7 +757,7 @@ PHP_METHOD(Phalcon_Assets_Manager, output){
*/
if (PHALCON_IS_EQUAL(target_path, source_path)) {
PHALCON_INIT_NVAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Resource '", source_path, "' have the same source and target paths");
PHALCON_CONCAT_SVS(exception_message, "Resource '", target_path, "' have the same source and target paths");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_assets_exception_ce, exception_message);
return;
}
Expand Down Expand Up @@ -780,6 +786,12 @@ PHP_METHOD(Phalcon_Assets_Manager, output){
PHALCON_INIT_NVAR(attributes);
phalcon_call_method(attributes, resource, "getattributes");

/**
* Filtered resources are always local
*/
PHALCON_INIT_NVAR(local);
ZVAL_BOOL(local, 1);

/**
* Prepare the parameters for the callback
*/
Expand Down Expand Up @@ -816,6 +828,97 @@ PHP_METHOD(Phalcon_Assets_Manager, output){
zend_hash_move_forward_ex(ah0, &hp0);
}

/**
* Output the joined resource
*/
if (zend_is_true(join)) {

/**
* We need a valid final target path
*/
if (PHALCON_IS_EMPTY(complete_target_path)) {
PHALCON_INIT_NVAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Resource '", complete_target_path, "' is not a valid target path");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_assets_exception_ce, exception_message);
return;
}

PHALCON_INIT_VAR(is_directory);
phalcon_call_func_p1(is_directory, "is_dir", complete_target_path);

/**
* The targetpath needs to be a valid file
*/
if (PHALCON_IS_TRUE(is_directory)) {
PHALCON_INIT_NVAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Resource '", complete_target_path, "' is not a valid target path");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_assets_exception_ce, exception_message);
return;
}

/**
* Write the file using file_put_contents. This respects the openbase-dir also
* writes to streams
*/
phalcon_call_func_p2_noret("file_put_contents", complete_target_path, filtered_joined_content);

/**
* Generate the HTML using the original path in the resource
*/
PHALCON_INIT_VAR(target_uri);
phalcon_call_method(target_uri, collection, "gettargeturi");
if (Z_TYPE_P(prefix) != IS_NULL) {
PHALCON_INIT_NVAR(prefixed_path);
PHALCON_CONCAT_VV(prefixed_path, prefix, target_uri);
} else {
PHALCON_CPY_WRT(prefixed_path, target_uri);
}

/**
* Gets extra HTML attributes in the resource
*/
PHALCON_INIT_NVAR(attributes);
phalcon_call_method(attributes, collection, "getattributes");

/**
* Joined resources are always local
*/
PHALCON_INIT_NVAR(local);
ZVAL_BOOL(local, 1);

/**
* Prepare the parameters for the callback
*/
if (Z_TYPE_P(attributes) == IS_ARRAY) {
phalcon_array_update_long(&attributes, 0, &prefixed_path, PH_COPY | PH_SEPARATE TSRMLS_CC);

PHALCON_INIT_NVAR(parameters);
array_init_size(parameters, 2);
phalcon_array_append(&parameters, attributes, PH_SEPARATE TSRMLS_CC);
phalcon_array_append(&parameters, local, PH_SEPARATE TSRMLS_CC);
} else {
PHALCON_INIT_NVAR(parameters);
array_init_size(parameters, 2);
phalcon_array_append(&parameters, prefixed_path, PH_SEPARATE TSRMLS_CC);
phalcon_array_append(&parameters, local, PH_SEPARATE TSRMLS_CC);
}

/**
* Call the callback to generate the HTML
*/
PHALCON_INIT_NVAR(html);
PHALCON_CALL_USER_FUNC_ARRAY(html, callback, parameters);

/**
* Implicit output prints the content directly
*/
if (zend_is_true(use_implicit_output)) {
zend_print_zval(html, 0);
} else {
phalcon_concat_self(&output, html TSRMLS_CC);
}
}

RETURN_CCTOR(output);
}

Expand Down
Loading

0 comments on commit ff26d57

Please sign in to comment.