Merge pull request #23 from radiorabe/feature/stop-using-reflection-inproduction-code

stop using reflection in production code
This commit is contained in:
Robb 2017-03-04 22:25:34 -05:00 committed by GitHub
commit 0abfa7f018
4 changed files with 7 additions and 52 deletions

View file

@ -350,8 +350,7 @@ class TaskFactory {
* @return bool true if the class $c implements AirtimeTask * @return bool true if the class $c implements AirtimeTask
*/ */
private static function _isTask($c) { private static function _isTask($c) {
$reflect = new ReflectionClass($c); return is_a('AirtimeTask', $c, true);
return $reflect->implementsInterface('AirtimeTask');
} }
/** /**

View file

@ -1,47 +0,0 @@
<?php
/**
* Basic enumeration implementation; from http://stackoverflow.com/questions/254514/php-and-enumerations
*
* Class Enum
*/
abstract class Enum {
const __default = NULL;
private static $constCacheArray = NULL;
private function __construct() {}
private static function getConstants() {
if (self::$constCacheArray == NULL) {
self::$constCacheArray = [];
}
$calledClass = get_called_class();
if (!array_key_exists($calledClass, self::$constCacheArray)) {
$reflect = new ReflectionClass($calledClass);
self::$constCacheArray[$calledClass] = $reflect->getConstants();
}
return self::$constCacheArray[$calledClass];
}
public static function isValidName($name, $strict = false) {
$constants = self::getConstants();
if ($strict) {
return array_key_exists($name, $constants);
}
$keys = array_map('strtolower', array_keys($constants));
return in_array(strtolower($name), $keys);
}
public static function isValidValue($value) {
$values = array_values(self::getConstants());
return in_array($value, $values, $strict = true);
}
public static function getDefault() {
return static::__default;
}
}

View file

@ -1,6 +1,6 @@
<?php <?php
final class HttpRequestType extends Enum { final class HttpRequestType {
const GET = "GET"; const GET = "GET";
const POST = "POST"; const POST = "POST";

View file

@ -1,7 +1,6 @@
<?php <?php
require_once "Enum.php"; final class MediaType {
final class MediaType extends Enum {
const __default = self::FILE; const __default = self::FILE;
@ -11,4 +10,8 @@ final class MediaType extends Enum {
const WEBSTREAM = 4; const WEBSTREAM = 4;
const PODCAST = 5; const PODCAST = 5;
public static function getDefault() {
return static::__default;
}
} }