119 lines
3.1 KiB
PHP
119 lines
3.1 KiB
PHP
<?php
|
|
|
|
/*
|
|
* AVSDev UF Worker (https://avsdev.uk)
|
|
*
|
|
* @link https://git.avsdev.uk/avsdev/sprinkle-worker
|
|
* @license https://git.avsdev.uk/avsdev/sprinkle-worker/blob/master/LICENSE.md (LGPL-3.0 License)
|
|
*/
|
|
|
|
namespace UserFrosting\Sprinkle\Worker\Jobs;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Str;
|
|
use Psr\Container\ContainerInterface;
|
|
use UserFrosting\UniformResourceLocator\Resource as ResourceInstance;
|
|
|
|
/**
|
|
* Job Inspector Class.
|
|
*
|
|
* Finds all job classes across sprinkles
|
|
*
|
|
* @author Craig Williams (https://avsdev.uk)
|
|
*/
|
|
class JobInspector
|
|
{
|
|
/**
|
|
* @var ContainerInterface
|
|
*/
|
|
protected $ci;
|
|
|
|
/**
|
|
* @var string The resource locator scheme
|
|
*/
|
|
protected $scheme = 'jobs://';
|
|
|
|
/**
|
|
* Class Constructor.
|
|
*
|
|
* @param ContainerInterface $ci
|
|
*/
|
|
public function __construct(ContainerInterface $ci)
|
|
{
|
|
$this->ci = $ci;
|
|
}
|
|
|
|
/**
|
|
* Loop all the available sprinkles and return a list of their jobs.
|
|
*
|
|
* @return array An array of all the task classes found for every sprinkle
|
|
*/
|
|
public function getAvailableJobDefinitions()
|
|
{
|
|
$tasks = $this->ci->locator->listResources($this->scheme, false, false);
|
|
|
|
return $this->loadAvailableJobDefinitions($tasks);
|
|
}
|
|
|
|
/**
|
|
* Process jobs Resource into info.
|
|
*
|
|
* @param array $taskFiles List of jobs files
|
|
*
|
|
* @return array
|
|
*/
|
|
protected function loadAvailableJobDefinitions(array $jobFiles)
|
|
{
|
|
$jobs = [];
|
|
|
|
foreach ($jobFiles as $jobFile) {
|
|
$job = $this->getJobDefinition($jobFile);
|
|
if ($job) {
|
|
$jobs[] = $job;
|
|
}
|
|
}
|
|
|
|
return $jobs;
|
|
}
|
|
|
|
/**
|
|
* Return an array of job details including the class name and the sprinkle name.
|
|
*
|
|
* @param ResourceInstance $file The job file
|
|
*
|
|
* @return array The details about a job file [name, class, sprinkle]
|
|
*/
|
|
protected function getJobDefinition(ResourceInstance $file)
|
|
{
|
|
/** @var \UserFrosting\System\Sprinkle\SprinkleManager */
|
|
$sprinkleManager = $this->ci->sprinkleManager;
|
|
|
|
// Format the sprinkle name for the namespace
|
|
$sprinkleName = $file->getLocation()->getName();
|
|
$sprinkleNS = $sprinkleManager->getSprinkleClassNamespace($sprinkleName);
|
|
|
|
// Getting base path, name and class name
|
|
$basePath = str_replace($file->getBasename(), '', $file->getBasePath());
|
|
$name = $basePath . $file->getFilename();
|
|
$className = str_replace('/', '\\', $basePath) . $file->getFilename();
|
|
$classPath = "\\$sprinkleNS\\Jobs\\$className";
|
|
|
|
// Exclude known helper classes
|
|
if ($className == "JobInterface" || $className == "JobBase" || $className == "JobInspector") {
|
|
return null;
|
|
}
|
|
|
|
$reflect = new \ReflectionClass($classPath);
|
|
if ($reflect->isAbstract()) {
|
|
return null;
|
|
}
|
|
|
|
// Build the class name and namespace
|
|
return [
|
|
'sprinkle' => $sprinkleName,
|
|
'name' => $name,
|
|
'class' => $classPath,
|
|
];
|
|
}
|
|
}
|