11<?php
22/**
3- * Class to manipulate urls, get the content, etc
3+ * Class to execute request and return the content
44 */
55namespace Embed ;
66
7- class Request extends Url
7+ use Embed \Url ;
8+
9+ class Request
810{
9- private static $ resolverClass = 'Embed \\RequestResolvers \\Curl ' ;
10- private static $ resolverConfig ;
11+ public $ url ;
12+ public $ resolver ;
13+
14+ private $ defaultResolverClass = 'Embed \\RequestResolvers \\Curl ' ;
15+ private $ resolverClass ;
16+ private $ resolverConfig ;
1117
12- private $ resolver ;
1318 private $ xmlContent ;
1419 private $ jsonContent ;
1520 private $ htmlContent ;
1621
22+
23+ /**
24+ * Constructor. Sets the url
25+ *
26+ * @param Url $url The Url instance
27+ * @param null|string $resolverClass The resolver classname
28+ * @param null|array $resolverConfig The resolver configuration
29+ */
30+ public function __construct (Url $ url , $ resolverClass = null , array $ resolverConfig = null )
31+ {
32+ if ($ resolverClass ) {
33+ $ this ->setResolverClass ($ resolverClass );
34+ }
35+
36+ if ($ resolverConfig ) {
37+ $ this ->setResolverConfig ($ resolverConfig );
38+ }
39+
40+ $ this ->setUrl ($ url );
41+ }
42+
43+
1744 /**
18- * Set a default url resolver used for http requests
45+ * Creates a new request with the same configuration than this
46+ *
47+ * @param string $url The url string
48+ */
49+ public function createSubRequest ($ url )
50+ {
51+ return new Request (new Url ($ url ), $ this ->resolverClass , $ this ->resolverConfig );
52+ }
53+
54+
55+ /**
56+ * Set the url resolver class used for http requests
1957 *
2058 * @param string $className
2159 */
22- public static function setDefaultResolver ($ className, array $ config = null )
60+ public function setResolverClass ($ className )
2361 {
2462 if (!class_exists ($ className )) {
2563 throw new \Exception ("This class does not exists " );
@@ -31,11 +69,7 @@ public static function setDefaultResolver($className, array $config = null)
3169 throw new \Exception ("The resolver class must implement the Embed \\RequestResolvers \\RequestResolverInterface interface " );
3270 }
3371
34- self ::$ resolverClass = $ className ;
35-
36- if ($ config !== null ) {
37- self ::setResolverConfig ($ config );
38- }
72+ $ this ->resolverClass = $ className ;
3973 }
4074
4175 /**
@@ -45,28 +79,34 @@ public static function setDefaultResolver($className, array $config = null)
4579 */
4680 public static function setResolverConfig (array $ config )
4781 {
48- self :: $ resolverConfig = $ config ;
82+ $ this -> resolverConfig = $ config ;
4983 }
5084
5185 /**
52- * Set a new url
86+ * Inicialize and returns the resolver instance
5387 *
54- * @param string $url The url
88+ * @return mixed
5589 */
56- public function setUrl ( $ url )
90+ public function getResolver ( )
5791 {
58- if ($ url instanceof RequestResolvers \RequestResolverInterface) {
59- $ this ->resolver = $ url ;
60- } else {
61- $ this ->resolver = new self::$ resolverClass (UrlRedirect::resolve ($ url ));
62- }
92+ if (!$ this ->resolver ) {
93+ $ resolverClass = $ this ->resolverClass ?: $ this ->defaultResolverClass ;
6394
64- if (self ::$ resolverConfig ) {
65- $ this ->resolver ->setConfig (self ::$ resolverConfig );
95+ $ this ->resolver = new $ resolverClass (UrlRedirect::resolve ($ this ->url ->getUrl ()), $ this ->resolverConfig );
6696 }
6797
68- $ this ->parseUrl ($ this ->getUrl ());
69- $ this ->updateUrl ();
98+ return $ this ->resolver ;
99+ }
100+
101+ /**
102+ * Set a new url
103+ *
104+ * @param Url $url The Url instance
105+ */
106+ public function setUrl (Url $ url )
107+ {
108+ $ this ->resolver = $ this ->htmlContent = $ this ->jsonContent = $ this ->xmlContent = null ;
109+ $ this ->url = $ url ;
70110 }
71111
72112 /**
@@ -76,7 +116,7 @@ public function setUrl($url)
76116 */
77117 public function getUrl ()
78118 {
79- return $ this ->resolver ->getLatestUrl ();
119+ return $ this ->getResolver () ->getLatestUrl ();
80120 }
81121
82122 /**
@@ -98,7 +138,7 @@ public function match($patterns)
98138 */
99139 public function getRequestInfo ()
100140 {
101- return $ this ->resolver ->getRequestInfo ();
141+ return $ this ->getResolver () ->getRequestInfo ();
102142 }
103143
104144 /**
@@ -108,7 +148,7 @@ public function getRequestInfo()
108148 */
109149 public function getStartingUrl ()
110150 {
111- return $ this ->resolver ->getStartingUrl ();
151+ return $ this ->getResolver () ->getStartingUrl ();
112152 }
113153
114154 /**
@@ -118,7 +158,7 @@ public function getStartingUrl()
118158 */
119159 public function getHttpCode ()
120160 {
121- return $ this ->resolver ->getHttpCode ();
161+ return $ this ->getResolver () ->getHttpCode ();
122162 }
123163
124164 /**
@@ -128,7 +168,7 @@ public function getHttpCode()
128168 */
129169 public function getMimeType ()
130170 {
131- return $ this ->resolver ->getMimeType ();
171+ return $ this ->getResolver () ->getMimeType ();
132172 }
133173
134174 /**
@@ -138,7 +178,7 @@ public function getMimeType()
138178 */
139179 public function getContent ()
140180 {
141- return $ this ->resolver ->getContent ();
181+ return $ this ->getResolver () ->getContent ();
142182 }
143183
144184 /**
@@ -233,55 +273,4 @@ public function isValid()
233273
234274 return true ;
235275 }
236-
237- /**
238- * {@inheritdoc}
239- */
240- public function setScheme ($ scheme )
241- {
242- parent ::setScheme ($ scheme );
243-
244- $ this ->updateUrl ();
245- }
246-
247- /**
248- * {@inheritdoc}
249- */
250- public function setHost ($ host )
251- {
252- parent ::setHost ($ host );
253-
254- $ this ->updateUrl ();
255- }
256-
257- /**
258- * {@inheritdoc}
259- */
260- public function setPath ($ path )
261- {
262- parent ::setPath ($ path );
263-
264- $ this ->updateUrl ();
265- }
266-
267- /**
268- * {@inheritdoc}
269- */
270- public function setParameter ($ name , $ value = null )
271- {
272- parent ::setParameter ($ name , $ value );
273-
274- $ this ->updateUrl ();
275- }
276-
277- /**
278- * Private function to update the url in the resolver and clear cache after any change (scheme, host, parameters, etc)
279- */
280- private function updateUrl ()
281- {
282- $ url = $ this ->buildUrl ();
283-
284- $ this ->resolver ->setUrl ($ url );
285- $ this ->htmlContent = $ this ->jsonContent = $ this ->xmlContent = null ;
286- }
287276}
0 commit comments