Completed
Push — develop ( 3bc77a...996ea2 )
by Jaap
06:58
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
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Application\Stage;
17
18
use Exception;
19
use phpDocumentor\Compiler\Compiler;
20
use phpDocumentor\Descriptor\Cache\ProjectDescriptorMapper;
21
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
22
use phpDocumentor\Event\Dispatcher;
23
use phpDocumentor\Transformer\Event\PreTransformationEvent;
24
use phpDocumentor\Transformer\Event\PreTransformEvent;
25
use phpDocumentor\Transformer\Event\WriterInitializationEvent;
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
    /** @var StorageInterface */
56
    private $cache;
57
58
    /** @var LoggerInterface */
59
    private $logger;
60
61
    /**
62
     * Initializes the command with all necessary dependencies to construct human-suitable output from the AST.
63
     */
64
    public function __construct(
65
        ProjectDescriptorBuilder $builder,
66
        Transformer $transformer,
67
        Compiler $compiler,
68
        StorageInterface $cache,
69
        LoggerInterface $logger
70
    ) {
71
        $this->builder = $builder;
72
        $this->transformer = $transformer;
73
        $this->compiler = $compiler;
74
        $this->cache = $cache;
75
        $this->logger = $logger;
76
77
        $this->connectOutputToEvents();
78
    }
79
80
    /**
81
     * Returns the builder object containing the AST and other meta-data.
82
     */
83
    private function getBuilder(): ProjectDescriptorBuilder
84
    {
85
        return $this->builder;
86
    }
87
88
    /**
89
     * Returns the transformer used to guide the transformation process from AST to output.
90
     */
91
    private function getTransformer(): Transformer
92
    {
93
        return $this->transformer;
94
    }
95
96
    /**
97
     * Executes the business logic involved with this command.
98
     *
99
     * @throws Exception if the target location is not a folder.
100
     */
101
    public function __invoke(array $configuration): int
102
    {
103
        // initialize transformer
104
        $transformer = $this->getTransformer();
105
106
        $target = $configuration['phpdocumentor']['paths']['output']->getPath();
107
        $fileSystem = new Filesystem();
108
        if (! $fileSystem->isAbsolutePath((string) $target)) {
109
            $target = getcwd() . DIRECTORY_SEPARATOR . $target;
110
        }
111
112
        $transformer->setTarget((string) $target);
113
114
        $projectDescriptor = $this->getBuilder()->getProjectDescriptor();
115
        $mapper = new ProjectDescriptorMapper($this->getCache());
116
117
        $stopWatch = new Stopwatch();
118
        $stopWatch->start('cache');
119
        $mapper->populate($projectDescriptor);
120
        $stopWatch->stop('cache');
121
122
        foreach (array_column($configuration['phpdocumentor']['templates'], 'name') as $template) {
123
            $stopWatch->start('load template');
124
            $this->transformer->getTemplates()->load($template);
125
//            $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...
126
//                'Preparing template "'. $template .'"',
127
//                array($transformer->getTemplates(), 'load'),
128
//                array($template, $transformer)
129
//            );
130
            $stopWatch->stop('load template');
131
        }
132
133
//        $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...
134
//            'Preparing ' . count($transformer->getTemplates()->getTransformations()) . ' transformations',
135
//            array($this, 'loadTransformations'),
136
//            array($transformer)
137
//        );
138
139
        //$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...
140
141
//        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...
142
//            $progress->start($output, count($transformer->getTemplates()->getTransformations()));
143
//        }
144
145
        /** @var \phpDocumentor\Compiler\CompilerPassInterface $pass */
146
        foreach ($this->compiler as $pass) {
147
            $pass->execute($projectDescriptor);
148
            //$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...
149
        }
150
151
//        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...
152
//            $progress->finish();
153
//        }
154
155
        return 0;
156
    }
157
158
    /**
159
     * Returns the Cache.
160
     */
161
    private function getCache(): StorageInterface
162
    {
163
        return $this->cache;
164
    }
165
166
    /**
167
     * Connect a series of output messages to various events to display progress.
168
     */
169
    private function connectOutputToEvents(): void
170
    {
171
        Dispatcher::getInstance()->addListener(
172
            Transformer::EVENT_PRE_TRANSFORM,
173
            function (PreTransformEvent $event) {
174
                /** @var Transformer $transformer */
175
                $transformer = $event->getSubject();
176
                $templates = $transformer->getTemplates();
177
                $transformations = $templates->getTransformations();
178
                $this->logger->info(sprintf("\nApplying %d transformations", count($transformations)));
179
            }
180
        );
181
        Dispatcher::getInstance()->addListener(
182
            Transformer::EVENT_PRE_INITIALIZATION,
183
            function (WriterInitializationEvent $event) {
184
                $this->logger->info('  Initialize writer "' . get_class($event->getWriter()) . '"');
185
            }
186
        );
187
        Dispatcher::getInstance()->addListener(
188
            Transformer::EVENT_PRE_TRANSFORMATION,
189
            function (PreTransformationEvent $event) {
190
                $this->logger->info(
191
                    '  Execute transformation using writer "' . $event->getTransformation()->getWriter() . '"'
192
                );
193
            }
194
        );
195
    }
196
}
197