This repository has been archived by the owner on Feb 16, 2019. It is now read-only.
/
Sami.php
200 lines (163 loc) · 6.36 KB
/
Sami.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/*
* This file is part of the Sami utility.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Sami;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
use Pimple\Container;
use Sami\Parser\ClassTraverser;
use Sami\Parser\ClassVisitor;
use Sami\Parser\CodeParser;
use Sami\Parser\DocBlockParser;
use Sami\Parser\Filter\DefaultFilter;
use Sami\Parser\NodeVisitor;
use Sami\Parser\Parser;
use Sami\Parser\ParserContext;
use Sami\RemoteRepository\AbstractRemoteRepository;
use Sami\Renderer\Renderer;
use Sami\Renderer\ThemeSet;
use Sami\Renderer\TwigExtension;
use Sami\Store\JsonStore;
use Sami\Version\SingleVersionCollection;
use Sami\Version\Version;
class Sami extends Container
{
const VERSION = '4.0.12-DEV';
public function __construct($iterator = null, array $config = array())
{
parent::__construct();
$sc = $this;
if (null !== $iterator) {
$this['files'] = $iterator;
}
$this['_versions'] = function ($sc) {
$versions = $sc['versions'] ?? $sc['version'];
if (is_string($versions)) {
$versions = new Version($versions);
}
if ($versions instanceof Version) {
$versions = new SingleVersionCollection($versions);
}
return $versions;
};
$this['project'] = function ($sc) {
$project = new Project($sc['store'], $sc['_versions'], array(
'build_dir' => $sc['build_dir'],
'cache_dir' => $sc['cache_dir'],
'remote_repository' => $sc['remote_repository'],
'simulate_namespaces' => $sc['simulate_namespaces'],
'include_parent_data' => $sc['include_parent_data'],
'default_opened_level' => $sc['default_opened_level'],
'theme' => $sc['theme'],
'title' => $sc['title'],
'source_url' => $sc['source_url'],
'source_dir' => $sc['source_dir'],
'insert_todos' => $sc['insert_todos'],
'sort_class_properties' => $sc['sort_class_properties'],
'sort_class_methods' => $sc['sort_class_methods'],
'sort_class_constants' => $sc['sort_class_constants'],
'sort_class_traits' => $sc['sort_class_traits'],
'sort_class_interfaces' => $sc['sort_class_interfaces'],
));
$project->setRenderer($sc['renderer']);
$project->setParser($sc['parser']);
return $project;
};
$this['parser'] = function ($sc) {
return new Parser($sc['files'], $sc['store'], $sc['code_parser'], $sc['traverser']);
};
$this['indexer'] = function () {
return new Indexer();
};
$this['tree'] = function () {
return new Tree();
};
$this['parser_context'] = function ($sc) {
return new ParserContext($sc['filter'], $sc['docblock_parser'], $sc['pretty_printer']);
};
$this['docblock_parser'] = function () {
return new DocBlockParser();
};
$this['php_parser'] = function () {
return (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
};
$this['php_traverser'] = function ($sc) {
$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
$traverser->addVisitor(new NodeVisitor($sc['parser_context']));
return $traverser;
};
$this['code_parser'] = function ($sc) {
return new CodeParser($sc['parser_context'], $sc['php_parser'], $sc['php_traverser']);
};
$this['pretty_printer'] = function () {
return new PrettyPrinter();
};
$this['filter'] = function () {
return new DefaultFilter();
};
$this['store'] = function () {
return new JsonStore();
};
$this['renderer'] = function ($sc) {
return new Renderer($sc['twig'], $sc['themes'], $sc['tree'], $sc['indexer']);
};
$this['traverser'] = function ($sc) {
$visitors = array(
new ClassVisitor\InheritdocClassVisitor(),
new ClassVisitor\MethodClassVisitor(),
new ClassVisitor\PropertyClassVisitor($sc['parser_context']),
);
if ($sc['remote_repository'] instanceof AbstractRemoteRepository) {
$visitors[] = new ClassVisitor\ViewSourceClassVisitor($sc['remote_repository']);
}
return new ClassTraverser($visitors);
};
$this['themes'] = function ($sc) {
$templates = $sc['template_dirs'];
$templates[] = __DIR__.'/Resources/themes';
return new ThemeSet($templates);
};
$this['twig'] = function () {
$twig = new \Twig_Environment(new \Twig_Loader_Filesystem(array('/')), array(
'strict_variables' => true,
'debug' => true,
'auto_reload' => true,
'cache' => false,
));
$twig->addExtension(new TwigExtension());
return $twig;
};
$this['theme'] = 'default';
$this['title'] = 'API';
$this['version'] = 'master';
$this['template_dirs'] = array();
$this['build_dir'] = getcwd().'/build';
$this['cache_dir'] = getcwd().'/cache';
$this['remote_repository'] = null;
$this['source_dir'] = '';
$this['source_url'] = '';
$this['default_opened_level'] = 2;
$this['insert_todos'] = false;
$this['sort_class_properties'] = false;
$this['sort_class_methods'] = false;
$this['sort_class_constants'] = false;
$this['sort_class_traits'] = false;
$this['sort_class_interfaces'] = false;
// simulate namespaces for projects based on the PEAR naming conventions
$this['simulate_namespaces'] = false;
// include parent properties and methods on class pages
$this['include_parent_data'] = true;
foreach ($config as $key => $value) {
$this[$key] = $value;
}
}
}