Completed
Push — 1.x ( c183a4...05b51a )
by Alexander
8s
created

WarmupCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 13
lcom 0
cbo 5
dl 0
loc 96
ccs 0
cts 73
cp 0
rs 10
c 2
b 1
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
C execute() 0 71 12
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 Go\Instrument\ClassLoading\SourceTransformingLoader;
15
use Go\Instrument\FileSystem\Enumerator;
16
use Go\Instrument\Transformer\FilterInjectorTransformer;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * Console command for warming the cache
24
 */
25
class WarmupCommand extends Command
26
{
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    protected function configure()
32
    {
33
        $this
34
            ->setName('goaop:warmup')
35
            ->addArgument('loader', InputArgument::REQUIRED, "Path to the aspect loader file")
36
            ->setDescription("Warm up the cache with woven aspects")
37
            ->setHelp(<<<EOT
38
Initializes the kernel and, if successful, warm up the cache for PHP
39
files under the application directory.
40
41
By default, the cache directory is taken from configured AspectKernel class.
42
EOT
43
            );
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $output->writeln("Loading aspect kernel for warmup...");
52
        $loader = $input->getArgument('loader');
53
        $path   = stream_resolve_include_path($loader);
54
        if (!is_readable($path)) {
55
            throw new \InvalidArgumentException("Invalid loader path: {$loader}");
56
        }
57
        include_once $path;
58
59
        if (!class_exists(AspectKernel::class, false)) {
60
            $message = "Kernel was not initialized yet, please configure it in the {$path}";
61
            throw new \InvalidArgumentException($message);
62
        }
63
64
        $kernel  = AspectKernel::getInstance();
65
        $options = $kernel->getOptions();
66
67
        if (empty($options['cacheDir'])) {
68
            throw new \InvalidArgumentException("Cache warmer require the `cacheDir` options to be configured");
69
        }
70
71
        $enumerator = new Enumerator($options['appDir'], $options['includePaths'], $options['excludePaths']);
72
        $iterator   = $enumerator->enumerate();
73
74
        $totalFiles = iterator_count($iterator);
75
        $output->writeln("Total <info>{$totalFiles}</info> files to process.");
76
        $iterator->rewind();
77
78
        set_error_handler(function($errno, $errstr, $errfile, $errline) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
79
            throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
80
        });
81
82
        $index  = 0;
83
        $errors = [];
84
        foreach ($iterator as $file) {
85
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
86
                $output->writeln("Processing file <info>{$file->getRealPath()}</info>");
87
            }
88
            $isSuccess = null;
0 ignored issues
show
Unused Code introduced by
$isSuccess is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
            try {
90
                // This will trigger creation of cache
91
                file_get_contents(
92
                    FilterInjectorTransformer::PHP_FILTER_READ .
93
                    SourceTransformingLoader::FILTER_IDENTIFIER .
94
                    "/resource=" . $file->getRealPath()
95
                );
96
                $isSuccess = true;
97
            } catch (\Exception $e) {
98
                $isSuccess = false;
99
                $errors[$file->getRealPath()] = $e;
100
            }
101
            if ($output->getVerbosity() == OutputInterface::VERBOSITY_NORMAL) {
102
                $output->write($isSuccess ? '.' : '<error>E</error>');
103
                if (++$index % 50 == 0) {
104
                    $output->writeln("($index/$totalFiles)");
105
                }
106
            }
107
        }
108
109
        restore_error_handler();
110
111
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
112
            foreach ($errors as $file=>$error) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after "=>"; 0 found
Loading history...
113
                $message = "File {$file} is not processed correctly due to exception: {$error->getMessage()}";
114
                $output->writeln($message);
115
            }
116
        }
117
118
        $output->writeln("<info>Done</info>");
119
    }
120
}
121