Completed
Push — master ( 09df77...d7ded3 )
by Pol
02:26
created

Taskman::createDefaultRunner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace phptaskman\core;
6
7
use Composer\Autoload\ClassLoader;
8
use League\Container\Container;
9
use phptaskman\core\Contract\ComposerAwareInterface;
10
use phptaskman\core\Services\Composer;
11
use Robo\Application;
12
use Robo\Config\Config;
13
use Robo\Robo;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * Class Taskman.
20
 */
21
final class Taskman
22
{
23
    public const APPLICATION_NAME = 'Taskman';
24
    public const VERSION = 'dev-master';
25
26
    /**
27
     * Create default configuration.
28
     *
29
     * @param null|mixed $workingDir
30
     *
31
     * @return Config
32
     */
33
    public static function createConfiguration($workingDir = null)
34
    {
35
        $workingDir = $workingDir ?? \getcwd();
36
37
        // Create a default configuration.
38
        $config = Robo::createConfiguration([]);
39
40
        // Set the variable working_dir.
41
        $config->set('taskman.working_dir', \realpath($workingDir));
42
43
        // Load the configuration.
44
        Robo::loadConfiguration(
45
            \phptaskman\core\Config\Config::findFilesToIncludeInConfiguration($workingDir),
46
            $config
47
        );
48
49
        return $config;
50
    }
51
52
    /**
53
     * Create and configure container.
54
     *
55
     * @param \Symfony\Component\Console\Input\InputInterface $input
56
     * @param \Symfony\Component\Console\Output\OutputInterface $output
57
     * @param \Robo\Application $application
58
     * @param \Robo\Config\Config $config
59
     * @param \Composer\Autoload\ClassLoader $classLoader
60
     *
61
     * @return \League\Container\Container|\League\Container\ContainerInterface
62
     */
63
    public static function createContainer(
64
        InputInterface $input,
65
        OutputInterface $output,
66
        Application $application,
67
        Config $config,
68
        ClassLoader $classLoader
69
    ) {
70
        $container = Robo::createDefaultContainer($input, $output, $application, $config, $classLoader);
71
        $container->get('commandFactory')->setIncludeAllPublicMethods(false);
72
        $container->share('taskman.composer', Composer::class)->withArgument(\getcwd());
73
74
        // Add service inflectors.
75
        if (null !== $service = $container->inflector(ComposerAwareInterface::class)) {
76
            $service->invokeMethod('setComposer', ['taskman.composer']);
77
        }
78
79
        return $container;
80
    }
81
82
    /**
83
     * @param null|string $appName
84
     * @param null|string $appVersion
85
     * @param null|string $workingDir
86
     *
87
     * @return \Robo\Application
88
     */
89
    public static function createDefaultApplication($appName = null, $appVersion = null, $workingDir = null)
90
    {
91
        $workingDir = $workingDir ?? \getcwd();
92
        $appName = $appName ?? self::APPLICATION_NAME;
93
        $appVersion = $appVersion ?? self::VERSION;
94
95
        $app = Robo::createDefaultApplication($appName, $appVersion);
96
97
        $app
98
            ->getDefinition()
99
            ->addOption(
100
                new InputOption(
101
                    '--working-dir',
102
                    null,
103
                    InputOption::VALUE_REQUIRED,
104
                    'Working directory, defaults to current working directory.',
105
                    \realpath($workingDir)
106
                )
107
            );
108
109
        $app->setAutoExit(false);
110
111
        return $app;
112
    }
113
114
    /**
115
     * @param \League\Container\Container $container
116
     *
117
     * @return \Robo\Runner
118
     */
119
    public static function createDefaultRunner(Container $container)
120
    {
121
        return (new \Robo\Runner())
122
            ->setRelativePluginNamespace('Robo\Plugin')
123
            ->setContainer($container);
124
    }
125
126
    /**
127
     * @param string $path
128
     *   The directory
129
     *
130
     * @return \phptaskman\core\Services\Composer
131
     *   The composer object.
132
     */
133
    public static function getComposerFromDirectory($path)
134
    {
135
        return new Composer($path);
136
    }
137
}
138