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