88
99class Request
1010{
11+ public $ startingUrl ;
1112 public $ url ;
1213 public $ resolver ;
1314
@@ -41,6 +42,23 @@ public function __construct(Url $url, $resolverClass = null, array $resolverConf
4142 }
4243
4344
45+ /**
46+ * Magic method to retrieve the resolver an url in lazy mode
47+ */
48+ public function __get ($ name )
49+ {
50+ switch ($ name ) {
51+ case 'url ' :
52+ return $ this ->url = new Url ($ this ->resolver ->getUrl ());
53+
54+ case 'resolver ' :
55+ $ resolverClass = $ this ->resolverClass ?: $ this ->defaultResolverClass ;
56+
57+ return $ this ->resolver = new $ resolverClass (UrlRedirect::resolve ($ this ->startingUrl ->getUrl ()), $ this ->resolverConfig );
58+ }
59+ }
60+
61+
4462 /**
4563 * Creates a new request with the same configuration than this
4664 *
@@ -82,31 +100,17 @@ public static function setResolverConfig(array $config)
82100 $ this ->resolverConfig = $ config ;
83101 }
84102
85- /**
86- * Inicialize and returns the resolver instance
87- *
88- * @return mixed
89- */
90- public function getResolver ()
91- {
92- if (!$ this ->resolver ) {
93- $ resolverClass = $ this ->resolverClass ?: $ this ->defaultResolverClass ;
94-
95- $ this ->resolver = new $ resolverClass (UrlRedirect::resolve ($ this ->url ->getUrl ()), $ this ->resolverConfig );
96- }
97-
98- return $ this ->resolver ;
99- }
100-
101103 /**
102104 * Set a new url
103105 *
104106 * @param Url $url The Url instance
105107 */
106108 public function setUrl (Url $ url )
107109 {
108- $ this ->resolver = $ this ->htmlContent = $ this ->jsonContent = $ this ->xmlContent = null ;
109- $ this ->url = $ url ;
110+ $ this ->htmlContent = $ this ->jsonContent = $ this ->xmlContent = null ;
111+ $ this ->startingUrl = $ url ;
112+
113+ unset($ this ->url , $ this ->resolver );
110114 }
111115
112116 /**
@@ -116,7 +120,7 @@ public function setUrl(Url $url)
116120 */
117121 public function getUrl ()
118122 {
119- return $ this ->getResolver ()-> getLatestUrl ();
123+ return $ this ->resolver -> getUrl ();
120124 }
121125
122126 /**
@@ -128,7 +132,7 @@ public function getUrl()
128132 */
129133 public function match ($ patterns )
130134 {
131- return Url:: urlMatches ( $ this ->getStartingUrl (), $ patterns ) || Url:: urlMatches ( $ this ->getUrl (), $ patterns );
135+ return $ this ->startingUrl -> match ( $ patterns ) || $ this ->url -> match ( $ patterns );
132136 }
133137
134138 /**
@@ -138,7 +142,7 @@ public function match($patterns)
138142 */
139143 public function getRequestInfo ()
140144 {
141- return $ this ->getResolver () ->getRequestInfo ();
145+ return $ this ->resolver ->getRequestInfo ();
142146 }
143147
144148 /**
@@ -148,7 +152,7 @@ public function getRequestInfo()
148152 */
149153 public function getStartingUrl ()
150154 {
151- return $ this ->getResolver () ->getStartingUrl ();
155+ return $ this ->resolver ->getStartingUrl ();
152156 }
153157
154158 /**
@@ -158,7 +162,7 @@ public function getStartingUrl()
158162 */
159163 public function getHttpCode ()
160164 {
161- return $ this ->getResolver () ->getHttpCode ();
165+ return $ this ->resolver ->getHttpCode ();
162166 }
163167
164168 /**
@@ -168,7 +172,7 @@ public function getHttpCode()
168172 */
169173 public function getMimeType ()
170174 {
171- return $ this ->getResolver () ->getMimeType ();
175+ return $ this ->resolver ->getMimeType ();
172176 }
173177
174178 /**
@@ -178,7 +182,7 @@ public function getMimeType()
178182 */
179183 public function getContent ()
180184 {
181- return $ this ->getResolver () ->getContent ();
185+ return $ this ->resolver ->getContent ();
182186 }
183187
184188 /**
0 commit comments