-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPatternModel.php
More file actions
179 lines (149 loc) · 4.17 KB
/
Copy pathPatternModel.php
File metadata and controls
179 lines (149 loc) · 4.17 KB
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
<?php
/**
* @file PatternModel.php
*/
namespace PatternKit\Model;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\HttpFoundation\Request;
/**
* Class PatternModel
*
* @package PatternKit
*/
class PatternModel
{
/**
* The defaults for this pattern.
*
* @var $seed_data
*/
public $seed_data;
public $asset_data;
public $docs_data;
public $raw_schema;
public $schema;
public $pattern;
public $template;
/**
* Constructor.
*
* @param string $pattern
* Pattern of interest.
*/
public function __construct($pattern)
{
$this->pattern = $pattern;
}
/**
* Load the seed data for a specific pattern.
*
* @return array|mixed
*/
public function getSeedData()
{
if (!empty($this->seed_data)) {
return $this->seed_data;
}
$seed_path = get_asset_path($this->pattern, 'data');
if ($seed_path) {
$seed_file = file_get_contents(
'file://'.realpath($seed_path)
);
if (($pathinfo = pathinfo(
$seed_path
)) && isset($pathinfo['extension']) && $pathinfo['extension'] == 'yaml') {
$this->seed_data = Yaml::parse($seed_file);
} elseif (!empty($seed_file)) {
$this->seed_data = json_decode($seed_file, true);
} else {
$this->seed_data = array();
}
data_replace($this->seed_data);
} else {
$this->seed_data = array();
}
return $this->seed_data;
}
/**
* Load the docs data for a specific pattern.
*
* @return \Mni\FrontYAML\Document
*/
public function getDocsData()
{
if (!empty($this->docs_data)) {
return $this->docs_data;
}
$docs_path = get_asset_path($this->pattern, 'docs');
$docs_file = file_get_contents('file://'.realpath($docs_path));
$parser = new \Mni\FrontYAML\Parser;
$this->docs_data = $parser->parse($docs_file);
return $this->docs_data;
}
/**
* Load the docs assets for a specific pattern.
*
* @return array
*/
public function getAssetData()
{
if (!empty($this->asset_data)) {
return $this->asset_data;
}
$asset_path = get_asset_path($this->pattern, 'assets');
$asset_file = file_get_contents('file://'.realpath($asset_path));
$this->asset_data = YAML::parse($asset_file, false);
return $this->asset_data;
}
/**
* Load schema for a specific pattern.
*/
public function getSchema()
{
if (!empty($this->schema)) {
return $this->schema;
}
// Define data necessary to fetch the schema.
$retriever = new \JsonSchema\Uri\UriRetriever;
$path = get_asset_path($this->pattern, 'schemas');
// Load the schema file.
$this->schema = $retriever->retrieve('file://'.realpath($path));
// Resolve any nested schemas that need to be loaded.
$refResolver = new \JsonSchema\RefResolver($retriever);
$refResolver::$maxDepth = 9999;
$refResolver->resolve($this->schema);
return $this->schema;
}
/**
* Load the raw schema for a specific pattern.
*
* @return mixed
*/
public function getRawSchema()
{
if (!empty($this->raw_schema)) {
return $this->raw_schema;
}
$path = get_asset_path($this->pattern, 'schemas');
$this->raw_schema = json_decode(file_get_contents($path), true);
return $this->raw_schema;
}
/**
* Load template for a specific pattern.
*/
public function getTemplate()
{
if (!empty($this->template)) {
return $this->template;
}
$template_path = get_asset_path($this->pattern, 'templates');
if ($template_path) {
$this->template = file_get_contents(
'file://'.realpath($template_path)
);
}
return $this->template;
}
}