summaryrefslogtreecommitdiffstats
path: root/src/Provision.php
blob: df8e2f9ae4c2bfd8192a294f1c5d5235de37e573 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php


namespace Aegir\Provision;

use Aegir\Provision\Commands\ExampleCommands;
use Aegir\Provision\Console\Config as ConsoleConfig;

use Consolidation\Config\Loader\ConfigProcessor;
use League\Container\Container;
use League\Container\ContainerAwareTrait;
use Robo\Collection\CollectionBuilder;
use Robo\Common\ConfigAwareTrait;
use Robo\Config\Config;
use Robo\Robo;
use Robo\Runner as RoboRunner;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Provision {
    
    const APPLICATION_NAME = 'Aegir Provision';
    const VERSION = '4.x-dev';
    const REPOSITORY = 'aegir-project/provision';
    
    use ConfigAwareTrait;
    use ContainerAwareTrait;
    
    public $runner;
    
    public function __construct(
//        Config $config,
        InputInterface $input = NULL,
        OutputInterface $output = NULL
    ) {
        
        // Prepare Console configuration and import it into Robo config.
        $consoleConfig = new \Aegir\Provision\Console\Config();

        $config = new Config();
        $config->import($consoleConfig->all());
        
        $this->setConfig($config);

        // Create Application.
        $application = new \Aegir\Provision\Application(self::APPLICATION_NAME, self::VERSION);
        $application->provision = $this;
        $application->console = $output;
        $application->setConfig($consoleConfig);
        
        // Create and configure container.
        $container = Robo::createDefaultContainer($input, $output, $application, $config);
        $this->setContainer($container);
        $this->configureContainer($container);
        
        // Instantiate Robo Runner.
        $this->runner = new RoboRunner([
            ExampleCommands::class
        ]);
        
        $this->runner->setContainer($container);
        $this->runner->setSelfUpdateRepository(self::REPOSITORY);
    }
    
    public function run(InputInterface $input, OutputInterface $output) {
        $status_code = $this->runner->run($input, $output);
        
        return $status_code;
    }
    
    /**
     * Register the necessary classes for BLT.
     */
    public function configureContainer(Container $container) {
    
        // FROM https://github.com/acquia/blt :
        // We create our own builder so that non-command classes are able to
        // implement task methods, like taskExec(). Yes, there are now two builders
        // in the container. "collectionBuilder" used for the actual command that
        // was executed, and "builder" to be used with non-command classes.
        $tasks = new ProvisionTasks();
        $builder = new CollectionBuilder($tasks);
        $tasks->setBuilder($builder);
        $container->add('builder', $builder);
    
    }
}