[Bridge] Refactor Bridge::create to improve readability
This commit is contained in:
parent
e3849f45ab
commit
0a92b5d29b
1 changed files with 10 additions and 10 deletions
|
@ -74,25 +74,25 @@ class Bridge {
|
||||||
* @throws \InvalidArgumentException if the requested bridge name is invalid.
|
* @throws \InvalidArgumentException if the requested bridge name is invalid.
|
||||||
* @throws \Exception if the requested bridge doesn't exist in the working
|
* @throws \Exception if the requested bridge doesn't exist in the working
|
||||||
* directory.
|
* directory.
|
||||||
* @param string $nameBridge Name of the bridge object.
|
* @param string $name Name of the bridge object.
|
||||||
* @return object|bool The bridge object or false if the class is not instantiable.
|
* @return object|bool The bridge object or false if the class is not instantiable.
|
||||||
*/
|
*/
|
||||||
public static function create($nameBridge){
|
public static function create($name){
|
||||||
if(!preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $nameBridge)) {
|
if(!preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) {
|
||||||
throw new \InvalidArgumentException('Bridge name invalid!');
|
throw new \InvalidArgumentException('Bridge name invalid!');
|
||||||
}
|
}
|
||||||
|
|
||||||
$nameBridge = self::sanitizeBridgeName($nameBridge) . 'Bridge';
|
$name = self::sanitizeBridgeName($name) . 'Bridge';
|
||||||
$pathBridge = self::getWorkingDir() . $nameBridge . '.php';
|
$filePath = self::getWorkingDir() . $name . '.php';
|
||||||
|
|
||||||
if(!file_exists($pathBridge)) {
|
if(!file_exists($filePath)) {
|
||||||
throw new \Exception('Cache file ' . $pathBridge . ' does not exist!');
|
throw new \Exception('Cache file ' . $filePath . ' does not exist!');
|
||||||
}
|
}
|
||||||
|
|
||||||
require_once $pathBridge;
|
require_once $filePath;
|
||||||
|
|
||||||
if((new \ReflectionClass($nameBridge))->isInstantiable()) {
|
if((new \ReflectionClass($name))->isInstantiable()) {
|
||||||
return new $nameBridge();
|
return new $name();
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Reference in a new issue