Skip to content

Commit 3c18b81

Browse files
committed
requireStrategy() now able to make use of autoloader, like that of Composer, to load strategies
1 parent 5f3cbaa commit 3c18b81

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

lib/Opauth/Opauth.php

+29-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public function run(){
106106
$safeEnv = $this->env;
107107
unset($safeEnv['Strategy']);
108108

109-
require $this->env['strategy_dir'].$class.'/'.$class.'.php';
110-
$this->Strategy = new $class($strategy, $safeEnv);
109+
$actualClass = $this->requireStrategy($class);
110+
$this->Strategy = new $actualClass($strategy, $safeEnv);
111111
$this->Strategy->callAction($this->env['params']['action']);
112112
}
113113
else{
@@ -258,6 +258,33 @@ public function callback(){
258258
echo "</pre>";
259259
}
260260

261+
/**
262+
* Loads a strategy, firstly check if the strategy's class already exists, especially for users of Composer;
263+
* If it isn't, attempts to load it from $this->env['strategy_dir']
264+
*
265+
* @param string $strategy Name of a strategy
266+
* @return string Class name of the strategy, usually StrategyStrategy
267+
*/
268+
private function requireStrategy($strategy){
269+
if (!class_exists($strategy.'Strategy')){
270+
if (file_exists($this->env['strategy_dir'].$strategy.'/'.$strategy.'Strategy.php')){
271+
require $this->env['strategy_dir'].$strategy.'/'.$strategy.'Strategy.php';
272+
return $strategy.'Strategy';
273+
}
274+
275+
// Deprecated support for strategies without Strategy postfix as class name or filename
276+
elseif (file_exists($this->env['strategy_dir'].$strategy.'/'.$strategy.'.php')){
277+
require $this->env['strategy_dir'].$strategy.'/'.$strategy.'.php';
278+
return $strategy;
279+
}
280+
281+
else{
282+
trigger_error('Strategy class file ('.$this->env['strategy_dir'].$strategy.'/'.$strategy.'Strategy.php'.') is missing', E_USER_ERROR);
283+
exit();
284+
}
285+
}
286+
return $strategy.'Strategy';
287+
}
261288

262289
/**
263290
* Prints out variable with <pre> tags

0 commit comments

Comments
 (0)