PDFFile::writeConstants()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace create_documentation;
3
4
/**
5
 * Class that generates the PDF output.
6
 * @package create_documentation
7
 */
8
class PDFFile extends \Mpdf\Mpdf {
9
10
    /**
11
     * @var string
12
     */
13
    private $templatesPath = '/templates/default';
14
15
    /**
16
     * @var \League\Plates\Engine
17
     */
18
    private $templatesEngine;
19
20
    /**
21
     * @var string
22
     */
23
    private $path;
24
25
    /**
26
     * @var string
27
     */
28
    private $html = '';
29
30
    /**
31
     * PDFFile constructor.
32
     * @param string $path
33
     * @throws \Mpdf\MpdfException
34
     */
35
    public function __construct(string $path) {
36
        $this->templatesPath = \Phar::running() !== '' ? \Phar::running() . $this->templatesPath
37
            : '../../templates/default';
38
        $this->path = $path;
39
        $this->templatesEngine = new \League\Plates\Engine($this->templatesPath);
40
        parent::__construct(['tempDir' => sys_get_temp_dir() . '/mpdf']);
41
        $this->simpleTables = true;
42
    }
43
44
    /**
45
     * @param \phpDocumentor\Reflection\Php\Class_ $class
46
     * @throws \Mpdf\MpdfException
47
     */
48
    public function createFromClass(\phpDocumentor\Reflection\Php\Class_ $class): void {
49
        $parsedown = new \Parsedown();
50
        $summary = $class->getDocBlock() !== null ? $class->getDocBlock()->getSummary() : '';
51
        $description = $class->getDocBlock() !== null ? $parsedown->text($class->getDocBlock()->getDescription()) : '';
52
        $this->writeHeader($class->getName(), $summary, $description);
53
        $this->writeSummary($class->getConstants(), $class->getProperties(), $class->getMethods());
54
        $this->writeConstants($class->getConstants());
55
        $this->writeProperties($class->getProperties());
56
        $this->writeMethods($class->getMethods());
57
        $this->create();
58
    }
59
60
    /**
61
     * @param \phpDocumentor\Reflection\Php\Interface_ $interface
62
     * @throws \Mpdf\MpdfException
63
     */
64
    public function createFromInterface(\phpDocumentor\Reflection\Php\Interface_ $interface): void {
65
        $summary = $interface->getDocBlock() !== null ? $interface->getDocBlock()->getSummary() : '';
66
        $description = $interface->getDocBlock() !== null ? $interface->getDocBlock()->getDescription() : '';
67
        $this->writeHeader($interface->getName(), $summary, $description);
68
        $this->writeSummary($interface->getConstants(), array(), $interface->getMethods());
69
        $this->writeConstants($interface->getConstants());
70
        $this->writeMethods($interface->getMethods());
71
        $this->create();
72
    }
73
74
    /**
75
     * @param \phpDocumentor\Reflection\Php\Trait_ $trait
76
     * @throws \Mpdf\MpdfException
77
     */
78
    public function createFromTrait(\phpDocumentor\Reflection\Php\Trait_ $trait): void {
79
        $summary = $trait->getDocBlock() !== null ? $trait->getDocBlock()->getSummary() : '';
80
        $description = $trait->getDocBlock() !== null ? $trait->getDocBlock()->getDescription() : '';
81
        $this->writeHeader($trait->getName(), $summary, $description);
82
        $this->writeSummary(array(), $trait->getProperties(), $trait->getMethods());
83
        $this->writeProperties($trait->getProperties());
84
        $this->writeMethods($trait->getMethods());
85
        $this->create();
86
    }
87
88
    /**
89
     * @param string $name
90
     * @param string $summary
91
     * @param string $description
92
     * @throws \Mpdf\MpdfException
93
     */
94
    private function writeHeader(string $name, string $summary, string $description): void {
95
        $this->WriteHTML(file_get_contents($this->templatesPath . '/styles.css'),\Mpdf\HTMLParserMode::HEADER_CSS);
96
        $this->SetHeader('Generated with <a href="https://github.com/lluiscamino/phpDoc2pdf">phpDoc2pdf</a>');
97
        $this->html .= $this->templatesEngine->render('header', array ('name' => $name, 'summary' => $summary, 'description' => $description));
98
    }
99
100
    /**
101
     * @param \phpDocumentor\Reflection\Php\Constant[] $constants
102
     * @param \phpDocumentor\Reflection\Php\Property[] $properties
103
     * @param \phpDocumentor\Reflection\Php\Method[] $methods
104
     */
105
    private function writeSummary(array $constants, array $properties, array $methods): void {
106
        $constants_ = $properties_ = $methods_ = array(
107
            'public' => '',
108
            'protected' => '',
109
            'private' => ''
110
        );
111
        foreach ($constants as $constant) {
112
            $constants_['public'] .= '<a href="#constant:' . $constant->getName() . '">' . $constant->getName() . '</a><br>';
113
        }
114
        foreach ($properties as $property) {
115
            $properties_[strval($property->getVisibility())] .= '<a href="#property:' .$property->getName() . '">$' .$property->getName() . '</a><br>';
116
        }
117
        foreach ($methods as $method) {
118
            $methods_[strval($method->getVisibility())] .= '<a href="#method:' .$method->getName() . '">' .$method->getName() . '</a><br>';
119
        }
120
        $this->html .= $this->templatesEngine->render('summary', array('methods' => $methods_, 'properties' => $properties_, 'constants' => $constants_));
121
    }
122
123
    /**
124
     * @param \phpDocumentor\Reflection\Php\Constant[] $constants
125
     */
126
    private function writeConstants(array $constants): void {
127
        if (!empty($constants)) {
128
            $this->html .= $this->templatesEngine->render('constants', array('constants' => $constants));
129
        }
130
    }
131
132
    /**
133
     * @param \phpDocumentor\Reflection\Php\Property[] $properties
134
     */
135
    private function writeProperties(array $properties): void {
136
        if (!empty($properties)) {
137
            $this->html .= $this->templatesEngine->render('properties', array('properties' => $properties));
138
        }
139
    }
140
141
    /**
142
     * @param \phpDocumentor\Reflection\Php\Method[] $methods
143
     */
144
    private function writeMethods(array $methods): void {
145
        if (!empty($methods)) {
146
            $this->html .= $this->templatesEngine->render('methods', array('methods' => $methods));
147
        }
148
    }
149
150
    /**
151
     * Generates PDF file.
152
     * @throws \Mpdf\MpdfException
153
     */
154
    private function create(): void {
155
        $this->WriteHTML($this->html);
156
        $this->output($this->path . '.pdf', 'F');
157
    }
158
}