Passed
Pull Request — master (#9)
by Pol
04:54
created

Taskman::createJsonConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core;
6
7
use Composer\Autoload\ClassLoader;
8
use Consolidation\Config\ConfigInterface;
9
use Consolidation\Config\Loader\ConfigProcessor;
10
use League\Container\Container;
11
use League\Container\ContainerInterface;
12
use PhpTaskman\Core\Config\Loader\JsonConfigLoader;
13
use Robo\Application;
14
use Robo\Config\Config;
15
use Robo\Robo;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * Class Taskman.
22
 */
23
final class Taskman
24
{
25
    public const APPLICATION_NAME = 'Taskman';
26
    public const VERSION = 'dev-master';
27
28
    /**
29
     * Create default configuration.
30
     *
31
     * @param null|mixed $workingDir
32
     * @param mixed $paths
33
     *
34
     * @return \Consolidation\Config\ConfigInterface
35
     */
36 3
    public static function createConfiguration($paths, $workingDir = null)
37
    {
38 3
        $workingDir = $workingDir ?? getcwd();
39
40
        // Create a default configuration.
41 3
        $config = Robo::createConfiguration($paths);
42
43
        // Set the variable working_dir.
44 3
        if (false === $workingDir = realpath($workingDir)) {
45 1
            return $config;
46
        }
47
48 3
        $config->set('taskman.working_dir', $workingDir);
49
50 3
        $paths = \PhpTaskman\Core\Config\Config::findFilesToIncludeInConfiguration($workingDir);
51
52
        // Load the configuration.
53 3
        Robo::loadConfiguration(
54 3
            $paths,
55
            $config
56
        );
57
58 3
        return $config;
59
    }
60
61
    /**
62
     * Create and configure container.
63
     *
64
     * @param InputInterface $input
65
     * @param OutputInterface $output
66
     * @param Application $application
67
     * @param ConfigInterface $config
68
     * @param ClassLoader $classLoader
69
     *
70
     * @return Container|\League\Container\ContainerInterface
71
     */
72 2
    public static function createContainer(
73
        InputInterface $input,
74
        OutputInterface $output,
75
        Application $application,
76
        ConfigInterface $config,
77
        ClassLoader $classLoader
78
    ) {
79 2
        $container = Robo::createDefaultContainer($input, $output, $application, $config, $classLoader);
80 2
        $container->get('commandFactory')->setIncludeAllPublicMethods(false);
81
82 2
        return $container;
83
    }
84
85
    /**
86
     * @param null|string $appName
87
     * @param null|string $appVersion
88
     * @param null|string $workingDir
89
     *
90
     * @return Application
91
     */
92 2
    public static function createDefaultApplication($appName = null, $appVersion = null, $workingDir = null)
93
    {
94 2
        $appName = $appName ?? self::APPLICATION_NAME;
95 2
        $appVersion = $appVersion ?? self::VERSION;
96
97 2
        $app = Robo::createDefaultApplication($appName, $appVersion);
98
99 2
        if (null === $workingDir || false === $workingDir = realpath($workingDir)) {
100 2
            $workingDir = getcwd();
101
        }
102
103
        $app
104 2
            ->getDefinition()
105 2
            ->addOption(
106 2
                new InputOption(
107 2
                    '--working-dir',
108 2
                    null,
109 2
                    InputOption::VALUE_REQUIRED,
110 2
                    'Working directory, defaults to current working directory.',
111
                    $workingDir
112
                )
113
            );
114
115 2
        $app->setAutoExit(false);
116
117 2
        return $app;
118
    }
119
120
    /**
121
     * @param ContainerInterface $container
122
     *
123
     * @return \Robo\Runner
124
     */
125 1
    public static function createDefaultRunner(ContainerInterface $container)
126
    {
127 1
        return (new \Robo\Runner())
128 1
            ->setRelativePluginNamespace('Robo\Plugin')
129 1
            ->setContainer($container);
130
    }
131
132
    /**
133
     * @param string[] $paths
134
     *   Array of JSON filepaths.
135
     *
136
     * @return Config
137
     *   A config object.
138
     */
139 3
    public static function createJsonConfiguration(array $paths)
140
    {
141 3
        $config = new Config();
142 3
        self::loadJsonConfiguration($paths, $config);
143
144 3
        return $config;
145
    }
146
147
    /**
148
     * @param string $relativeNamespace
149
     *
150
     * @return array|string[]
151
     */
152
    public static function discoverTasksClasses($relativeNamespace)
153
    {
154
        /** @var \Robo\ClassDiscovery\RelativeNamespaceDiscovery $discovery */
155
        $discovery = Robo::service('relativeNamespaceDiscovery');
156
        $discovery->setRelativeNamespace($relativeNamespace . '\Task')
157
            ->setSearchPattern('*Task.php');
158
159
        return $discovery->getClasses();
160
    }
161
162
    /**
163
     * @param string[] $paths
164
     *   Array of JSON filepaths.
165
     * @param null|Config $config
166
     *   A config object.
167
     */
168 3
    public static function loadJsonConfiguration(array $paths, ?Config $config): void
169
    {
170 3
        if (null === $config) {
171
            // This needs to be removed when Robo will have the method replace()
172
            // in the ConfigInterface interface.
173
            /** @var Config $config */
174
            $config = Robo::config();
175
        }
176
177 3
        $loader = new JsonConfigLoader();
178 3
        $processor = new ConfigProcessor();
179 3
        $processor->add($config->export());
180
181 3
        foreach ($paths as $path) {
182 3
            $processor->extend($loader->load($path));
183
        }
184
185 3
        $config->replace($processor->export());
186 3
    }
187
}
188