Provide an ability to use static methods as updates

This commit is contained in:
Dmitriy Simushev 2014-11-28 13:34:37 +00:00
parent e2d4ab3715
commit 459f75727a
2 changed files with 24 additions and 3 deletions

View File

@ -142,7 +142,7 @@ class Updater
} }
// Run the update // Run the update
if (!$this->{$method}()) { if (!$method()) {
$this->errors[] = getlocal('Cannot update to {0}', array($version)); $this->errors[] = getlocal('Cannot update to {0}', array($version));
return false; return false;

View File

@ -52,7 +52,8 @@ class Utils
/** /**
* Gets list of all available updates. * Gets list of all available updates.
* *
* @param object $container Instance of the class that keeps update methods. * @param object|string $container Either an instance of the class that
* keeps update methods or fully classified name of such class.
* @return array The keys of this array are version numbers and values are * @return array The keys of this array are version numbers and values are
* methods of the $container class that should be performed. * methods of the $container class that should be performed.
*/ */
@ -60,6 +61,23 @@ class Utils
{ {
$updates = array(); $updates = array();
if (is_object($container)) {
// If an objects is passed to the method we can use its public
// static and non-static methods as updates.
$methods_filter = \ReflectionMethod::IS_PUBLIC;
} else {
// If a class name is passed to the method we can use only its
// public static methods as updates. Also we need to make sure the
// class exists.
if (!class_exists($container)) {
throw new \InvalidArgumentException(sprintf(
'Class "%s" does not exist',
$container
));
}
$methods_filter = \ReflectionMethod::IS_PUBLIC & \ReflectionMethod::IS_STATIC;
}
$container_reflection = new \ReflectionClass($container); $container_reflection = new \ReflectionClass($container);
foreach ($container_reflection->getMethods() as $method_reflection) { foreach ($container_reflection->getMethods() as $method_reflection) {
// Filter update methods // Filter update methods
@ -71,7 +89,10 @@ class Utils
$version .= sprintf('-%s.%u', strtolower($matches[2]), $matches[3]); $version .= sprintf('-%s.%u', strtolower($matches[2]), $matches[3]);
} }
$updates[$version] = $name; $updates[$version] = array(
$method_reflection->isStatic() ? $container_reflection->getName() : $container,
$name
);
} }
} }