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