8000 renamed RequestBag to ParameterBag, added HeaderBag, changed the Resp… · Arief57/symfony@c34da5d · GitHub
[go: up one dir, main page]

Skip to content

Commit c34da5d

Browse files
committed
renamed RequestBag to ParameterBag, added HeaderBag, changed the Response to use the new HeaderBag, added a class to manage the Cache-Control header
1 parent b3a6c6f commit c34da5d

File tree

14 files changed

+598
-221
lines changed

14 files changed

+598
-221
lines changed
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
<?php
2+
3+
namespace Symfony\Components\RequestHandler;
4+
5+
/*
6+
* This file is part of the Symfony framework.
7+
*
8+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
/**
15+
* CacheControl is a wrapper for the Cache-Control HTTP header.
16+
*
17+
* This class knows about allowed attributes
18+
* (and those that only apply to requests or responses).
19+
*
20+
* @package Symfony
21+
* @subpackage Components_RequestHandler
22+
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
23+
*/
24+
class CacheControl
25+
{
26+
protected $bag;
27+
protected $attributes;
28+
protected $type;
29+
30+
/**
31+
* Constructor.
32+
*
33+
* @param Symfony\Components\RequestHandler\HeaderBag $bag A HeaderBag instance
34+
* @param string $header The value of the Cache-Control HTTP header
35+
* @param string $type The type (null, request, or response)
36+
*/
37+
public function __construct(HeaderBag $bag, $header, $type = null)
38+
{
39+
$this->bag = $bag;
40+
$this->attributes = $this->parse($header);
41+
42+
if (null !== $type && !in_array($type, array('request', 'response')))
43+
{
44+
throw new \InvalidArgumentException(sprintf('The "%s" type is not supported by the CacheControl constructor.', $type));
45+
}
46+
$this->type = $type;
47+
}
48+
49+
public function __toString()
50+
{
51+
$parts = array();
52+
ksort($this->attributes);
53+
foreach ($this->attributes as $key => $value)
54+
{
55+
if (true === $value)
56+
{
57+
$parts[] = $key;
58+
}
59+
else
60+
{
61+
if (preg_match('#[^a-zA-Z0-9._-]#', $value))
62+
{
63+
$value = '"'.$value.'"';
64+
}
65+
66+
$parts[] = "$key=$value";
67+
}
68+
}
69+
70+
return implode(',', $parts);
71+
}
72+
73+
public function getMaxStale()
74+
{
75+
$this->checkAttribute('max-stale', 'request');
76+
77+
return array_key_exists('max-stale', $this->attributes) ? $this->attributes['max-stale'] : false;
78+
}
79+
80+
public function setMaxStale($value)
81+
{
82+
$this->checkAttribute('max-stale', 'request');
83+
84+
$this->setValue('max-stale', $value);
85+
}
86+
87+
public function getMinFresh()
88+
{
89+
$this->checkAttribute('min-fresh', 'request');
90+
91+
return array_key_exists('min-fresh', $this->attributes) ? $this->attributes['min-fresh'] : false;
92+
}
93+
94+
public function setMinFresh($value)
95+
{
96+
$this->checkAttribute('min-fresh', 'request');
97+
98+
$this->setValue('min-fresh', $value);
99+
}
100+
101+
public function isOnlyIfCached()
102+
{
103+
$this->checkAttribute('only-if-cached', 'request');
104+
105+
return array_key_exists('only-if-cached', $this->attributes);
106+
}
107+
108+
public function setOnlyIfCached($value)
109+
{
110+
$this->checkAttribute('only-if-cached', 'request');
111+
112+
$this->setValue('only-if-cached', $value, true);
113+
}
114+
115+
public function isPublic()
116+
{
117+
$this->checkAttribute('public', 'response');
118+
119+
return array_key_exists('public', $this->attributes);
120+
}
121+
122+
public function setPublic($value)
123+
{
124+
$this->checkAttribute('public', 'response');
125+
126+
$this->setValue('public', $value, true);
127+
}
128+
129+
public function isPrivate()
130+
{
131+
$this->checkAttribute('private', 'response');
132+
133+
return array_key_exists('private', $this->attributes);
134+
}
135+
136+
public function getPrivate()
137+
{
138+
$this->checkAttribute('private', 'response');
139+
140+
return array_key_exists('private', $this->attributes) ? $this->attributes['private'] : false;
141+
}
142+
143+
public function setPrivate($value)
144+
{
145+
$this->checkAttribute('private', 'response');
146+
147+
$this->setValue('private', $value, true);
148+
}
149+
150+
public function isNoCache()
151+
{
152+
return array_key_exists('no-cache', $this->attributes);
153+
}
154+
155+
public function getNoCache()
156+
{
157+
return array_key_exists('no-cache', $this->attributes) ? $this->attributes['no-cache'] : false;
158+
}
159+
160+
public function setNoCache($value)
161+
{
162+
$this->setValue('no-cache', $value, true);
163+
}
164+
165+
public function isNoStore()
166+
{
167+
return array_key_exists('no-store', $this->attributes);
168+
}
169+
170+
public function setNoStore($value)
171+
{
172+
$this->setValue('no-store', $value, true);
173+
}
174+
175+
public function isNoTransform()
176+
{
177+
return array_key_exists('no-tranform', $this->attributes);
178+
}
179+
180+
public function setNoTransform($value)
181+
{
182+
$this->setValue('no-transform', $value, true);
183+
}
184+
185+
public function getMaxAge()
186+
{
187+
return array_key_exists('max-age', $this->attributes) ? $this->attributes['max-age'] : null;
188+
}
189+
190+
public function setMaxAge($age)
191+
{
192+
$this->setValue('max-age', (integer) $age);
193+
}
194+
195+
public function getSharedMaxAge()
196+
{
197+
$this->checkAttribute('s-maxage', 'response');
198+
199+
return array_key_exists('s-maxage', $this->attributes) ? $this->attributes['s-maxage'] : null;
200+
}
201+
202+
public function setSharedMaxAge($age)
203+
{
204+
$this->checkAttribute('s-maxage', 'response');
205+
206+
$this->setValue('s-maxage', (integer) $age);
207+
}
208+
209+
public function mustRevalidate()
210+
{
211+
$this->checkAttribute('must-revalidate', 'response');
212+
213+
return array_key_exists('must-revalidate', $this->attributes);
214+
}
215+
216+
public function setMustRevalidate($value)
217+
{
218+
$this->checkAttribute('must-revalidate', 'response');
219+
220+
$this->setValue('must-revalidate', $value);
221+
}
222+
223+
public function mustProxyRevalidate()
224+
{
225+
$this->checkAttribute('proxy-revalidate', 'response');
226+
227+
return array_key_exists('proxy-revalidate', $this->attributes);
228+
}
229+
230+
public function setProxyRevalidate($value)
231+
{
232+
$this->checkAttribute('proxy-revalidate', 'response');
233+
234+
$this->setValue('proxy-revalidate', $value);
235+
}
236+
237+
/**
238+
* Parses a Cache-Control HTTP header.
239+
*
240+
* @param string $header The value of the Cache-Control HTTP header
241+
*
242+
* @param array An array representing the attribute values
243+
*/
244+
protected function parse($header)
245+
{
246+
$attributes = array();
247+
preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
248+
foreach ($matches as $match)
249+
{
250+
$attributes[strtolower($match[1])] = isset($match[2]) && $match[2] ? $match[2] : (isset($match[3]) ? $match[3] : true);
251+
}
252+
253+
return $attributes;
254+
}
255+
256+
protected function setValue($key, $value, $isBoolean = false)
257+
{
258+
if (false === $value)
259+
{
260+
unset($this->attributes[$key]);
261+
}
262+
else
263+
{
264+
$this->attributes[$key] = $isBoolean ? true : $value;
265+
}
266+
267+
$this->bag->set('Cache-Control', (string) $this);
268+
}
269+
270+
protected function checkAttribute($name, $expected)
271+
{
272+
if (null !== $this->type && $expected !== $this->type)
273+
{
274+
throw new \LogicException(sprintf("The property %s only applies to the %s Cache-Control.", $name, $expected));
275+
}
276+
}
277+
}

0 commit comments

Comments
 (0)
0