Completed
Push — master ( 8d6189...196a37 )
by Pol
04:33 queued 31s
created

src/Taskman.php (2 issues)

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core;
6
7
use Composer\Autoload\ClassLoader;
8
use Consolidation\Config\Loader\ConfigProcessor;
9
use League\Container\Container;
10
use League\Container\ContainerInterface;
11
use PhpTaskman\Core\Config\Loader\JsonConfigLoader;
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 3
    public static function createConfiguration($paths, $workingDir = null)
36
    {
37 3
        $workingDir = $workingDir ?? \getcwd();
38
39
        // Create a default configuration.
40 3
        $config = Robo::createConfiguration($paths);
41
42
        // Set the variable working_dir.
43 3
        if (false === $workingDir = \realpath($workingDir)) {
44 1
            return $config;
45
        }
46
47 3
        $config->set('taskman.working_dir', \realpath($workingDir));
48
49
        // Load the configuration.
50 3
        Robo::loadConfiguration(
51 3
            \PhpTaskman\Core\Config\Config::findFilesToIncludeInConfiguration($workingDir),
52
            $config
53
        );
54
55 3
        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 2
    public static function createContainer(
70
        InputInterface $input,
71
        OutputInterface $output,
72
        Application $application,
73
        Config $config,
74
        ClassLoader $classLoader
75
    ) {
76 2
        $container = Robo::createDefaultContainer($input, $output, $application, $config, $classLoader);
77 2
        $container->get('commandFactory')->setIncludeAllPublicMethods(false);
78
79 2
        return $container;
80
    }
81
82
    /**
83
     * @param null|string $appName
84
     * @param null|string $appVersion
85
     * @param null|string $workingDir
86
     *
87
     * @return Application
88
     */
89 2
    public static function createDefaultApplication($appName = null, $appVersion = null, $workingDir = null)
90
    {
91 2
        $workingDir = $workingDir ?? \getcwd();
92 2
        $appName = $appName ?? self::APPLICATION_NAME;
93 2
        $appVersion = $appVersion ?? self::VERSION;
94
95 2
        $app = Robo::createDefaultApplication($appName, $appVersion);
96
97
        $app
98 2
            ->getDefinition()
99 2
            ->addOption(
100 2
                new InputOption(
101 2
                    '--working-dir',
102 2
                    null,
103 2
                    InputOption::VALUE_REQUIRED,
104 2
                    'Working directory, defaults to current working directory.',
105 2
                    \realpath($workingDir)
106
                )
107
            );
108
109 2
        $app->setAutoExit(false);
110
111 2
        return $app;
112
    }
113
114
    /**
115
     * @param ContainerInterface $container
116
     *
117
     * @return \Robo\Runner
118
     */
119 1
    public static function createDefaultRunner(ContainerInterface $container)
120
    {
121 1
        return (new \Robo\Runner())
122 1
            ->setRelativePluginNamespace('Robo\Plugin')
123 1
            ->setContainer($container);
124
    }
125
126
    /**
127
     * @param $paths
128
     *
129
     * @return Config
130
     */
131 3
    public static function createJsonConfiguration($paths)
132
    {
133 3
        $config = new Config();
134 3
        self::loadJsonConfiguration($paths, $config);
135
136 3
        return $config;
137
    }
138
139
    /**
140
     * @param $paths
141
     * @param null $config
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $config is correct as it would always require null to be passed?
Loading history...
142
     */
143 3
    public static function loadJsonConfiguration($paths, $config = null)
144
    {
145 3
        if (null === $config) {
0 ignored issues
show
The condition null === $config is always true.
Loading history...
146
            $config = Robo::config();
147
        }
148
149 3
        $loader = new JsonConfigLoader();
150 3
        $processor = new ConfigProcessor();
151 3
        $processor->add($config->export());
152
153 3
        foreach ($paths as $path) {
154 3
            $processor->extend($loader->load($path));
155
        }
156
157 3
        $config->import($processor->export());
158 3
    }
159
}
160