Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Console.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1547d6aec09c137454d4a03b05b6c2986eac71bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik
 * @package Piwik
 */
namespace Piwik;

use Symfony\Component\Console\Application;

class Console
{
    public function run()
    {
        $console  = new Application();
        $commands = $this->getAvailableCommands();

        foreach ($commands as $command) {

            if (!class_exists($command)) {

                Log::warning(sprintf('Cannot add command %s, class does not exist', $command));

            } elseif (!is_subclass_of($command, 'Piwik\Plugin\ConsoleCommand')) {

                Log::warning(sprintf('Cannot add command %s, class does not extend Piwik\Plugin\ConsoleCommand', $command));

            } else {

                $console->add(new $command);
            }
        }

        $console->run();
    }

    /**
     * Returns a list of available command classnames.
     *
     * @return string[]
     */
    private function getAvailableCommands()
    {
        $commands = array();

        /**
         * Triggered to gather all available console commands. Plugins that want to expose new console commands
         * should subscribe to this event and add commands to the incoming array.
         *
         * **Example**
         * 
         *     public function addConsoleCommands(&$commands)
         *     {
         *         $commands[] = 'Piwik\Plugins\MyPlugin\Commands\MyCommand';
         *     }
         *
         * @param array &$commands An array containing a list of command class names.
         */
        Piwik::postEvent('Console.addCommands', array(&$commands));

        return $commands;
    }
}