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

PSR-3 Classes #1888

Merged
merged 7 commits into from Jan 23, 2014
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
11 changes: 10 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@
- Phalcon\Logger\Adapter::commit() now clears the queue (#1742)
- Phalcon\Logger\Adapter now implements Phalcon\Logger\AdapterInterface (#1852)
- Phalcon\Logger\Formatter now implements Phalcon\Logger\FormatterInterface (#1852)
- Phalcon\Logger\Adapter is now PSR-3 compliant (#1873)
- Phalcon\Logger\Adapter and Phalcon\Logger\AdapterInterface are now PSR-3 compliant (#1873, #1888)
- Added Phalcon\Logger\Adapter::critical() method (#1888)
- For PHP 5.3.9+ when phalcon.register_psr3_classes php.ini option is set, Phalcon\Logger\Adapter implements Psr\Log\LoggerInterface (#1888)
- When phalcon.register_psr3_classes php.ini option is set, Phalcon\Logger\Adapter and derived classes throw Psr\Log\InvalidArgumentException
instead of Phalcon\Logger\Exception when the arguments are not valid (#1888)
- Phalcon\Mvc:
- Phalcon\Mvc\Application::handle() now checks whether the class exists before include()'ing its file (#812, #818)
- Phalcon\Mvc\Model\Criteria::fromInput() now sets _modelName (#866, #873)
Expand Down Expand Up @@ -271,6 +275,11 @@
- Optimized Phalcon\Validation\Message\Group by using native iterators (#1657)
- Optimized Phalcon\Validation\Message\Group by using object handlers (#1765)
- Phalcon\Validation\Validator now implements Phalcon\Validation\ValidatorInterface (#1852)
- Psr\Log:
- when phalcon.register_psr3_classes php.ini option is set, Psr\Log\LoggerAwareInterface, Psr\Log\LoggerInterface,
Psr\Log\AbstractLogger, Psr\Log\InvalidArgumentException, Psr\Log\LogLevel, Psr\Log\NullLogger,
Psr\Log\LoggerAwareTrait (PHP 5.4+), Psr\Log\LoggerTrait (PHP 5.4+) are registered as internal interfaces/classes/traits
by Phalcon (#1888)
- Unit tests:
- Travis CI imporvements (#819, #846, #949)
- Use markTestSkipped() instead of echo (#862)
Expand Down
10 changes: 9 additions & 1 deletion ext/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,15 @@ image/adapter.c \
image/adapterinterface.c \
image/exception.c \
image/adapter/gd.c \
image/adapter/imagick.c"
image/adapter/imagick.c \
psr/log/abstractlogger.c \
psr/log/invalidargumentexception.c \
psr/log/loggerawareinterface.c \
psr/log/loggerawaretrait.c \
psr/log/loggerinterface.c \
psr/log/loggertrait.c \
psr/log/loglevel.c \
psr/log/nulllogger.c"

PHP_NEW_EXTENSION(phalcon, $phalcon_sources, $ext_shared)
PHP_ADD_EXTENSION_DEP([phalcon], [spl])
Expand Down
1 change: 1 addition & 0 deletions ext/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@ if (PHP_PHALCON != "no") {
ADD_SOURCES("ext/phalcon/validation", "validatorinterface.c exception.c message.c validator.c", "phalcon")
ADD_SOURCES("ext/phalcon/validation/message", "group.c", "phalcon")
ADD_SOURCES("ext/phalcon/validation/validator", "email.c presenceof.c confirmation.c regex.c exclusionin.c identical.c between.c inclusionin.c stringlength.c url.c", "phalcon")
ADD_SOURCES("ext/phalcon/psr/log", "abstractlogger.c invalidargumentexception.c loggerawareinterface.c loggerawaretrait.c loggerinterface.c loggertrait.c loglevel.c nulllogger.c")
}
76 changes: 53 additions & 23 deletions ext/logger/adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "logger/formatterinterface.h"
#include "logger/item.h"
#include "logger.h"
#include "psr/log/loggerinterface.h"
#include "psr/log/invalidargumentexception.h"

#include "kernel/main.h"
#include "kernel/memory.h"
Expand All @@ -45,12 +47,13 @@ PHP_METHOD(Phalcon_Logger_Adapter, begin);
PHP_METHOD(Phalcon_Logger_Adapter, commit);
PHP_METHOD(Phalcon_Logger_Adapter, rollback);
PHP_METHOD(Phalcon_Logger_Adapter, emergency);
PHP_METHOD(Phalcon_Logger_Adapter, debug);
PHP_METHOD(Phalcon_Logger_Adapter, alert);
PHP_METHOD(Phalcon_Logger_Adapter, critical);
PHP_METHOD(Phalcon_Logger_Adapter, error);
PHP_METHOD(Phalcon_Logger_Adapter, info);
PHP_METHOD(Phalcon_Logger_Adapter, notice);
PHP_METHOD(Phalcon_Logger_Adapter, warning);
PHP_METHOD(Phalcon_Logger_Adapter, alert);
PHP_METHOD(Phalcon_Logger_Adapter, notice);
PHP_METHOD(Phalcon_Logger_Adapter, info);
PHP_METHOD(Phalcon_Logger_Adapter, debug);
PHP_METHOD(Phalcon_Logger_Adapter, log);

static const zend_function_entry phalcon_logger_adapter_method_entry[] = {
Expand All @@ -61,15 +64,16 @@ static const zend_function_entry phalcon_logger_adapter_method_entry[] = {
PHP_ME(Phalcon_Logger_Adapter, begin, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, commit, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, rollback, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, emergency, arginfo_phalcon_logger_adapterinterface_emergency, ZEND_ACC_PUBLIC)
PHP_MALIAS(Phalcon_Logger_Adapter, emergence, emergency, arginfo_phalcon_logger_adapterinterface_emergency, ZEND_ACC_PUBLIC | ZEND_ACC_DEPRECATED)
PHP_ME(Phalcon_Logger_Adapter, log, arginfo_phalcon_logger_adapterinterface_log, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, debug, arginfo_phalcon_logger_adapterinterface_debug, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, error, arginfo_phalcon_logger_adapterinterface_error, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, info, arginfo_phalcon_logger_adapterinterface_info, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, notice, arginfo_phalcon_logger_adapterinterface_notice, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, warning, arginfo_phalcon_logger_adapterinterface_warning, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, error, arginfo_phalcon_logger_adapterinterface_error, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, critical, arginfo_phalcon_logger_adapterinterface_critical, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, alert, arginfo_phalcon_logger_adapterinterface_alert, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, log, arginfo_phalcon_logger_adapterinterface_log, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, emergency, arginfo_phalcon_logger_adapterinterface_emergency, ZEND_ACC_PUBLIC)
ZEND_FENTRY(logInternal, NULL, arginfo_phalcon_logger_adapter_loginternal, ZEND_ACC_PROTECTED | ZEND_ACC_ABSTRACT)
PHP_FE_END
};
Expand All @@ -86,8 +90,21 @@ PHALCON_INIT_CLASS(Phalcon_Logger_Adapter){
zend_declare_property_null(phalcon_logger_adapter_ce, SL("_formatter"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_long(phalcon_logger_adapter_ce, SL("_logLevel"), PHALCON_LOGGER_SPECIAL, ZEND_ACC_PROTECTED TSRMLS_CC);

/* Prior to PHP 5.3.9, a class could not implement two interfaces
* that specified a method with the same name, since it would cause
* ambiguity. More recent versions of PHP allow this as long as
* the duplicate methods have the same signature.
*/
#if PHP_VERSION_ID >= 50309
if (PHALCON_GLOBAL(register_psr3_classes)) {
zend_class_implements(phalcon_logger_adapter_ce TSRMLS_CC, 2, phalcon_logger_adapterinterface_ce, psr_log_loggerinterface_ce);
}
else {
zend_class_implements(phalcon_logger_adapter_ce TSRMLS_CC, 1, phalcon_logger_adapterinterface_ce);
}
#else
zend_class_implements(phalcon_logger_adapter_ce TSRMLS_CC, 1, phalcon_logger_adapterinterface_ce);

#endif
return SUCCESS;
}

Expand Down Expand Up @@ -176,9 +193,10 @@ PHP_METHOD(Phalcon_Logger_Adapter, getLogLevel){
PHP_METHOD(Phalcon_Logger_Adapter, setFormatter){

zval *formatter;
zend_class_entry *exception = PHALCON_GLOBAL(register_psr3_classes) ? psr_log_invalidargumentexception_ce : phalcon_logger_exception_ce;

phalcon_fetch_params(0, 1, 0, &formatter);
PHALCON_VERIFY_INTERFACE_EX(formatter, phalcon_logger_formatterinterface_ce, phalcon_logger_exception_ce, 0);
PHALCON_VERIFY_INTERFACE_EX(formatter, phalcon_logger_formatterinterface_ce, exception, 0);

phalcon_update_property_this(this_ptr, SL("_formatter"), formatter TSRMLS_CC);
RETURN_THISW();
Expand Down Expand Up @@ -216,24 +234,22 @@ PHP_METHOD(Phalcon_Logger_Adapter, commit){
zval *transaction, *queue, *message_str = NULL;
zval *type = NULL, *time = NULL, *context = NULL;

PHALCON_MM_GROW();

transaction = phalcon_fetch_nproperty_this(this_ptr, SL("_transaction"), PH_NOISY_CC);
if (!zend_is_true(transaction)) {
PHALCON_THROW_EXCEPTION_STR(phalcon_logger_exception_ce, "There is no active transaction");
PHALCON_THROW_EXCEPTION_STRW(phalcon_logger_exception_ce, "There is no active transaction");
return;
}

phalcon_update_property_bool(this_ptr, SL("_transaction"), 0 TSRMLS_CC);

/**
* Check if the queue has something to log
*/
/* Check if the queue has something to log */
queue = phalcon_fetch_nproperty_this(this_ptr, SL("_queue"), PH_NOISY_CC);
if (Z_TYPE_P(queue) == IS_ARRAY) {
HashPosition hp;
zval **message;

PHALCON_MM_GROW();

for (
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(queue), &hp);
zend_hash_get_current_data_ex(Z_ARRVAL_P(queue), (void**)&message, &hp) == SUCCESS;
Expand All @@ -259,9 +275,11 @@ PHP_METHOD(Phalcon_Logger_Adapter, commit){
array_init(queue);
phalcon_update_property_this(getThis(), SL("_queue"), queue TSRMLS_CC);
}

PHALCON_MM_RESTORE();
}

RETURN_THIS();
RETURN_THISW();
}

/**
Expand Down Expand Up @@ -317,7 +335,7 @@ static void phalcon_logger_adapter_log_helper(INTERNAL_FUNCTION_PARAMETERS, int
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\Adapter
* @return Phalcon\Logger\AdapterInterface
*/
PHP_METHOD(Phalcon_Logger_Adapter, emergency){

Expand All @@ -329,7 +347,7 @@ PHP_METHOD(Phalcon_Logger_Adapter, emergency){
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\Adapter
* @return Phalcon\Logger\AdapterInterface
*/
PHP_METHOD(Phalcon_Logger_Adapter, debug){

Expand All @@ -341,7 +359,7 @@ PHP_METHOD(Phalcon_Logger_Adapter, debug){
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\Adapter
* @return Phalcon\Logger\AdapterInterface
*/
PHP_METHOD(Phalcon_Logger_Adapter, error){

Expand All @@ -353,7 +371,7 @@ PHP_METHOD(Phalcon_Logger_Adapter, error){
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\Adapter
* @return Phalcon\Logger\AdapterInterface
*/
PHP_METHOD(Phalcon_Logger_Adapter, info){

Expand All @@ -365,7 +383,7 @@ PHP_METHOD(Phalcon_Logger_Adapter, info){
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\Adapter
* @return Phalcon\Logger\AdapterInterface
*/
PHP_METHOD(Phalcon_Logger_Adapter, notice){

Expand All @@ -377,7 +395,7 @@ PHP_METHOD(Phalcon_Logger_Adapter, notice){
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\Adapter
* @return Phalcon\Logger\AdapterInterface
*/
PHP_METHOD(Phalcon_Logger_Adapter, warning){

Expand All @@ -389,13 +407,25 @@ PHP_METHOD(Phalcon_Logger_Adapter, warning){
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\Adapter
* @return Phalcon\Logger\AdapterInterface
*/
PHP_METHOD(Phalcon_Logger_Adapter, alert){

phalcon_logger_adapter_log_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHALCON_LOGGER_ALERT);
}

/**
* Sends/Writes a critical message to the log
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\AdapterInterface
*/
PHP_METHOD(Phalcon_Logger_Adapter, critical){

phalcon_logger_adapter_log_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHALCON_LOGGER_CRITICAL);
}

/**
* Logs messages to the internal logger. Appends messages to the log
*
Expand Down
48 changes: 23 additions & 25 deletions ext/logger/adapter/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "logger/adapterinterface.h"
#include "logger/exception.h"
#include "logger/formatter/line.h"
#include "psr/log/invalidargumentexception.h"

#include "kernel/main.h"
#include "kernel/memory.h"
Expand Down Expand Up @@ -93,44 +94,41 @@ PHALCON_INIT_CLASS(Phalcon_Logger_Adapter_File){
*/
PHP_METHOD(Phalcon_Logger_Adapter_File, __construct){

zval *name, *options = NULL, *mode = NULL, *handler, *exception_message;
zval **name, **options = NULL, *mode = NULL, *handler;
zend_class_entry *exception = PHALCON_GLOBAL(register_psr3_classes) ? psr_log_invalidargumentexception_ce : phalcon_logger_exception_ce;

phalcon_fetch_params_ex(1, 1, &name, &options);
PHALCON_ENSURE_IS_STRING(name);

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 1, &name, &options);

if (!options) {
options = PHALCON_GLOBAL(z_null);
options = &PHALCON_GLOBAL(z_null);
}

if (phalcon_array_isset_string(options, SS("mode"))) {

PHALCON_OBS_VAR(mode);
phalcon_array_fetch_string(&mode, options, SL("mode"), PH_NOISY);
if (phalcon_array_isset_string_fetch(&mode, *options, SS("mode"))) {
if (phalcon_memnstr_str(mode, SL("r"))) {
PHALCON_THROW_EXCEPTION_STR(phalcon_logger_exception_ce, "Logger must be opened in append or write mode");
PHALCON_THROW_EXCEPTION_STR(exception, "Logger must be opened in append or write mode");
return;
}
} else {
PHALCON_INIT_NVAR(mode);
PHALCON_INIT_VAR(mode);
ZVAL_STRING(mode, "ab", 1);
}

/**
* We use 'fopen' to respect to open-basedir directive
*/
PHALCON_OBS_VAR(handler);
PHALCON_CALL_FUNCTION(&handler, "fopen", name, mode);
if (!zend_is_true(handler)) {
PHALCON_INIT_VAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Can't open log file at '", name, "'");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_logger_exception_ce, exception_message);
return;
PHALCON_CALL_FUNCTION(&handler, "fopen", *name, mode);
if (Z_TYPE_P(handler) != IS_RESOURCE) {
zend_throw_exception_ex(exception, 0 TSRMLS_CC, "Cannot open log file '%s'", Z_STRVAL_PP(name));
}
else {
phalcon_update_property_this(this_ptr, SL("_path"), *name TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_options"), *options TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_fileHandler"), handler TSRMLS_CC);
}

phalcon_update_property_this(this_ptr, SL("_path"), name TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_options"), options TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_fileHandler"), handler TSRMLS_CC);

PHALCON_MM_RESTORE();
}
Expand Down Expand Up @@ -176,9 +174,8 @@ PHP_METHOD(Phalcon_Logger_Adapter_File, logInternal){

phalcon_fetch_params(1, 4, 0, &message, &type, &time, &context);

PHALCON_OBS_VAR(file_handler);
phalcon_read_property_this(&file_handler, this_ptr, SL("_fileHandler"), PH_NOISY_CC);
if (!zend_is_true(file_handler)) {
file_handler = phalcon_fetch_nproperty_this(this_ptr, SL("_fileHandler"), PH_NOISY_CC);
if (Z_TYPE_P(file_handler) != IS_RESOURCE) {
PHALCON_THROW_EXCEPTION_STR(phalcon_logger_exception_ce, "Cannot send message to the log because it is invalid");
return;
}
Expand Down Expand Up @@ -225,13 +222,14 @@ PHP_METHOD(Phalcon_Logger_Adapter_File, getPath) {
PHP_METHOD(Phalcon_Logger_Adapter_File, __wakeup){

zval *path, *options, *mode = NULL, *file_handler;
zend_class_entry *exception = PHALCON_GLOBAL(register_psr3_classes) ? psr_log_invalidargumentexception_ce : phalcon_logger_exception_ce;

PHALCON_MM_GROW();

PHALCON_OBS_VAR(path);
phalcon_read_property_this(&path, this_ptr, SL("_path"), PH_NOISY_CC);
if (Z_TYPE_P(path) != IS_STRING) {
PHALCON_THROW_EXCEPTION_STR(phalcon_logger_exception_ce, "Invalid data passed to Phalcon\\Logger\\Adapter\\File::__wakeup()");
PHALCON_THROW_EXCEPTION_STR(exception, "Invalid data passed to Phalcon\\Logger\\Adapter\\File::__wakeup()");
return;
}

Expand All @@ -241,7 +239,7 @@ PHP_METHOD(Phalcon_Logger_Adapter_File, __wakeup){
PHALCON_OBS_VAR(mode);
phalcon_array_fetch_string(&mode, options, SL("mode"), PH_NOISY);
if (Z_TYPE_P(mode) != IS_STRING) {
PHALCON_THROW_EXCEPTION_STR(phalcon_logger_exception_ce, "Invalid data passed to Phalcon\\Logger\\Adapter\\File::__wakeup()");
PHALCON_THROW_EXCEPTION_STR(exception, "Invalid data passed to Phalcon\\Logger\\Adapter\\File::__wakeup()");
return;
}
} else {
Expand Down
Loading