Completed
Pull Request — master (#331)
by Nikola
02:33
created

BaseAspectCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 47
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 4 1
A loadAspectKernel() 0 19 3
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2013, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Console\Command;
12
13
use Go\Core\AspectKernel;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Base command for all aspect commands
21
 */
22
class BaseAspectCommand extends Command
23
{
24
    /**
25
     * Stores an instance of aspect kernel
26
     *
27
     * @var null|AspectKernel
28
     */
29
    protected $aspectKernel = null;
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    protected function configure()
35
    {
36
        $this->addArgument('loader', InputArgument::REQUIRED, "Path to the aspect loader file");
37
    }
38
39
    /**
40
     * Loads aspect kernel.
41
     *
42
     * Aspect kernel is loaded by executing loader and fetching singleton instance.
43
     * If your application environment initializes aspect kernel differently, you may
44
     * modify this metod to get aspect kernel suitable to your needs.
45
     *
46
     * @param InputInterface $input
47
     * @param OutputInterface $output
48
     */
49
    protected function loadAspectKernel(InputInterface $input, OutputInterface $output)
50
    {
51
        $loader = $input->getArgument('loader');
52
        $path   = stream_resolve_include_path($loader);
53
        if (!is_readable($path)) {
54
            throw new \InvalidArgumentException("Invalid loader path: {$loader}");
55
        }
56
57
        ob_start();
58
        include_once $path;
59
        ob_clean();
60
61
        if (!class_exists(AspectKernel::class, false)) {
62
            $message = "Kernel was not initialized yet, please configure it in the {$path}";
63
            throw new \InvalidArgumentException($message);
64
        }
65
66
        $this->aspectKernel = AspectKernel::getInstance();
67
    }
68
}
69