Completed
Push — master ( 4409a5...8614c9 )
by Martijn van
02:32
created

ConsoleController::ConsoleForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
4
use Symfony\Component\Console\Application;
5
use Symfony\Component\Console\Input\ArgvInput;
6
use Symfony\Component\Console\Input\ArrayInput;
7
use Symfony\Component\Console\Output\BufferedOutput;
8
use Symfony\Component\Console\Output\ConsoleOutput;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Class ConsoleController
13
 * The Central Command Access Point and Bootstrapper.
14
 */
15
class ConsoleController extends Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
16
{
17
    /**
18
     * @var array
19
     */
20
    private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
        'publish',
22
        'ConsoleForm'
23
    ];
24
25
    /**
26
     * @var SilverstripeApplication
27
     */
28
    protected $application;
29
30
    public function init()
31
    {
32
        parent::init();
33
34
        $this->application = new SilverstripeApplication();
35
    }
36
37
    public function index(SS_HTTPRequest $request)
38
    {
39
        /**
40
         * Lets be clear when calling commands
41
         */
42
        ini_set('display_errors', 1);
43
44
        if(!Director::is_cli()) {
45
            if(!Permission::check("ADMIN")) {
46
                return Security::permissionFailure();
47
            }
48
            return $this->callFromBrowser($request);
49
        }
50
51
        return $this->callFromConsole();
52
    }
53
54
    protected function callFromConsole()
0 ignored issues
show
Coding Style introduced by
callFromConsole uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
55
    {
56
        // remove the framework/cli-script.php argument
57
        array_shift($_SERVER['argv']);
58
59
        $this->application->run(new ArgvInput($_SERVER['argv']), new ConsoleOutput());
60
    }
61
62
    protected function callFromBrowser(SS_HTTPRequest $request)
63
    {
64
        $input = new ArrayInput(array(
65
            'command' => $request->param('ID')
66
        ));
67
68
        $output = new BufferedOutput(
69
            OutputInterface::VERBOSITY_NORMAL,
70
            true // true for decorated
71
        );
72
        $this->application->run($input, $output);
73
74
        // return the output
75
        $converter = new AnsiToHtmlConverter();
76
        $content = $output->fetch();
77
78
        return ['ConsoleOutput' => $converter->convert($content)];
79
    }
80
    /**
81
     * @return ConsoleForm
82
     */
83
    public function ConsoleForm()
84
    {
85
        return new ConsoleForm($this, 'ConsoleForm');
86
    }
87
}
88