Completed
Push — master ( ff8711...d29de7 )
by Pol
05:47
created

Taskman::createConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

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