Completed
Pull Request — master (#675)
by Greg
03:05
created

Robo::storeCommandInstance()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
namespace Robo;
3
4
use League\Container\Container;
5
use League\Container\ContainerInterface;
6
use Robo\Common\ProcessExecutor;
7
use Consolidation\Config\ConfigInterface;
8
use Consolidation\Config\Loader\ConfigProcessor;
9
use Consolidation\Config\Loader\YamlConfigLoader;
10
use Symfony\Component\Console\Input\StringInput;
11
use Symfony\Component\Console\Application as SymfonyApplication;
12
use Symfony\Component\Process\Process;
13
14
/**
15
 * Manages the container reference and other static data.  Favor
16
 * using dependency injection wherever possible.  Avoid using
17
 * this class directly, unless setting up a custom DI container.
18
 */
19
class Robo
20
{
21
    const APPLICATION_NAME = 'Robo';
22
    const VERSION = '1.2.2-dev';
23
24
    /**
25
     * The currently active container object, or NULL if not initialized yet.
26
     *
27
     * @var ContainerInterface|null
28
     */
29
    protected static $container;
30
31
    /**
32
     * Entrypoint for standalone Robo-based tools.  See docs/framework.md.
33
     *
34
     * @param string[] $argv
35
     * @param string $commandClasses
36
     * @param null|string $appName
37
     * @param null|string $appVersion
38
     * @param null|\Symfony\Component\Console\Output\OutputInterface $output
39
     *
40
     * @return int
41
     */
42
    public static function run($argv, $commandClasses, $appName = null, $appVersion = null, $output = null, $repository = null)
43
    {
44
        $runner = new \Robo\Runner($commandClasses);
45
        $runner->setSelfUpdateRepository($repository);
46
        $statusCode = $runner->execute($argv, $appName, $appVersion, $output);
47
        return $statusCode;
48
    }
49
50
    /**
51
     * Sets a new global container.
52
     *
53
     * @param ContainerInterface $container
54
     *   A new container instance to replace the current.
55
     */
56
    public static function setContainer(ContainerInterface $container)
57
    {
58
        static::$container = $container;
59
    }
60
61
    /**
62
     * Unsets the global container.
63
     */
64
    public static function unsetContainer()
65
    {
66
        static::$container = null;
67
    }
68
69
    /**
70
     * Returns the currently active global container.
71
     *
72
     * @return \League\Container\ContainerInterface
73
     *
74
     * @throws \RuntimeException
75
     */
76
    public static function getContainer()
77
    {
78
        if (static::$container === null) {
79
            throw new \RuntimeException('container is not initialized yet. \Robo\Robo::setContainer() must be called with a real container.');
80
        }
81
        return static::$container;
82
    }
83
84
    /**
85
     * Returns TRUE if the container has been initialized, FALSE otherwise.
86
     *
87
     * @return bool
88
     */
89
    public static function hasContainer()
90
    {
91
        return static::$container !== null;
92
    }
93
94
    /**
95
     * Create a config object and load it from the provided paths.
96
     */
97
    public static function createConfiguration($paths)
98
    {
99
        $config = new \Robo\Config\Config();
100
        static::loadConfiguration($paths, $config);
101
        return $config;
102
    }
103
104
    /**
105
     * Use a simple config loader to load configuration values from specified paths
106
     */
107
    public static function loadConfiguration($paths, $config = null)
108
    {
109
        if ($config == null) {
110
            $config = static::config();
111
        }
112
        $loader = new YamlConfigLoader();
113
        $processor = new ConfigProcessor();
114
        $processor->add($config->export());
115
        foreach ($paths as $path) {
116
            $processor->extend($loader->load($path));
117
        }
118
        $config->import($processor->export());
119
    }
120
121
    /**
122
     * Create a container and initiailze it.  If you wish to *change*
123
     * anything defined in the container, then you should call
124
     * \Robo::configureContainer() instead of this function.
125
     *
126
     * @param null|\Symfony\Component\Console\Input\InputInterface $input
127
     * @param null|\Symfony\Component\Console\Output\OutputInterface $output
128
     * @param null|\Robo\Application $app
129
     * @param null|ConfigInterface $config
130
     *
131
     * @return \League\Container\Container|\League\Container\ContainerInterface
132
     */
133
    public static function createDefaultContainer($input = null, $output = null, $app = null, $config = null)
134
    {
135
        // Do not allow this function to be called more than once.
136
        if (static::hasContainer()) {
137
            return static::getContainer();
138
        }
139
140
        if (!$app) {
141
            $app = static::createDefaultApplication();
142
        }
143
144
        if (!$config) {
145
            $config = new \Robo\Config\Config();
146
        }
147
148
        // Set up our dependency injection container.
149
        $container = new Container();
150
        static::configureContainer($container, $app, $config, $input, $output);
151
152
        // Set the application dispatcher
153
        $app->setDispatcher($container->get('eventDispatcher'));
154
155
        return $container;
156
    }
157
158
    /**
159
     * Initialize a container with all of the default Robo services.
160
     * IMPORTANT:  after calling this method, clients MUST call:
161
     *
162
     * $dispatcher = $container->get('eventDispatcher');
163
     * $app->setDispatcher($dispatcher);
164
     *
165
     * Any modification to the container should be done prior to fetching
166
     * objects from it.
167
     *
168
     * It is recommended to use \Robo::createDefaultContainer()
169
     * instead, which does all required setup for the caller, but has
170
     * the limitation that the container it creates can only be
171
     * extended, not modified.
172
     *
173
     * @param \League\Container\ContainerInterface $container
174
     * @param \Symfony\Component\Console\Application $app
175
     * @param ConfigInterface $config
176
     * @param null|\Symfony\Component\Console\Input\InputInterface $input
177
     * @param null|\Symfony\Component\Console\Output\OutputInterface $output
178
     */
179
    public static function configureContainer(ContainerInterface $container, SymfonyApplication $app, ConfigInterface $config, $input = null, $output = null)
180
    {
181
        // Self-referential container refernce for the inflector
182
        $container->add('container', $container);
183
        static::setContainer($container);
184
185
        // Create default input and output objects if they were not provided
186
        if (!$input) {
187
            $input = new StringInput('');
188
        }
189
        if (!$output) {
190
            $output = new \Symfony\Component\Console\Output\ConsoleOutput();
191
        }
192
        $config->set(Config::DECORATED, $output->isDecorated());
193
        $config->set(Config::INTERACTIVE, $input->isInteractive());
194
195
        $container->share('application', $app);
196
        $container->share('config', $config);
197
        $container->share('input', $input);
198
        $container->share('output', $output);
199
        $container->share('outputAdapter', \Robo\Common\OutputAdapter::class);
200
201
        // Register logging and related services.
202
        $container->share('logStyler', \Robo\Log\RoboLogStyle::class);
203
        $container->share('logger', \Robo\Log\RoboLogger::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
204
            ->withArgument('output')
205
            ->withMethodCall('setLogOutputStyler', ['logStyler']);
206
        $container->add('progressBar', \Symfony\Component\Console\Helper\ProgressBar::class)
207
            ->withArgument('output');
208
        $container->share('progressIndicator', \Robo\Common\ProgressIndicator::class)
209
            ->withArgument('progressBar')
210
            ->withArgument('output');
211
        $container->share('resultPrinter', \Robo\Log\ResultPrinter::class);
212
        $container->add('simulator', \Robo\Task\Simulator::class);
213
        $container->share('globalOptionsEventListener', \Robo\GlobalOptionsEventListener::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
214
            ->withMethodCall('setApplication', ['application']);
215
        $container->share('injectConfigEventListener', \Consolidation\Config\Inject\ConfigForCommand::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
216
            ->withArgument('config')
217
            ->withMethodCall('setApplication', ['application']);
218
        $container->share('collectionProcessHook', \Robo\Collection\CollectionProcessHook::class);
219
        $container->share('alterOptionsCommandEvent', \Consolidation\AnnotatedCommand\Options\AlterOptionsCommandEvent::class)
220
            ->withArgument('application');
221
        $container->share('hookManager', \Consolidation\AnnotatedCommand\Hooks\HookManager::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
222
            ->withMethodCall('addCommandEvent', ['alterOptionsCommandEvent'])
223
            ->withMethodCall('addCommandEvent', ['injectConfigEventListener'])
224
            ->withMethodCall('addCommandEvent', ['globalOptionsEventListener'])
225
            ->withMethodCall('addResultProcessor', ['collectionProcessHook', '*']);
226
        $container->share('eventDispatcher', \Symfony\Component\EventDispatcher\EventDispatcher::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
227
            ->withMethodCall('addSubscriber', ['hookManager']);
228
        $container->share('formatterManager', \Consolidation\OutputFormatters\FormatterManager::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
229
            ->withMethodCall('addDefaultFormatters', [])
230
            ->withMethodCall('addDefaultSimplifiers', []);
231
        $container->share('prepareTerminalWidthOption', \Consolidation\AnnotatedCommand\Options\PrepareTerminalWidthOption::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
232
            ->withMethodCall('setApplication', ['application']);
233
        $container->share('commandProcessor', \Consolidation\AnnotatedCommand\CommandProcessor::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
234
            ->withArgument('hookManager')
235
            ->withMethodCall('setFormatterManager', ['formatterManager'])
236
            ->withMethodCall('addPrepareFormatter', ['prepareTerminalWidthOption'])
237
            ->withMethodCall(
238
                'setDisplayErrorFunction',
239
                [
240
                    function ($output, $message) use ($container) {
241
                        $logger = $container->get('logger');
242
                        $logger->error($message);
243
                    }
244
                ]
245
            );
246
        $container->share('commandFactory', \Consolidation\AnnotatedCommand\AnnotatedCommandFactory::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
247
            ->withMethodCall('setCommandProcessor', ['commandProcessor']);
248
249
        // Deprecated: favor using collection builders to direct use of collections.
250
        $container->add('collection', \Robo\Collection\Collection::class);
251
        // Deprecated: use CollectionBuilder::create() instead -- or, better
252
        // yet, BuilderAwareInterface::collectionBuilder() if available.
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
253
        $container->add('collectionBuilder', \Robo\Collection\CollectionBuilder::class);
254
255
        static::addInflectors($container);
256
257
        // Make sure the application is appropriately initialized.
258
        $app->setAutoExit(false);
259
    }
260
261
    /**
262
     * @param null|string $appName
263
     * @param null|string $appVersion
264
     *
265
     * @return \Robo\Application
266
     */
267
    public static function createDefaultApplication($appName = null, $appVersion = null)
268
    {
269
        $appName = $appName ?: self::APPLICATION_NAME;
270
        $appVersion = $appVersion ?: self::VERSION;
271
272
        $app = new \Robo\Application($appName, $appVersion);
273
        $app->setAutoExit(false);
274
        return $app;
275
    }
276
277
    /**
278
     * Add the Robo League\Container inflectors to the container
279
     *
280
     * @param \League\Container\ContainerInterface $container
281
     */
282
    public static function addInflectors($container)
283
    {
284
        // Register our various inflectors.
285
        $container->inflector(\Robo\Contract\ConfigAwareInterface::class)
286
            ->invokeMethod('setConfig', ['config']);
287
        $container->inflector(\Psr\Log\LoggerAwareInterface::class)
288
            ->invokeMethod('setLogger', ['logger']);
289
        $container->inflector(\League\Container\ContainerAwareInterface::class)
290
            ->invokeMethod('setContainer', ['container']);
291
        $container->inflector(\Symfony\Component\Console\Input\InputAwareInterface::class)
292
            ->invokeMethod('setInput', ['input']);
293
        $container->inflector(\Robo\Contract\OutputAwareInterface::class)
294
            ->invokeMethod('setOutput', ['output']);
295
        $container->inflector(\Robo\Contract\ProgressIndicatorAwareInterface::class)
296
            ->invokeMethod('setProgressIndicator', ['progressIndicator']);
297
        $container->inflector(\Consolidation\AnnotatedCommand\Events\CustomEventAwareInterface::class)
298
            ->invokeMethod('setHookManager', ['hookManager']);
299
        $container->inflector(\Robo\Contract\VerbosityThresholdInterface::class)
300
            ->invokeMethod('setOutputAdapter', ['outputAdapter']);
301
    }
302
303
    /**
304
     * Given the fully qualified namespace and classname of a command instance,
305
     * instantiate it and store it in our container.
306
     *
307
     * @param string $commandClass
308
     *
309
     * @return null|object
310
     */
311
    public static function storeCommandInstance($commandClass)
312
    {
313
        $container = Robo::getContainer();
314
315
        if (!class_exists($commandClass)) {
316
            return;
317
        }
318
        $reflectionClass = new \ReflectionClass($commandClass);
319
        if ($reflectionClass->isAbstract()) {
320
            return;
321
        }
322
323
        $commandFileName = static::commandInstanceContainerName($commandClass);
324
        $container->share($commandFileName, $commandClass);
325
        return static::getCommandInstance($commandClass);
326
    }
327
328
    /**
329
     * Given the fully qualified namespace and classname of a command instance,
330
     * look it up from our container.
331
     *
332
     * @param string $commandClass
333
     * @return object
334
     */
335
    public static function getCommandInstance($commandClass)
336
    {
337
        $commandFileName = static::commandInstanceContainerName($commandClass);
338
        return static::service($commandFileName);
339
    }
340
341
    protected static function commandInstanceContainerName($commandClass)
342
    {
343
        return "{$commandClass}Commands";
344
    }
345
346
    /**
347
     * Retrieves a service from the container.
348
     *
349
     * Use this method if the desired service is not one of those with a dedicated
350
     * accessor method below. If it is listed below, those methods are preferred
351
     * as they can return useful type hints.
352
     *
353
     * @param string $id
354
     *   The ID of the service to retrieve.
355
     *
356
     * @return mixed
357
     *   The specified service.
358
     */
359
    public static function service($id)
360
    {
361
        return static::getContainer()->get($id);
362
    }
363
364
    /**
365
     * Indicates if a service is defined in the container.
366
     *
367
     * @param string $id
368
     *   The ID of the service to check.
369
     *
370
     * @return bool
371
     *   TRUE if the specified service exists, FALSE otherwise.
372
     */
373
    public static function hasService($id)
374
    {
375
        // Check hasContainer() first in order to always return a Boolean.
376
        return static::hasContainer() && static::getContainer()->has($id);
377
    }
378
379
    /**
380
     * Return the result printer object.
381
     *
382
     * @return \Robo\Log\ResultPrinter
383
     */
384
    public static function resultPrinter()
385
    {
386
        return static::service('resultPrinter');
387
    }
388
389
    /**
390
     * @return ConfigInterface
391
     */
392
    public static function config()
393
    {
394
        return static::service('config');
395
    }
396
397
    /**
398
     * @return \Consolidation\Log\Logger
399
     */
400
    public static function logger()
401
    {
402
        return static::service('logger');
403
    }
404
405
    /**
406
     * @return \Robo\Application
407
     */
408
    public static function application()
409
    {
410
        return static::service('application');
411
    }
412
413
    /**
414
     * Return the output object.
415
     *
416
     * @return \Symfony\Component\Console\Output\OutputInterface
417
     */
418
    public static function output()
419
    {
420
        return static::service('output');
421
    }
422
423
    /**
424
     * Return the input object.
425
     *
426
     * @return \Symfony\Component\Console\Input\InputInterface
427
     */
428
    public static function input()
429
    {
430
        return static::service('input');
431
    }
432
433
    public static function process(Process $process)
434
    {
435
        return ProcessExecutor::create(static::getContainer(), $process);
436
    }
437
}
438