-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidation.php
107 lines (85 loc) · 2.83 KB
/
Validation.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
<?php
namespace Barkley\CreatePhar\Utilities;
use Fixes;
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Fixes.php');
class Validation
{
private string $_projectDirectory;
private Fixes $_fixes;
private bool $_performFixes;
private array $_errorList;
public function __construct($projectDirectory, $performFixes = true)
{
$this->_projectDirectory = $projectDirectory;
$this->_fixes = new Fixes($projectDirectory);
$this->_performFixes = $performFixes;
$this->_errorList = array();
}
private function _GetComposerConfig(): array
{
$content = file_get_contents($this->_projectDirectory . DIRECTORY_SEPARATOR . 'composer.json');
$ary = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
return $ary;
}
/**
* Returns an array of errors that were encountered. Validate() function needs called prior to this.
* @return array
*/
public function GetErrorMessages()
{
return $this->_errorList;
}
/**
* Performs validations, and fixes issues if applicable. Additionally, for each failed validation, an error message
* is added to the error message queue.
* @return bool
*/
public function Validate()
{
$valid = true;
$this->_errorList = array();
if (!$this->_IsValidLocalRepositorySymlink()) {
$this->_errorList[] = 'Local composer repositories require options/symlink to be set to false to properly build.';
if ($this->_performFixes) {
$this->_fixes->FixLocalRepositorySymlink();
} else {
$valid = false;
}
}
if (!$this->_HasIndexFile()) {
$this->_errorList[] = 'Project requires an index.php file in the root which calls the autoloader.';
if ($this->_performFixes) {
$this->_fixes->FixIndexFile();
} else {
$valid = false;
}
}
return $valid;
}
/**
* Checks whether local composer repository references have their symlink to false.
* @return bool
*/
private function _IsValidLocalRepositorySymlink()
{
$conf = $this->_GetComposerConfig();
if (isset($conf['repositories'])) {
foreach ($conf['repositories'] as $repo) {
if ($repo['type'] == 'path') {
if (!isset($repo['options']['symlink']) || $repo['options']['symlink'] !== false) {
return false;
}
}
}
}
return true;
}
/**
* Determines if the project has an index file in the root.
* @return bool
*/
private function _HasIndexFile()
{
return file_exists($this->_projectDirectory . DIRECTORY_SEPARATOR . 'index.php');
}
}