Documentarian::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
namespace Mpociot\Documentarian;
4
5
use Illuminate\Support\Arr;
6
use Mni\FrontYAML\Parser;
7
use Windwalker\Renderer\BladeRenderer;
8
9
/**
10
 * Class Documentarian
11
 * @package Mpociot\Documentarian
12
 */
13
class Documentarian
14
{
15
16
    /**
17
     * Returns a config value
18
     *
19
     * @param string $key
20
     * @return mixed
21
     */
22
    public function config($folder, $key = null)
23
    {
24
        $config = include($folder . '/source/config.php');
25
26
        return is_null($key) ? $config : Arr::get($config, $key);
27
    }
28
29
    /**
30
     * Create a new API documentation folder and copy all needed files/stubs
31
     *
32
     * @param $folder
33
     */
34
    public function create($folder)
35
    {
36
        $folder = $folder . '/source';
37
        if (!is_dir($folder)) {
38
            mkdir($folder, 0777, true);
39
            mkdir($folder . '/../css');
40
            mkdir($folder . '/../js');
41
            mkdir($folder . '/includes');
42
            mkdir($folder . '/assets');
43
        }
44
45
        // copy stub files
46
        copy(__DIR__ . '/../resources/stubs/index.md', $folder . '/index.md');
47
        copy(__DIR__ . '/../resources/stubs/gitignore.stub', $folder . '/.gitignore');
48
        copy(__DIR__ . '/../resources/stubs/includes/_errors.md', $folder . '/includes/_errors.md');
49
        copy(__DIR__ . '/../resources/stubs/package.json', $folder . '/package.json');
50
        copy(__DIR__ . '/../resources/stubs/gulpfile.js', $folder . '/gulpfile.js');
51
        copy(__DIR__ . '/../resources/stubs/config.php', $folder . '/config.php');
52
        copy(__DIR__ . '/../resources/stubs/js/all.js', $folder . '/../js/all.js');
53
        copy(__DIR__ . '/../resources/stubs/css/style.css', $folder . '/../css/style.css');
54
55
        // copy resources
56
        rcopy(__DIR__ . '/../resources/images/', $folder . '/assets/images');
57
        rcopy(__DIR__ . '/../resources/js/', $folder . '/assets/js');
58
        rcopy(__DIR__ . '/../resources/stylus/', $folder . '/assets/stylus');
59
    }
60
61
    /**
62
     * Generate the API documentation using the markdown and include files
63
     *
64
     * @param $folder
65
     * @return false|null
66
     */
67
    public function generate($folder)
68
    {
69
        $source_dir = $folder . '/source';
70
71
        if (!is_dir($source_dir)) {
72
            return false;
73
        }
74
75
        $parser = new Parser();
76
77
        $document = $parser->parse(file_get_contents($source_dir . '/index.md'));
78
79
        $frontmatter = $document->getYAML();
80
        $html = $document->getContent();
81
82
        $renderer = new BladeRenderer([__DIR__ . '/../resources/views'], ['cache_path' => $source_dir . '/_tmp']);
0 ignored issues
show
Documentation introduced by
array(__DIR__ . '/../resources/views') is of type array<integer,string,{"0":"string"}>, but the function expects a object<SplPriorityQueue>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
84
        // Parse and include optional include markdown files
85
        if (isset($frontmatter['includes'])) {
86
            foreach ($frontmatter['includes'] as $include) {
87
                if (file_exists($include_file = $source_dir . '/includes/_' . $include . '.md')) {
88
                    $document = $parser->parse(file_get_contents($include_file));
89
                    $html .= $document->getContent();
90
                }
91
            }
92
        }
93
94
        $output = $renderer->render('index', [
95
            'page' => $frontmatter,
96
            'content' => $html
97
        ]);
98
99
        file_put_contents($folder . '/index.html', $output);
100
101
        // Copy assets
102
        rcopy($source_dir . '/assets/images/', $folder . '/images');
103
        rcopy($source_dir . '/assets/stylus/fonts/', $folder . '/css/fonts');
104
    }
105
106
}