Skip to content

Commit 6605f39

Browse files
committed
more fixes
1 parent ddbef74 commit 6605f39

File tree

7 files changed

+70
-91
lines changed

7 files changed

+70
-91
lines changed

src/Adapters/Adapter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function getUrlFromProviders($name)
132132

133133
foreach ($this->providers as $provider) {
134134
if (($url = $provider->$method())) {
135-
return $this->request->getAbsolute($url);
135+
return $this->request->url->getAbsolute($url);
136136
}
137137
}
138138
}
@@ -313,15 +313,15 @@ public function getProviderIcon()
313313
*/
314314
public function getProviderName()
315315
{
316-
return $this->getFromProviders('providerName') ?: $this->request->getDomain();
316+
return $this->getFromProviders('providerName') ?: $this->request->url->getDomain();
317317
}
318318

319319
/**
320320
* {@inheritDoc}
321321
*/
322322
public function getProviderUrl()
323323
{
324-
return $this->getUrlFromProviders('providerUrl') ?: ($this->request->getScheme().'://'.$this->request->getDomain(true));
324+
return $this->getUrlFromProviders('providerUrl') ?: ($this->request->url->getScheme().'://'.$this->request->url->getDomain(true));
325325
}
326326

327327
/**

src/Adapters/Webpage.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ protected function initProviders(Request $request)
3535
}
3636

3737
if ($this->providers['Html']->get('oembed')) {
38-
$request = $request->CreateSubRequest($request->getAbsolute($this->providers['Html']->get('oembed')));
39-
$request->setParameter($this->options['oembedParameters']);
38+
$request = $request->CreateSubRequest($request->url->getAbsolute($this->providers['Html']->get('oembed')));
39+
$request->url->setParameter($this->options['oembedParameters']);
4040
$this->providers['OEmbed'] = new Providers\OEmbed($request);
4141
} elseif (($oEmbed = Providers\OEmbedImplementations::create($request, $this->options['oembedParameters']))) {
4242
$this->providers['OEmbed'] = $oEmbed;
@@ -62,13 +62,13 @@ public function getImages()
6262
}
6363

6464
if (!is_array($src)) {
65-
$images[] = $this->request->getAbsolute($src);
65+
$images[] = $this->request->url->getAbsolute($src);
6666
continue;
6767
}
6868

6969
foreach ($src as $src) {
7070
if (!empty($src)) {
71-
$images[] = $this->request->getAbsolute($src);
71+
$images[] = $this->request->url->getAbsolute($src);
7272
}
7373
}
7474
}
@@ -89,16 +89,16 @@ public function getProviderIcons()
8989
if ($src) {
9090
if (is_array($src)) {
9191
foreach ($src as $src) {
92-
$icons[] = $this->request->getAbsolute($src);
92+
$icons[] = $this->request->url->getAbsolute($src);
9393
}
9494
} else {
95-
$icons[] = $this->request->getAbsolute($src);
95+
$icons[] = $this->request->url->getAbsolute($src);
9696
}
9797
}
9898
}
9999

100-
$icons[] = $this->request->getAbsolute('/favicon.ico');
101-
$icons[] = $this->request->getAbsolute('/favicon.png');
100+
$icons[] = $this->request->url->getAbsolute('/favicon.ico');
101+
$icons[] = $this->request->url->getAbsolute('/favicon.png');
102102

103103
return array_unique($icons);
104104
}

src/Providers/Html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function __construct(Request $request)
129129
$content = $html;
130130
}
131131

132-
$domain = $request->getDomain();
132+
$domain = $request->url->getDomain();
133133

134134
foreach ($content->getElementsByTagName('img') as $img) {
135135
if ($img->hasAttribute('src')) {

src/Request.php

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
class 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
/**

src/RequestResolvers/Curl.php

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ class Curl implements RequestResolverInterface
1111
protected $result;
1212
protected $url;
1313
protected $config = array(
14-
CURLOPT_RETURNTRANSFER => true,
15-
CURLOPT_FOLLOWLOCATION => true,
1614
CURLOPT_MAXREDIRS => 20,
1715
CURLOPT_CONNECTTIMEOUT => 10,
1816
CURLOPT_TIMEOUT => 10,
@@ -55,6 +53,14 @@ public function setUrl($url)
5553
$this->url = $url;
5654
}
5755

56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public function getUrl()
60+
{
61+
return $this->getResult('url');
62+
}
63+
5864
/**
5965
* {@inheritdoc}
6066
*/
@@ -83,22 +89,6 @@ public function getContent()
8389
return $this->content;
8490
}
8591

