Commit f0a32c97 authored by Ludovic Ganée's avatar Ludovic Ganée

la fonction WorkerShell::availablesWorkers est désormais statique et publique

parent 60da555f
Pipeline #8365 failed with stage
in 5 minutes and 54 seconds
......@@ -128,15 +128,7 @@ class WorkerShell extends Shell
public function main($worker = null)
{
// On récupère la liste des workers selon les valeurs par défaut ou le param dir
$dir = $this->param('dir');
if ($dir && !is_dir($dir) && is_dir(ROOT . DS . $dir)) {
$dir = ROOT . DS . $dir;
}
$dirs = (array)$dir;
if (!$dirs) {
$dirs = (array)Configure::read('Beanstalk.workerPaths', [APP.'Shell'.DS.'Worker']);
}
$workers = $this->availablesWorkers($dirs);
$workers = WorkerShell::availablesWorkers($this->param('dir'), $this->param('suffix'), $dir);
$availables = array_keys($workers);
// On récupère le worker défini en s'assurant qu'il soit parmis les dispos
......@@ -222,18 +214,33 @@ class WorkerShell extends Shell
/**
* Donne la liste des workers Disponibles sur un ensemble de répertoires
* @param array $dirs
* @param string|array $dirs
* @param string $suffix
* @return array[\ReflectionClass]
* @throws \ReflectionException
*/
protected function availablesWorkers(array $dirs)
public static function availablesWorkers($dirs = [], string $suffix = 'Worker')
{
if (is_string($dirs)) {
$dirs = (array)$dirs;
}
$dirs = array_filter($dirs);
foreach ($dirs as $key => $dir) {
// compatible chemins relatif
if (!is_dir($dir) && is_dir(ROOT . DS . $dir)) {
$dirs[$key] = ROOT . DS . $dir;
}
}
if (!$dirs) {
$dirs = (array)Configure::read('Beanstalk.workerPaths', [APP.'Shell'.DS.'Worker']);
}
$workers = [];
foreach ($dirs as $dir) {
foreach (glob($dir.DS.'*'.$this->param('suffix').'.php') as $filename) {
$this->parseWorkerClassname($filename, $workers);
foreach (glob($dir.DS.'*'.$suffix.'.php') as $filename) {
WorkerShell::parseWorkerClassname($filename, $workers, $suffix);
}
}
ksort($workers);
return $workers;
}
......@@ -242,16 +249,17 @@ class WorkerShell extends Shell
* si la classe utilise WorkerInterface et est instanciable
* @param string $filename
* @param array $workers
* @param string $suffix
* @throws \ReflectionException
*/
protected function parseWorkerClassname($filename, &$workers)
protected static function parseWorkerClassname($filename, &$workers, string $suffix = 'Worker')
{
$tokens = token_get_all(file_get_contents($filename));
$inNamespace = false;
$inClassname = false;
$classname = '';
$workername = Inflector::dasherize(
basename($filename, $this->param('suffix').'.php')
basename($filename, $suffix.'.php')
);
// Récupère le contenu du namespace et du classname par tokens dans le fichier
foreach ($tokens as $token) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment