alt3 / cakephp-swagger
1
<?php
2
declare(strict_types=1);
3

4
namespace Alt3\Swagger\Lib;
5

6
use Cake\Core\Configure;
7
use Cake\Filesystem\File;
8
use Cake\Http\Exception\InternalErrorException;
9
use Cake\Http\Exception\NotFoundException;
10

11
class SwaggerTools
12
{
13
    /**
14
     * @var string Prepended to filesystem swagger json files
15
     */
16
    protected static $filePrefix = 'cakephp_swagger_';
17

18
    /**
19
     * Returns a single swagger document from filesystem or crawl-generates
20
     * a fresh one.
21
     *
22
     * @param string $id Name of the document
23
     * @param string $host Hostname of system serving swagger documents (without protocol)
24
     * @throws \InvalidArgumentException
25
     * @return string
26
     */
27 1
    public static function getSwaggerDocument($id, $host)
28
    {
29
        // load document from filesystem
30 1
        $filePath = CACHE . self::$filePrefix . $id . '.json';
31 1
        if (!Configure::read('Swagger.docs.crawl')) {
32 1
            if (!file_exists($filePath)) {
33 1
                throw new NotFoundException("Swagger json document was not found on filesystem: $filePath");
34
            }
35 1
            $fh = new File($filePath);
36

37 1
            return $fh->read();
38
        }
39

40
        // otherwise crawl-generate a fresh document
41 1
        $swaggerOptions = [];
42 1
        if (Configure::read("Swagger.library.$id.exclude")) {
43
            $swaggerOptions = [
44 1
                'exclude' => Configure::read("Swagger.library.$id.exclude"),
45
            ];
46
        }
47 1
        if (Configure::read('Swagger.analyser')) {
48 1
            $swaggerOptions['analyser'] = Configure::read('Swagger.analyser');
49
        }
50 1
        $swagger = \Swagger\scan(Configure::read("Swagger.library.$id.include"), $swaggerOptions);
51

52
        // set object properties required by UI to generate the BASE URL
53 1
        $swagger->host = $host;
54 1
        if (empty($swagger->basePath)) {
55 1
            $swagger->basePath = '/' . Configure::read('App.base');
56
        }
57 1
        $swagger->schemes = Configure::read('Swagger.ui.schemes');
58

59
        // write document to filesystem
60 1
        self::writeSwaggerDocumentToFile($filePath, $swagger);
61

62 1
        return $swagger;
63
    }
64

65
    /**
66
     * Write swagger document to filesystem.
67
     *
68
     * @param string $path Full path to the json document including filename
69
     * @param object $content Swagger content
70
     * @throws \Cake\Http\Exception\InternalErrorException
71
     * @return bool
72
     */
73 1
    protected static function writeSwaggerDocumentToFile($path, $content)
74
    {
75
        // we need to silence the warning so we can satisfy our Test by throwing without a warning interfering
76 1
        $fh = @fopen($path, 'w'); // phpcs:ignore
77 1
        if (!$fh || !fwrite($fh, $content->__toString())) {
78 1
            throw new InternalErrorException('Error writing Swagger json document to filesystem');
79
        }
80

81 1
        return true;
82
    }
83

84
    /**
85
     * Convenience function used by the shell to create filesystem documents
86
     * for all entries found in the library.
87
     *
88
     * @param string $host Hostname of system serving swagger documents (without protocol)
89
     * @throws \InvalidArgumentException
90
     * @return bool true if successful, false on all errors
91
     */
92 1
    public static function makeDocs($host)
93
    {
94 1
        if (!Configure::read('Swagger.library')) {
95 1
            throw new \InvalidArgumentException('Swagger configuration file does not contain a library section');
96
        }
97

98
        // make sure documents will be crawled and not read from filesystem
99 1
        Configure::write('Swagger.docs.crawl', true);
100

101
        // generate docs
102 1
        foreach (array_keys(Configure::read('Swagger.library')) as $doc) {
103 1
            self::getSwaggerDocument($doc, $host);
104
        }
105

106 1
        return true;
107
    }
108
}

Read our documentation on viewing source code .

Loading