86-
/**
87-
* {@inheritdoc}
88-
*/
89-
public function getStartingUrl()
90-
{
91-
return $this->url;
92-
}
93-
94-
/**
95-
* {@inheritdoc}
96-
*/
97-
public function getLatestUrl()
98-
{
99-
return $this->getResult('url');
100-
}
101-
10292
/**
10393
* {@inheritdoc}
10494
*/
@@ -140,18 +130,20 @@ protected function resolve()
140130
$connection = curl_init();
141131

142132
curl_setopt_array($connection, array(
133+
CURLOPT_RETURNTRANSFER => false,
134+
CURLOPT_FOLLOWLOCATION => true,
143135
CURLOPT_URL => $this->url,
144136
CURLOPT_COOKIEJAR => $tmpCookies,
145137
CURLOPT_COOKIEFILE => $tmpCookies,
146138
CURLOPT_HEADERFUNCTION => array($this, 'headerCallback'),
147139
CURLOPT_WRITEFUNCTION => array($this, 'writeCallback'),
148140
) + $this->config);
149141

150-
curl_exec($connection);
142+
$result = curl_exec($connection);
151143

152-
$this->result = curl_getinfo($connection);
144+
$this->result = curl_getinfo($connection) ?: array();
153145

154-
if ($this->content === false) {
146+
if (!$result) {
155147
$this->result['error'] = curl_error($connection);
156148
$this->result['error_number'] = curl_errno($connection);
157149
}

src/RequestResolvers/RequestResolverInterface.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,12 @@ public function getMimeType();
4646
*/
4747
public function getContent();
4848

49-
/**
50-
* Return the starting url (before all possible redirects)
51-
*
52-
* @return string The starting url
53-
*/
54-
public function getStartingUrl();
55-
5649
/**
5750
* Return the final url (after all possible redirects)
5851
*
5952
* @return string The final url
6053
*/
61-
public function getLatestUrl();
54+
public function getUrl();
6255

6356
/**
6457
* Return the http request info (for debug purposes)

src/Url.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,21 @@ public function getUrl()
4747
*/
4848
public function match($patterns)
4949
{
50-
return static::urlMatches($this->getUrl(), $patterns);
50+
if (!is_array($patterns)) {
51+
$patterns = array($patterns);
52+
}
53+
54+
$url = $this->getUrl();
55+
56+
foreach ($patterns as $pattern) {
57+
$pattern = str_replace(array('\\*', '\\?'), array('.+', '?'), preg_quote($pattern, '|'));
58+
59+
if (preg_match('|^'.$pattern.'$|i', $url)) {
60+
return true;
61+
}
62+
}
63+
64+
return false;
5165
}
5266

5367
/**
@@ -342,28 +356,4 @@ public function getAbsolute($url)
342356

343357
return $this->getScheme().'://'.$this->getHost().$this->getPath().$url;
344358
}
345-
346-
/**
347-
* Check if the url match with a specific pattern. The patterns only accepts * and ?
348-
*
349-
* @param string|array $patterns The pattern or an array with various patterns
350-
*
351-
* @return boolean True if the url match, false if not
352-
*/
353-
public static function urlMatches($url, $patterns)
354-
{
355-
if (!is_array($patterns)) {
356-
$patterns = array($patterns);
357-
}
358-
359-
foreach ($patterns as $pattern) {
360-
$pattern = str_replace(array('\\*', '\\?'), array('.+', '?'), preg_quote($pattern, '|'));
361-
362-
if (preg_match('|^'.$pattern.'$|i', $url)) {
363-
return true;
364-
}
365-
}
366-
367-
return false;
368-
}
369359
}

0 commit comments

Comments
 (0)