Skip to content
This repository was archived by the owner on Nov 30, 2019. It is now read-only.

Commit bd2bb60

Browse files
committed
v1.0
1 parent 5b6e18d commit bd2bb60

11 files changed

+2401
-0
lines changed

Conf.php

+711
Large diffs are not rendered by default.

Directive.php

+447
Large diffs are not rendered by default.

DirectiveCollection.php

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
namespace Nemiro\Nginx
3+
{
4+
5+
/*
6+
* Copyright © Aleksey Nemiro, 2015. All rights reserved.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/**
22+
* Represents Nginx directive collection.
23+
*
24+
* @author Aleksey Nemiro <[email protected]>
25+
* @copyright © Aleksey Nemiro, 2015. All rights reserved.
26+
*/
27+
class DirectiveCollection implements \ArrayAccess
28+
{
29+
30+
/**
31+
* The list of directives.
32+
*
33+
* @var Directive[]|DirectiveGroup[]
34+
*/
35+
public $Items;
36+
37+
function __construct()
38+
{
39+
$this->Items = array();
40+
}
41+
42+
/**
43+
* Determines whether the DirectiveCollection contains the specified directive name.
44+
*
45+
* @param \string $name The key to locate in the DirectiveCollection.
46+
* @return \bool
47+
*/
48+
public function ContainsDirective($name)
49+
{
50+
if (is_null($name) || (gettype($name) != 'string' && gettype($name) != 'integer') || (string)$name == '')
51+
{
52+
throw new \InvalidArgumentException('Name is required. The name must be a string. Value can not be null or empty.');
53+
}
54+
55+
return array_key_exists($name, $this->Items);
56+
}
57+
58+
/**
59+
* Adds a new directive or group to the collection.
60+
*
61+
* @param Directive|DirectiveGroup[] $item The directive or group to add.
62+
* @return Directive|DirectiveGroup
63+
*/
64+
public function Add($item)
65+
{
66+
if ($this->ContainsDirective($item->Name))
67+
{
68+
throw new \ErrorException(sprintf('Directive `%s` already exists.', $item->Name));
69+
}
70+
71+
$this->Items[$item->Name] = $item;
72+
73+
return end($this->Items);
74+
}
75+
76+
/**
77+
* Returns elements count.
78+
*
79+
* @return \int
80+
*/
81+
public function Count()
82+
{
83+
return $this->Items != NULL ? count($this->Items) : 0;
84+
}
85+
86+
/**
87+
* Returns the first directive of the collection.
88+
*
89+
* @return Directive|DirectiveGroup
90+
*/
91+
public function First()
92+
{
93+
reset($this->Items);
94+
return current($this->Items);
95+
}
96+
97+
/**
98+
* Returns the last directive of the collection.
99+
*
100+
* @return Directive|DirectiveGroup
101+
*/
102+
public function Last()
103+
{
104+
return end($this->Items);
105+
}
106+
107+
#region ArrayAccess Members
108+
109+
/**
110+
* Whether a offset exists
111+
* Whether or not an offset exists.
112+
*
113+
* @param \string|\int $offset An offset to check for.
114+
* @return \bool
115+
*/
116+
function offsetExists($offset)
117+
{
118+
return isset($this->Items[$offset]);
119+
}
120+
121+
/**
122+
* Offset to retrieve
123+
* Returns the value at specified offset.
124+
*
125+
* @param mixed $offset The offset to retrieve.
126+
* @return Directive|DirectiveGroup
127+
*/
128+
function offsetGet($offset)
129+
{
130+
return isset($this->Items[$offset]) ? $this->Items[$offset] : NULL;
131+
}
132+
133+
/**
134+
* Offset to set
135+
* Assigns a value to the specified offset.
136+
*
137+
* @param mixed $offset The offset to assign the value to.
138+
* @param mixed $value The value to set.
139+
* @return void
140+
*/
141+
function offsetSet($offset, $value)
142+
{
143+
$this->Items[$offset] = $value;
144+
}
145+
146+
/**
147+
* Offset to unset
148+
* Unsets an offset.
149+
*
150+
* @param mixed $offset The offset to unset.
151+
* @return void
152+
*/
153+
function offsetUnset($offset)
154+
{
155+
unset($this->Items[$offset]);
156+
}
157+
158+
#endregion
159+
160+
}
161+
162+
}

DirectiveGroup.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
namespace Nemiro\Nginx
3+
{
4+
5+
/*
6+
* Copyright © Aleksey Nemiro, 2015. All rights reserved.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/**
22+
* Represents Nginx directive group.
23+
*
24+
* @author Aleksey Nemiro <[email protected]>
25+
* @copyright © Aleksey Nemiro, 2015. All rights reserved.
26+
*/
27+
class DirectiveGroup extends Directive
28+
{
29+
30+
/**
31+
* Initializes a new instance of the class with the specified parameters.
32+
*
33+
* @param \string $groupName Name of the group.
34+
*/
35+
function __construct($groupName)
36+
{
37+
parent::__construct($groupName, NULL);
38+
}
39+
40+
/**
41+
* Adds a new directive with specified parameters to the group.
42+
*
43+
* @param \string|\string[]|Directive $value List of parameters or directive instance.
44+
* @return Directive|DirectiveGroup
45+
*/
46+
public function AddDirective($value)
47+
{
48+
if (is_null($value) || !is_object($value))
49+
{
50+
return $this->Directives->Add(new Directive($this->Directives->Count(), $value));
51+
}
52+
else if (get_class($value) == 'Nemiro\Nginx\Directive')
53+
{
54+
$d = new Directive($this->Directives->Count(), $value->Parameters, $value->Directives);
55+
return $this->Directives->Add($d);
56+
}
57+
else
58+
{
59+
throw new \ErrorException('The value of the specified type is not supported. Expected: string, string array or Directive instance.');
60+
}
61+
}
62+
63+
/**
64+
* [NOT SUPPORED FOR GROUPS!] Adds new parameter to the parameter collection of the instance.
65+
*
66+
* @param \object|\array $value The value to add.
67+
* @return void
68+
*/
69+
public function AddParameter($value)
70+
{
71+
throw new \ErrorException('Parameters not suppered for groups. It is only for single directives.');
72+
}
73+
74+
}
75+
76+
}

DirectiveType.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace Nemiro\Nginx
3+
{
4+
5+
/*
6+
* Copyright © Aleksey Nemiro, 2015. All rights reserved.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
const NGINX_DIRECTIVE_SIMPLE = 0;
22+
const NGINX_DIRECTIVE_BLOCK = 1;
23+
24+
}

0 commit comments

Comments
 (0)