2011-10-27 23:45:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles raising an notice when accessing a deprecated method
|
|
|
|
*
|
|
|
|
* A pattern used in SilverStripe when deprecating a method is to add something like
|
|
|
|
* user_error('This method is deprecated', E_USER_NOTICE);
|
|
|
|
* to the method
|
|
|
|
*
|
|
|
|
* However sometimes we want to mark that a method will be deprecated in some future version and shouldn't be used in
|
|
|
|
* new code, but not forbid in the current version - for instance when that method is still heavily used in framework
|
|
|
|
* or cms.
|
|
|
|
*
|
|
|
|
* This class abstracts the above pattern and adds a way to do that.
|
|
|
|
*
|
|
|
|
* Each call to notice passes a version that the notice will be valid from. Additionally this class has a notion of the
|
|
|
|
* version it should use when deciding whether to raise the notice. If that version is equal to or greater than the
|
|
|
|
* notices version (and SilverStripe is in dev mode) a deprecation message will be raised.
|
|
|
|
*
|
|
|
|
* Normally the checking version will be the release version of SilverStripe, but a developer can choose to set it to a
|
|
|
|
* future version, to see how their code will behave in future versions.
|
|
|
|
*
|
|
|
|
* Modules can also set the version for calls they make - either setting it to a future version in order to ensure
|
|
|
|
* forwards compatibility or setting it backwards if a module has not yet removed references to deprecated methods.
|
|
|
|
*
|
|
|
|
* When set per-module, only direct calls to deprecated methods from those modules are considered - if the module
|
|
|
|
* calls a non-module method which then calls a deprecated method, that call will use the global check version, not
|
|
|
|
* the module specific check version.
|
2011-10-28 02:11:55 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-10-28 02:11:55 +02:00
|
|
|
* @subpackage dev
|
2011-10-27 23:45:12 +02:00
|
|
|
*/
|
|
|
|
class Deprecation {
|
|
|
|
|
2012-07-13 11:37:35 +02:00
|
|
|
const SCOPE_METHOD = 1;
|
|
|
|
const SCOPE_CLASS = 2;
|
|
|
|
const SCOPE_GLOBAL = 4;
|
|
|
|
|
2011-10-28 02:11:55 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-10-27 23:45:12 +02:00
|
|
|
protected static $version;
|
|
|
|
|
2011-10-28 02:11:55 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-10-27 23:45:12 +02:00
|
|
|
protected static $module_version_overrides = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int - the notice level to raise on a deprecation notice. Defaults to E_USER_DEPRECATED if that exists,
|
|
|
|
* E_USER_NOTICE if not
|
|
|
|
*/
|
|
|
|
public static $notice_level = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the version that is used to check against the version passed to notice. If the ::notice version is
|
|
|
|
* greater than or equal to this version, a message will be raised
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param $ver string -
|
|
|
|
* A php standard version string, see http://php.net/manual/en/function.version-compare.php for details.
|
|
|
|
* @param null $forModule string -
|
|
|
|
* The name of a module. The passed version will be used as the check value for
|
|
|
|
* calls directly from this module rather than the global value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function notification_version($ver, $forModule = null) {
|
|
|
|
if ($forModule) {
|
|
|
|
self::$module_version_overrides[$forModule] = $ver;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self::$version = $ver;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* Given a backtrace, get the module name from the caller two removed (the caller of the method that called
|
|
|
|
* #notice)
|
2011-10-27 23:45:12 +02:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param $backtrace array - a backtrace as returned from debug_backtrace
|
|
|
|
* @return string - the name of the module the call came from, or null if we can't determine
|
|
|
|
*/
|
|
|
|
protected static function get_calling_module_from_trace($backtrace) {
|
|
|
|
if (!isset($backtrace[1]['file'])) return;
|
|
|
|
|
2012-10-16 06:10:54 +02:00
|
|
|
$callingfile = realpath($backtrace[1]['file']);
|
2011-10-27 23:45:12 +02:00
|
|
|
|
2013-05-31 15:32:10 +02:00
|
|
|
$manifest = SS_ClassLoader::instance()->getManifest();
|
2011-10-27 23:45:12 +02:00
|
|
|
foreach ($manifest->getModules() as $name => $path) {
|
2012-10-16 06:10:54 +02:00
|
|
|
if (strpos($callingfile, realpath($path)) === 0) {
|
2011-10-27 23:45:12 +02:00
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a backtrace, get the method name from the immediate parent caller (the caller of #notice)
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param $backtrace array - a backtrace as returned from debug_backtrace
|
2011-10-29 01:00:45 +02:00
|
|
|
* @param $level - 1 (default) will return immediate caller, 2 will return caller's caller, etc.
|
2011-10-27 23:45:12 +02:00
|
|
|
* @return string - the name of the method
|
|
|
|
*/
|
2011-10-29 01:00:45 +02:00
|
|
|
protected static function get_called_method_from_trace($backtrace, $level = 1) {
|
|
|
|
$level = (int)$level;
|
|
|
|
if(!$level) $level = 1;
|
|
|
|
$called = $backtrace[$level];
|
2011-10-28 02:11:55 +02:00
|
|
|
|
2011-10-27 23:45:12 +02:00
|
|
|
if (isset($called['class'])) {
|
|
|
|
return $called['class'] . $called['type'] . $called['function'];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $called['function'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Raise a notice indicating the method is deprecated if the version passed as the second argument is greater
|
|
|
|
* than or equal to the check version set via ::notification_version
|
2011-10-28 02:11:55 +02:00
|
|
|
*
|
2011-10-27 23:45:12 +02:00
|
|
|
* @static
|
|
|
|
* @param $string - The notice to raise
|
|
|
|
* @param $atVersion - The version at which this notice should start being raised
|
2012-07-13 11:37:35 +02:00
|
|
|
* @param Boolean $scope - Notice relates to the method or class context its called in.
|
2011-10-27 23:45:12 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-07-13 11:37:35 +02:00
|
|
|
public static function notice($atVersion, $string = '', $scope = Deprecation::SCOPE_METHOD) {
|
2011-10-31 01:51:31 +01:00
|
|
|
// Never raise deprecation notices in a live environment
|
2013-07-17 11:55:19 +02:00
|
|
|
if(Director::isLive(true)) return;
|
2011-10-27 23:45:12 +02:00
|
|
|
|
|
|
|
$checkVersion = self::$version;
|
|
|
|
// Getting a backtrace is slow, so we only do it if we need it
|
|
|
|
$backtrace = null;
|
|
|
|
|
2013-03-22 13:30:54 +01:00
|
|
|
// If you pass #.#, assume #.#.0
|
|
|
|
if(preg_match('/^[0-9]+\.[0-9]+$/', $atVersion)) $atVersion .= '.0';
|
|
|
|
if(preg_match('/^[0-9]+\.[0-9]+$/', $checkVersion)) $checkVersion .= '.0';
|
|
|
|
|
2011-10-27 23:45:12 +02:00
|
|
|
if(self::$module_version_overrides) {
|
|
|
|
$module = self::get_calling_module_from_trace($backtrace = debug_backtrace(0));
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset(self::$module_version_overrides[$module])) {
|
|
|
|
$checkVersion = self::$module_version_overrides[$module];
|
|
|
|
}
|
2011-10-27 23:45:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the version against the notice version
|
|
|
|
if ($checkVersion && version_compare($checkVersion, $atVersion, '>=')) {
|
2012-07-13 11:37:35 +02:00
|
|
|
// Get the calling scope
|
|
|
|
if($scope == Deprecation::SCOPE_METHOD) {
|
|
|
|
if (!$backtrace) $backtrace = debug_backtrace(0);
|
|
|
|
$caller = self::get_called_method_from_trace($backtrace);
|
|
|
|
} elseif($scope == Deprecation::SCOPE_CLASS) {
|
|
|
|
if (!$backtrace) $backtrace = debug_backtrace(0);
|
|
|
|
$caller = isset($backtrace[1]['class']) ? $backtrace[1]['class'] : '(unknown)';
|
|
|
|
} else {
|
|
|
|
$caller = false;
|
|
|
|
}
|
2011-10-27 23:45:12 +02:00
|
|
|
|
|
|
|
// Get the level to raise the notice as
|
|
|
|
$level = self::$notice_level;
|
2012-04-18 13:10:57 +02:00
|
|
|
if (!$level) $level = E_USER_DEPRECATED;
|
2011-10-27 23:45:12 +02:00
|
|
|
|
|
|
|
// Then raise the notice
|
2011-10-29 01:00:45 +02:00
|
|
|
if(substr($string,-1) != '.') $string .= ".";
|
|
|
|
|
|
|
|
$string .= " Called from " . self::get_called_method_from_trace($backtrace, 2) . '.';
|
|
|
|
|
2012-07-13 11:37:35 +02:00
|
|
|
if($caller) {
|
2014-08-15 08:53:05 +02:00
|
|
|
user_error($caller.' is deprecated.'.($string ? ' '.$string : ''), $level);
|
2012-07-13 11:37:35 +02:00
|
|
|
} else {
|
2014-08-15 08:53:05 +02:00
|
|
|
user_error($string, $level);
|
2012-07-13 11:37:35 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-10-27 23:45:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method for when testing. Dump all the current version settings to a variable for later passing to restore
|
|
|
|
* @return array - opaque array that should only be used to pass to ::restore_version_settings
|
|
|
|
*/
|
|
|
|
public static function dump_settings() {
|
|
|
|
return array(
|
|
|
|
'level' => self::$notice_level,
|
|
|
|
'version' => self::$version,
|
|
|
|
'moduleVersions' => self::$module_version_overrides
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method for when testing. Restore all the current version settings from a variable
|
|
|
|
* @static
|
|
|
|
* @param $settings array - An array as returned by ::dump_version_settings
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function restore_settings($settings) {
|
|
|
|
self::$notice_level = $settings['level'];
|
|
|
|
self::$version = $settings['version'];
|
|
|
|
self::$module_version_overrides = $settings['moduleVersions'];
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|