Completed
Push — develop ( 80740b...61b5c3 )
by Mike
10:20
created

Sourcecode   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B transform() 0 71 4
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\Transformer\Writer;
17
18
use phpDocumentor\Descriptor\FileDescriptor;
19
use phpDocumentor\Descriptor\ProjectDescriptor;
20
use phpDocumentor\Transformer\Transformation;
21
use phpDocumentor\Transformer\Writer\WriterAbstract;
22
23
/**
24
 * Sourcecode transformation writer; generates syntax highlighted source files in a destination's subfolder.
25
 */
26
class Sourcecode extends WriterAbstract
27
{
28
    /**
29
     * This method writes every source code entry in the structure file to a highlighted file.
30
     *
31
     * @param ProjectDescriptor $project        Document containing the structure.
32
     * @param Transformation    $transformation Transformation to execute.
33
     */
34
    public function transform(ProjectDescriptor $project, Transformation $transformation)
35
    {
36
        $artifact = $transformation->getTransformer()->getTarget()
37
            . DIRECTORY_SEPARATOR
38
            . ($transformation->getArtifact()
39
                ?: 'source');
40
41
        /** @var FileDescriptor $file */
42
        foreach ($project->getFiles() as $file) {
43
            $filename = $file->getPath();
44
            $source = $file->getSource();
45
46
            $root = str_repeat('../', count(explode(DIRECTORY_SEPARATOR, $filename)));
47
            $path = $artifact . DIRECTORY_SEPARATOR . $filename;
48
            if (!file_exists(dirname($path))) {
49
                mkdir(dirname($path), 0755, true);
50
            }
51
52
            $source = htmlentities($source);
53
            file_put_contents(
54
                $path . '.html',
55
                <<<HTML
56
<html>
57
    <head>
58
        <script
59
            type="text/javascript"
60
            src="{$root}js/jquery-1.4.2.min.js">
61
        </script>
62
        <script
63
            type="text/javascript"
64
            src="{$root}syntax_highlighter/scripts/shCore.js">
65
        </script>
66
        <script
67
            type="text/javascript"
68
            src="{$root}syntax_highlighter/scripts/shBrushJScript.js">
69
        </script>
70
        <script
71
            type="text/javascript"
72
            src="{$root}syntax_highlighter/scripts/shBrushPhp.js">
73
        </script>
74
        <script
75
            type="text/javascript"
76
            src="{$root}syntax_highlighter/scripts/shBrushXml.js">
77
        </script>
78
        <link
79
            href="{$root}syntax_highlighter/styles/shCore.css" rel="stylesheet"
80
            type="text/css"
81
        />
82
        <link
83
            href="{$root}syntax_highlighter/styles/shCoreEclipse.css"
84
            rel="stylesheet" type="text/css"
85
        />
86
        <link
87
            href="{$root}syntax_highlighter/styles/shThemeWordpress.css"
88
            rel="stylesheet" type="text/css"
89
        />
90
    </head>
91
    <body>
92
        <pre class="brush: php">${source}</pre>
93
        <script type="text/javascript">
94
             SyntaxHighlighter.all();
95
             jQuery('.gutter div').each(function(key, data){
96
                jQuery(data).prepend('<a name="L'+jQuery(data).text()+'"/>');
97
             });
98
        </script>
99
    </body>
100
</html>
101
HTML
102
            );
103
        }
104
    }
105
}
106