1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Installer;
13
14 use ICanBoogie\Errors;
15 use ICanBoogie\I18n;
16 use ICanBoogie\I18n\FormattedString;
17
18 use Brickrouge\Alert;
19 use Brickrouge\Button;
20
21 class Requirements implements \IteratorAggregate
22 {
23 static public function get()
24 {
25 return new static;
26 }
27
28 protected $title;
29 protected $requirements;
30
31 public function __construct($title, array $requirements)
32 {
33 $this->title = $title;
34 $this->requirements = $requirements;
35 }
36
37 public function __invoke(Errors $errors)
38 {
39 foreach ($this->requirements as $requirement)
40 {
41 $requirement($errors);
42 }
43
44 return !$errors->count();
45 }
46
47 public function getIterator()
48 {
49 return new \ArrayIterator($this->requirements);
50 }
51
52 public function render()
53 {
54 $html = '';
55 $errors = new Errors();
56
57 foreach ($this->requirements as $id => $requirement)
58 {
59 $errors->clear();
60 $requirement($errors);
61
62 if (!$errors->count())
63 {
64 continue;
65 }
66
67 $html .= $requirement->render($errors);
68 }
69
70 if (!$html)
71 {
72 return;
73 }
74
75 $action = new Button("Check again", array('class' => 'btn-primary'));
76
77 return <<<EOT
78 <div class="requirements">
79 <h2>{$this->title}</h2>
80 $html
81 <p>{$action}</p>
82 </div>
83 EOT;
84 }
85 }
86
87 class WelcomeRequirements extends Requirements
88 {
89 public function __construct()
90 {
91 parent::__construct
92 (
93 I18n\t("Before we can continue, you need to check the following things:"), array
94 (
95 'repository' => new RepositoryRequirement,
96 'user_config' => new UserConfigRequirement,
97 'pdo_drivers' => new PDODriversRequirement
98 )
99 );
100 }
101 }
102
103 class InstallRequirements extends Requirements
104 {
105 static public function get()
106 {
107 return new static
108 (
109 I18n\t("Before we can continue, you need to check the following things:"), array
110 (
111 'core_config' => new CoreConfigRequirement
112 )
113 );
114 }
115 }
116
117 118 119
120 abstract class Requirement
121 {
122 public $title;
123 public $description;
124
125 abstract public function __invoke(Errors $errors);
126
127 128 129 130 131 132 133
134 public function render(Errors $errors)
135 {
136 $alert = new Alert($errors, array(Alert::UNDISSMISABLE => true));
137
138 return <<<EOT
139 <div class="requirement">
140 <h3 class="requirement-title">{$this->title}</h3>
141 <div class="requirement-description">{$this->description}</div>
142 {$alert}
143 </div>
144 EOT;
145 }
146 }