Skip to content

Commit

Permalink
Warning BC break, remove the automatic __init() calling from `lithi…
Browse files Browse the repository at this point in the history
…um\core\Libraries`.
  • Loading branch information
jails committed Jul 14, 2013
1 parent 81c873a commit fa4ef11
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 76 deletions.
89 changes: 36 additions & 53 deletions core/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,49 +56,6 @@ class ErrorHandler extends \lithium\core\StaticObject {

protected static $_runOptions = array();

/**
* Setup basic error handling checks/types, as well as register the error and exception
* handlers.
*
* Called on static class initialization (i.e. when first loaded).
*
* @return void
*/
public static function __init() {
static::$_checks = array(
'type' => function($config, $info) {
return (boolean) array_filter((array) $config['type'], function($type) use ($info) {
return $type === $info['type'] || is_subclass_of($info['type'], $type);
});
},
'code' => function($config, $info) {
return ($config['code'] & $info['code']);
},
'stack' => function($config, $info) {
return (boolean) array_intersect((array) $config['stack'], $info['stack']);
},
'message' => function($config, $info) {
return preg_match($config['message'], $info['message']);
}
);
$self = get_called_class();

static::$_exceptionHandler = function($exception, $return = false) use ($self) {
if (ob_get_length()) {
ob_end_clean();
}
$info = compact('exception') + array(
'type' => get_class($exception),
'stack' => $self::trace($exception->getTrace())
);
foreach (array('message', 'file', 'line', 'trace') as $key) {
$method = 'get' . ucfirst($key);
$info[$key] = $exception->{$method}();
}
return $return ? $info : $self::handle($info);
};
}

/**
* Configure the `ErrorHandler`.
*
Expand All @@ -125,7 +82,6 @@ public static function config($config = array()) {
* where the error occurred. The exception will be caught at the first point in
* the stack trace inside a matching `try`/`catch` block, or that has a matching
* error handler applied using the `apply()` method.
* @return void
*/
public static function run(array $config = array()) {
$defaults = array('trapErrors' => false, 'convertErrors' => true);
Expand Down Expand Up @@ -158,8 +114,6 @@ public static function run(array $config = array()) {
/**
* Returns the state of the `ErrorHandler`, indicating whether or not custom error/exception
* handers have been regsitered.
*
* @return void
*/
public static function isRunning() {
return static::$_isRunning;
Expand All @@ -169,8 +123,6 @@ public static function isRunning() {
* Unooks `ErrorHandler`'s exception and error handlers, and restores PHP's defaults. May have
* unexpected results if it is not matched with a prior call to `run()`, or if other error
* handlers are set after a call to `run()`.
*
* @return void
*/
public static function stop() {
restore_error_handler();
Expand All @@ -179,16 +131,46 @@ public static function stop() {
}

/**
* Wipes out all configuration and resets the error handler to its initial state when loaded.
* Mainly used for testing.
*
* @return void
* Setup basic error handling checks/types, as well as register the error and exception
* handlers and wipes out all configuration and resets the error handler to its initial state
* when loaded. Mainly used for testing.
*/
public static function reset() {
static::$_config = array();
static::$_checks = array();
static::$_exceptionHandler = null;
static::__init();
static::$_checks = array(
'type' => function($config, $info) {
return (boolean) array_filter((array) $config['type'], function($type) use ($info) {
return $type === $info['type'] || is_subclass_of($info['type'], $type);
});
},
'code' => function($config, $info) {
return ($config['code'] & $info['code']);
},
'stack' => function($config, $info) {
return (boolean) array_intersect((array) $config['stack'], $info['stack']);
},
'message' => function($config, $info) {
return preg_match($config['message'], $info['message']);
}
);
$self = get_called_class();

static::$_exceptionHandler = function($exception, $return = false) use ($self) {
if (ob_get_length()) {
ob_end_clean();
}
$info = compact('exception') + array(
'type' => get_class($exception),
'stack' => $self::trace($exception->getTrace())
);
foreach (array('message', 'file', 'line', 'trace') as $key) {
$method = 'get' . ucfirst($key);
$info[$key] = $exception->{$method}();
}
return $return ? $info : $self::handle($info);
};
}

/**
Expand Down Expand Up @@ -323,4 +305,5 @@ public static function trace(array $stack) {
}
}

ErrorHandler::reset();
?>
10 changes: 6 additions & 4 deletions core/Libraries.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,8 @@ public static function find($library, array $options = array()) {
}

/**
* Loads the class definition specified by `$class`. Also calls the `__init()` method on the
* class, if defined. Looks through the list of libraries defined in `$_configurations`, which
* are added through `lithium\core\Libraries::add()`.
* Loads the class definition specified by `$class`. Looks through the list of libraries
* defined in `$_configurations`, which are added through `lithium\core\Libraries::add()`.
*
* @see lithium\core\Libraries::add()
* @see lithium\core\Libraries::path()
Expand All @@ -501,7 +500,10 @@ public static function load($class, $require = false) {

if ($path && include $path) {
static::$_cachedPaths[$class] = $path;
method_exists($class, '__init') ? $class::__init() : null;
if (method_exists($class, '__init')) {
$msg = "Deprecated `{$class}::__init()` method, needs to be called it manually.";
throw new RuntimeException($msg);
}
} elseif ($require) {
throw new RuntimeException("Failed to load class `{$class}` from path `{$path}`.");
}
Expand Down
5 changes: 2 additions & 3 deletions test/filter/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ class Profiler extends \lithium\test\Filter {
/**
* Verifies that the corresponding function exists for each built-in profiler check.
* Initializes display formatters.
*
* @return void
*/
public static function __init() {
public static function reset() {
foreach (static::$_metrics as $name => $check) {
$function = current((array) $check['function']);

Expand Down Expand Up @@ -243,4 +241,5 @@ public static function collect($filterResults) {
}
}

Profiler::reset();
?>
6 changes: 6 additions & 0 deletions tests/cases/core/LibrariesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use SplFileInfo;
use lithium\util\Inflector;
use lithium\core\Libraries;
use lithium\tests\mocks\core\MockInitMethod;

class LibrariesTest extends \lithium\test\Unit {

Expand Down Expand Up @@ -773,6 +774,11 @@ public function testMe() {
$result = $object->testMe();
$this->assertEqual('patched class', $result);
}

public function testDeprectatedInit() {
$this->expectException("/Deprecated/");
MockInitMethod::li3();
}
}

?>
9 changes: 4 additions & 5 deletions tests/cases/util/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@

class ValidatorTest extends \lithium\test\Unit {

public function setUp() {
Validator::__init();
}
public function setUp() {}

public function tearDown() {
Validator::reset();
Mocker::overwriteFunction(false);
}

Expand Down Expand Up @@ -85,15 +84,15 @@ public function testAddCustomRegexMethods() {
}

/**
* Tests that the rules state is reset when calling `Validator::__init()`.
* Tests that the rules state is reset when calling `Validator::reset()`.
*/
public function testStateReset() {
$this->assertNull(Validator::rules('foo'));

Validator::add('foo', '/foo/');
$this->assertEqual('/foo/', Validator::rules('foo'));

Validator::__init();
Validator::reset();
$this->assertNull(Validator::rules('foo'));
}

Expand Down
1 change: 0 additions & 1 deletion tests/integration/g11n/CatalogInflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class CatalogInflectorTest extends \lithium\test\Integration {

public function setUp() {
$this->_backup['catalogConfig'] = Catalog::config();
Catalog::reset();
Catalog::config(array(
'runtime' => array('adapter' => new Memory())
));
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/g11n/CatalogValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ class CatalogValidatorTest extends \lithium\test\Integration {

public function setUp() {
$this->_backup['catalogConfig'] = Catalog::config();
Catalog::reset();
Catalog::config(array(
'runtime' => array('adapter' => new Memory())
));
Validator::__init();
}

public function tearDown() {
Catalog::reset();
Validator::reset();
Catalog::config($this->_backup['catalogConfig']);
}

Expand Down
1 change: 0 additions & 1 deletion tests/integration/g11n/ResourcesMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class ResourcesMessageTest extends \lithium\test\Integration {

public function setUp() {
$this->_backup['catalogConfig'] = Catalog::config();
Catalog::reset();
Catalog::config(array(
'lithium' => array(
'adapter' => 'Php',
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/g11n/ResourcesValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ class ResourcesValidatorTest extends \lithium\test\Integration {

public function setUp() {
$this->_backup['catalogConfig'] = Catalog::config();
Catalog::reset();
Catalog::config(array(
'lithium' => array(
'adapter' => 'Php',
'path' => Libraries::get('lithium', 'path') . '/g11n/resources/php'
)
));
Validator::__init();
}

public function tearDown() {
Catalog::reset();
Validator::reset();
Catalog::config($this->_backup['catalogConfig']);
}

Expand Down
18 changes: 18 additions & 0 deletions tests/mocks/core/MockInitMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\tests\mocks\core;

class MockInitMethod extends \lithium\core\StaticObject {

public static function __init() {}

public static function li3() {}
}

?>
4 changes: 3 additions & 1 deletion tests/mocks/data/MockPostForValidates.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MockPostForValidates extends \lithium\data\Model {
)
);

public static function __init() {
public static function init() {
$class = __CLASS__;
Validator::add('modelIsSet', function($value, $format, $options) use ($class) {
if (isset($options['model']) && $options['model'] = $class) {
Expand All @@ -46,4 +46,6 @@ public static function __init() {
}
}

MockPostForValidates::init();

?>
7 changes: 3 additions & 4 deletions util/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,8 @@ class Validator extends \lithium\core\StaticObject {

/**
* Initializes the list of default validation rules.
*
* @return void
*/
public static function __init() {
public static function reset() {
$alnum = '[A-Fa-f0-9]';
$class = get_called_class();
static::$_methodFilters[$class] = array();
Expand Down Expand Up @@ -444,7 +442,7 @@ public static function respondsTo($method, $internal = false) {
* will cause the validation rule to be skipped if the corresponding value
* is empty (an empty string or `null`). Defaults to `false`.
* - `'format'` _string_: If the validation rule has multiple format definitions
* (see the `add()` or `__init()` methods), the name of the format to be used
* (see the `add()` or `init()` methods), the name of the format to be used
* can be specified here. Additionally, two special values can be used:
* either `'any'`, which means that all formats will be checked and the rule
* will pass if any format passes, or `'all'`, which requires all formats to
Expand Down Expand Up @@ -672,4 +670,5 @@ protected static function _checkFormats($rules) {
}
}

Validator::reset();
?>

0 comments on commit fa4ef11

Please sign in to comment.