forked from GoIntegro/raml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DereferencesIncludes.php
63 lines (57 loc) · 1.58 KB
/
DereferencesIncludes.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
<?php
/**
* @copyright 2014 Integ S.A.
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
* @author Javier Lorenzana <[email protected]>
*/
namespace GoIntegro\Raml;
// YAML.
use Symfony\Component\Yaml\Yaml;
/**
* A class that has this trait dereferences includes.
*
* I'm tryint to start a new naming convention in which traits are names like
* traits. What are this classes traits? Well, it's handsome, polite, and it
* dereferences includes.
*/
trait DereferencesIncludes
{
/**
* @param string $value
* @param string $fileDir
* @return mixed
*/
protected function dereferenceInclude($value, $fileDir = __DIR__)
{
$filePath = $fileDir . preg_replace('/^!include +/', '/', $value);
if (!is_readable($filePath)) {
throw new \ErrorException(
DocNavigator::ERROR_INCLUDED_FILE_PERMISSION
);
} elseif ($this->isJsonFile($filePath)) {
return $this->jsonCoder->decode($filePath, TRUE);
} elseif ($this->isYamlFile($filePath)) {
return Yaml::parse($filePath);
} else {
throw new \ErrorException(
DocNavigator::ERROR_INCLUDED_FILE_TYPE
);
}
}
/**
* @param string $path
* @return boolean
*/
protected function isJsonFile($path)
{
return 1 === preg_match('/\.json$/', $path);
}
/**
* @param string $path
* @return boolean
*/
protected function isYamlFile($path)
{
return 1 === preg_match('/\.(y|ra|ya)ml$/', $path);
}
}