Skip to content

Commit 447f50c

Browse files
committed
fixed #31
1 parent d5bd79b commit 447f50c

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

Embed/Request.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ public function setUrl($url)
6868
$this->resolver->setConfig(self::$resolverConfig);
6969
}
7070

71-
$this->parseUrl($this->resolver->getLatestUrl());
72-
71+
$this->parseUrl($url);
7372
$this->updateUrl();
7473
}
7574

@@ -85,6 +84,19 @@ public function getUrl()
8584
}
8685

8786

87+
/**
88+
* Check if the url match with a specific pattern. The patterns only accepts * and ?
89+
*
90+
* @param string|array $patterns The pattern or an array with various patterns
91+
*
92+
* @return boolean True if the url match, false if not
93+
*/
94+
public function match($patterns)
95+
{
96+
return static::urlMatches($this->getStartingUrl(), $patterns) || static::urlMatches($this->getUrl(), $patterns);
97+
}
98+
99+
88100
/**
89101
* Return the http request info (for debug purposes)
90102
*
@@ -289,9 +301,7 @@ private function updateUrl()
289301
{
290302
$url = $this->buildUrl();
291303

292-
if (($this->resolver->getStartingUrl() !== $url) && ($this->resolver->getLatestUrl() !== $url)) {
293-
$this->resolver->setUrl($url);
294-
$this->htmlContent = $this->jsonContent = $this->xmlContent = null;
295-
}
304+
$this->resolver->setUrl($url);
305+
$this->htmlContent = $this->jsonContent = $this->xmlContent = null;
296306
}
297307
}

Embed/Url.php

+26-15
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,7 @@ public function getUrl()
5151
*/
5252
public function match($patterns)
5353
{
54-
$url = $this->getUrl();
55-
56-
if (!is_array($patterns)) {
57-
$patterns = array($patterns);
58-
}
59-
60-
foreach ($patterns as $pattern) {
61-
$pattern = str_replace(array('\\*', '\\?'), array('.+', '?'), preg_quote($pattern, '|'));
62-
63-
if (preg_match('|^'.$pattern.'$|i', $url)) {
64-
return true;
65-
}
66-
}
67-
68-
return false;
54+
return static::urlMatches($this->getUrl(), $patterns);
6955
}
7056

7157

@@ -378,4 +364,29 @@ public function getAbsolute($url)
378364

379365
return $this->getScheme().'://'.$this->getHost().$this->getPath().$url;
380366
}
367+
368+
369+
/**
370+
* Check if the url match with a specific pattern. The patterns only accepts * and ?
371+
*
372+
* @param string|array $patterns The pattern or an array with various patterns
373+
*
374+
* @return boolean True if the url match, false if not
375+
*/
376+
protected static function urlMatches($url, $patterns)
377+
{
378+
if (!is_array($patterns)) {
379+
$patterns = array($patterns);
380+
}
381+
382+
foreach ($patterns as $pattern) {
383+
$pattern = str_replace(array('\\*', '\\?'), array('.+', '?'), preg_quote($pattern, '|'));
384+
385+
if (preg_match('|^'.$pattern.'$|i', $url)) {
386+
return true;
387+
}
388+
}
389+
390+
return false;
391+
}
381392
}

Embed/UrlRedirect.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class UrlRedirect
88
{
99
protected static $urls = array(
1010
'google' => 'https?://www.google.com/url*',
11-
'googleTranslator' => 'https?://translate.google.com/translate*'
11+
'googleTranslator' => 'https?://translate.google.com/translate*',
12+
'hashBang' => '*#!*'
1213
);
1314

1415

@@ -57,4 +58,17 @@ protected static function googleTranslator(Url $url)
5758
$url->setUrl($urlString);
5859
}
5960
}
61+
62+
63+
/**
64+
* Resolve an url with hashbang
65+
*
66+
* @param Url $url
67+
*/
68+
protected static function hashBang(Url $url)
69+
{
70+
if (($path = preg_replace('|^(/?!)|', '', $url->getFragment()))) {
71+
$url->setPath($url->getPath().$path);
72+
}
73+
}
6074
}

0 commit comments

Comments
 (0)