Completed
Push — develop ( 8fda15...dbbcf5 )
by Jaap
14s
created

src/phpDocumentor/Application/Stage/Transform.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 *  For the full copyright and license information, please view the LICENSE
6
 *  file that was distributed with this source code.
7
 *
8
 *  @copyright 2010-2017 Mike van Riel<[email protected]>
9
 *  @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 *  @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Application\Stage;
14
15
use phpDocumentor\Application\Console\Command\Command;
16
use phpDocumentor\Compiler\Compiler;
17
use phpDocumentor\Compiler\CompilerPassInterface;
18
use phpDocumentor\Descriptor\Cache\ProjectDescriptorMapper;
19
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
20
use phpDocumentor\Event\Dispatcher;
21
use phpDocumentor\Transformer\Event\PreTransformationEvent;
22
use phpDocumentor\Transformer\Event\PreTransformEvent;
23
use phpDocumentor\Transformer\Event\WriterInitializationEvent;
24
use phpDocumentor\Transformer\Template;
25
use phpDocumentor\Transformer\Transformation;
26
use phpDocumentor\Transformer\Transformer;
27
use Psr\Log\LoggerInterface;
28
use Symfony\Component\Filesystem\Filesystem;
29
use Symfony\Component\Stopwatch\Stopwatch;
30
use Zend\Cache\Storage\StorageInterface;
31
32
/**
33
 * Transforms the structure file into the specified output format
34
 *
35
 * This task will execute the transformation rules described in the given
36
 * template (defaults to 'responsive') with the given source (defaults to
37
 * output/structure.xml) and writes these to the target location (defaults to
38
 * 'output').
39
 *
40
 * It is possible for the user to receive additional information using the
41
 * verbose option or stop additional information using the quiet option. Please
42
 * take note that the quiet option also disables logging to file.
43
 */
44
final class Transform
45
{
46
    /** @var ProjectDescriptorBuilder $builder Object containing the project meta-data and AST */
47
    private $builder;
48
49
    /** @var Transformer $transformer Principal object for guiding the transformation process */
50
    private $transformer;
51
52
    /** @var Compiler $compiler Collection of pre-transformation actions (Compiler Passes) */
53
    private $compiler;
54
55
    /**
56
     * @var StorageInterface
57
     */
58
    private $cache;
59
60
    private $logger;
61
62
    /**
63
     * Initializes the command with all necessary dependencies to construct human-suitable output from the AST.
64
     */
65
    public function __construct(
66
        ProjectDescriptorBuilder $builder,
67
        Transformer $transformer,
68
        Compiler $compiler,
69
        StorageInterface $cache,
70
        LoggerInterface $logger
71
    ) {
72
        $this->builder = $builder;
73
        $this->transformer = $transformer;
74
        $this->compiler = $compiler;
75
        $this->cache = $cache;
76
        $this->logger = $logger;
77
78
        $this->connectOutputToEvents();
79
    }
80
81
    /**
82
     * Returns the builder object containing the AST and other meta-data.
83
     *
84
     * @return ProjectDescriptorBuilder
85
     */
86
    private function getBuilder()
87
    {
88
        return $this->builder;
89
    }
90
91
    /**
92
     * Returns the transformer used to guide the transformation process from AST to output.
93
     *
94
     * @return Transformer
95
     */
96
    private function getTransformer()
97
    {
98
        return $this->transformer;
99
    }
100
101
    /**
102
     * Executes the business logic involved with this command.
103
     *
104
     * @return integer
105
     * @throws \Exception if the target location is not a folder.
106
     */
107
    public function __invoke(array $configuration)
108
    {
109
        // initialize transformer
110
        $transformer = $this->getTransformer();
111
112
        $target = $configuration['phpdocumentor']['paths']['output']->getPath();
113
        $fileSystem = new Filesystem();
114
        if (! $fileSystem->isAbsolutePath((string) $target)) {
115
            $target = getcwd() . DIRECTORY_SEPARATOR . $target;
116
        }
117
        $transformer->setTarget((string) $target);
118
119
        $source = $configuration['phpdocumentor']['paths']['cache'];
120
        if (!file_exists($source) || !is_dir($source)) {
121
            throw new \Exception('Invalid source location provided, a path to an existing folder was expected');
122
        }
123
124
        $this->getCache()->getOptions()->setCacheDir($source);
125
126
        $projectDescriptor = $this->getBuilder()->getProjectDescriptor();
127
        $mapper = new ProjectDescriptorMapper($this->getCache());
128
129
        $stopWatch = new Stopwatch();
130
        $stopWatch->start('cache');
131
        $mapper->populate($projectDescriptor);
132
        $stopWatch->stop('cache');
133
134
        foreach (array_column($configuration['phpdocumentor']['templates'], 'name') as $template) {
135
            $stopWatch->start('load template');
136
            $this->transformer->getTemplates()->load($template);
137
//            $output->writeTimedLog(
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
138
//                'Preparing template "'. $template .'"',
139
//                array($transformer->getTemplates(), 'load'),
140
//                array($template, $transformer)
141
//            );
142
            $stopWatch->stop('load template');
143
        }
144
145
//        $output->writeTimedLog(
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
146
//            'Preparing ' . count($transformer->getTemplates()->getTransformations()) . ' transformations',
147
//            array($this, 'loadTransformations'),
148
//            array($transformer)
149
//        );
150
151
        //$this->loadTransformations($transformer);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% 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...
152
153
//        if ($progress) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
154
//            $progress->start($output, count($transformer->getTemplates()->getTransformations()));
155
//        }
156
157
        /** @var CompilerPassInterface $pass */
158
        foreach ($this->compiler as $pass) {
159
            $pass->execute($projectDescriptor);
160
            //$output->writeTimedLog($pass->getDescription(), array($pass, 'execute'), array($projectDescriptor));
0 ignored issues
show
Unused Code Comprehensibility introduced by
81% 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...
161
        }
162
163
//        if ($progress) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
164
//            $progress->finish();
165
//        }
166
167
        return 0;
168
    }
169
170
    /**
171
     * Returns the Cache.
172
     */
173
    private function getCache(): StorageInterface
174
    {
175
        return $this->cache;
176
    }
177
178
    /**
179
     * Connect a series of output messages to various events to display progress.
180
     */
181
    private function connectOutputToEvents()
182
    {
183
        Dispatcher::getInstance()->addListener(
184
            Transformer::EVENT_PRE_TRANSFORM,
185
            function (PreTransformEvent $event) {
186
                /** @var Transformer $transformer */
187
                $transformer = $event->getSubject();
188
                $templates = $transformer->getTemplates();
189
                $transformations = $templates->getTransformations();
190
                $this->logger->info(sprintf("\nApplying %d transformations", count($transformations)));
191
            }
192
        );
193
        Dispatcher::getInstance()->addListener(
194
            Transformer::EVENT_PRE_INITIALIZATION,
195
            function (WriterInitializationEvent $event) {
196
                $this->logger->info('  Initialize writer "' . get_class($event->getWriter()) . '"');
197
            }
198
        );
199
        Dispatcher::getInstance()->addListener(
200
            Transformer::EVENT_PRE_TRANSFORMATION,
201
            function (PreTransformationEvent $event) {
202
                $this->logger->info(
203
                    '  Execute transformation using writer "' . $event->getTransformation()->getWriter() . '"'
204
                );
205
            }
206
        );
207
    }
208
}
209