mirror of
				https://github.com/hauke68/Magallanes.git
				synced 2025-11-04 00:50:18 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			113 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
/*
 | 
						|
 * This file is part of the Magallanes package.
 | 
						|
 *
 | 
						|
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
						|
 *
 | 
						|
 * For the full copyright and license information, please view the LICENSE
 | 
						|
 * file that was distributed with this source code.
 | 
						|
 */
 | 
						|
 | 
						|
namespace Mage\Task;
 | 
						|
 | 
						|
use Mage\Runtime\RuntimeInterface;
 | 
						|
use Mage\Runtime\Exception\RuntimeException;
 | 
						|
use Symfony\Component\Finder\Finder;
 | 
						|
use Symfony\Component\Finder\SplFileInfo;
 | 
						|
 | 
						|
/**
 | 
						|
 * Task Factory
 | 
						|
 *
 | 
						|
 * @author Andrés Montañez <andresmontanez@gmail.com>
 | 
						|
 */
 | 
						|
class TaskFactory
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * @var RuntimeInterface
 | 
						|
     */
 | 
						|
    protected $runtime;
 | 
						|
 | 
						|
    /**
 | 
						|
     * @var array Registered Tasks
 | 
						|
     */
 | 
						|
    protected $registeredTasks = [];
 | 
						|
 | 
						|
    /**
 | 
						|
     * Constructor
 | 
						|
     *
 | 
						|
     * @param RuntimeInterface $runtime
 | 
						|
     */
 | 
						|
    public function __construct(RuntimeInterface $runtime)
 | 
						|
    {
 | 
						|
        $this->runtime = $runtime;
 | 
						|
        $this->loadBuiltInTasks();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Add a Task
 | 
						|
     *
 | 
						|
     * @param AbstractTask $task
 | 
						|
     */
 | 
						|
    public function add(AbstractTask $task)
 | 
						|
    {
 | 
						|
        $task->setRuntime($this->runtime);
 | 
						|
        $this->registeredTasks[$task->getName()] = $task;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Get a Task by it's registered Name/Code, or it can be a Class Name,
 | 
						|
     * in that case the class will be instantiated
 | 
						|
     *
 | 
						|
     * @param string $name Name/Code or Class of the Task
 | 
						|
     * @return AbstractTask
 | 
						|
     * @throws RuntimeException
 | 
						|
     */
 | 
						|
    public function get($name)
 | 
						|
    {
 | 
						|
        if (is_array($name)) {
 | 
						|
            $options = $name;
 | 
						|
            list($name) = array_keys($name);
 | 
						|
            $options = $options[$name];
 | 
						|
        } else {
 | 
						|
            $options = [];
 | 
						|
        }
 | 
						|
 | 
						|
        if (array_key_exists($name, $this->registeredTasks)) {
 | 
						|
            /** @var AbstractTask $task */
 | 
						|
            $task = $this->registeredTasks[$name];
 | 
						|
            $task->setOptions($options);
 | 
						|
            return $task;
 | 
						|
        } elseif (class_exists($name)) {
 | 
						|
            $task = new $name();
 | 
						|
            if ($task instanceof AbstractTask) {
 | 
						|
                $task->setOptions($options);
 | 
						|
                $this->add($task);
 | 
						|
                return $task;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        throw new RuntimeException(sprintf('Invalid task name "%s"', $name));
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Load BuiltIn Tasks
 | 
						|
     */
 | 
						|
    protected function loadBuiltInTasks()
 | 
						|
    {
 | 
						|
        $finder = new Finder();
 | 
						|
        $finder->files()->in(__DIR__ . '/BuiltIn')->name('*Task.php');
 | 
						|
 | 
						|
        /** @var SplFileInfo $file */
 | 
						|
        foreach ($finder as $file) {
 | 
						|
            $class = substr('\\Mage\\Task\\BuiltIn\\' . str_replace('/', '\\', $file->getRelativePathname()), 0, -4);
 | 
						|
            if (class_exists($class)) {
 | 
						|
                $task = new $class();
 | 
						|
 | 
						|
                if ($task instanceof AbstractTask) {
 | 
						|
                    $this->add($task);
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |