Issue #2787567 by alexpott: Refactor \Drupal\Component\Assertion\Handle to not break PSR-0/4 one class per file
parent
d798c289d6
commit
850b9eebef
|
@ -1,41 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @file
|
|
||||||
* Contains \Drupal\Component\Assertion\Handle.
|
|
||||||
*
|
|
||||||
* For PHP 5 this contains \AssertionError as well.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace {
|
namespace Drupal\Component\Assertion;
|
||||||
|
|
||||||
if (!class_exists('AssertionError', FALSE)) {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Emulates PHP 7 AssertionError as closely as possible.
|
|
||||||
*
|
|
||||||
* We force this class to exist at the root namespace for PHP 5.
|
|
||||||
* This class exists natively in PHP 7. Note that in PHP 7 it extends from
|
|
||||||
* Error, not Exception, but that isn't possible for PHP 5 - all exceptions
|
|
||||||
* must extend from exception.
|
|
||||||
*/
|
|
||||||
class AssertionError extends Exception {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function __construct($message = '', $code = 0, Exception $previous = NULL, $file = '', $line = 0) {
|
|
||||||
parent::__construct($message, $code, $previous);
|
|
||||||
// Preserve the filename and line number of the assertion failure.
|
|
||||||
$this->file = $file;
|
|
||||||
$this->line = $line;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace Drupal\Component\Assertion {
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for runtime assertion failures.
|
* Handler for runtime assertion failures.
|
||||||
|
@ -56,6 +21,9 @@ class Handle {
|
||||||
assert_options(ASSERT_WARNING, FALSE);
|
assert_options(ASSERT_WARNING, FALSE);
|
||||||
|
|
||||||
if (version_compare(PHP_VERSION, '7.0.0-dev') < 0) {
|
if (version_compare(PHP_VERSION, '7.0.0-dev') < 0) {
|
||||||
|
if (!class_exists('AssertionError', FALSE)) {
|
||||||
|
require __DIR__ . '/global_namespace_php5.php';
|
||||||
|
}
|
||||||
// PHP 5 - create a handler to throw the exception directly.
|
// PHP 5 - create a handler to throw the exception directly.
|
||||||
assert_options(ASSERT_CALLBACK, function($file = '', $line = 0, $code = '', $message = '') {
|
assert_options(ASSERT_CALLBACK, function($file = '', $line = 0, $code = '', $message = '') {
|
||||||
if (empty($message)) {
|
if (empty($message)) {
|
||||||
|
@ -71,5 +39,3 @@ class Handle {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Contains PHP5 version of the \AssertionError class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emulates PHP 7 AssertionError as closely as possible.
|
||||||
|
*
|
||||||
|
* This class is declared in the global namespace. It will only be included by
|
||||||
|
* \Drupal\Component\Assertion\Handle for PHP5 since this class exists natively
|
||||||
|
* in PHP 7. Note that in PHP 7 it extends from Error, not Exception, but that
|
||||||
|
* isn't possible for PHP 5 - all exceptions must extend from exception.
|
||||||
|
*/
|
||||||
|
class AssertionError extends Exception {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function __construct($message = '', $code = 0, Exception $previous = NULL, $file = '', $line = 0) {
|
||||||
|
parent::__construct($message, $code, $previous);
|
||||||
|
// Preserve the filename and line number of the assertion failure.
|
||||||
|
$this->file = $file;
|
||||||
|
$this->line = $line;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue