Skip to content

Commit 349f3e4

Browse files
committed
Changes
1 parent 2fed197 commit 349f3e4

File tree

3 files changed

+100
-104
lines changed

3 files changed

+100
-104
lines changed

src/Adapters/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function __construct(Request $request, array $options = null)
6262
$this->initProviders($request);
6363

6464
if ($request->getUrl() !== $this->url) {
65-
$this->initProviders(new Request($this->url));
65+
$this->initProviders($request->createSubRequest($this->url));
6666
}
6767
}
6868

src/Embed.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,42 @@ class Embed
66
/**
77
* Gets the info from an url
88
*
9-
* @param string|Request $url The url or a request with the url
9+
* @param string|Request $request The url or a request with the url
1010
* @param null|array $options Options passed to the adapter
1111
*
1212
* @return false|Adapters\AdapterInterface
1313
*/
14-
public static function create($url, array $options = null)
14+
public static function create($request, array $options = null)
1515
{
16-
if (is_string($url)) {
17-
$url = new Request($url);
18-
} elseif (!($url instanceof Request)) {
16+
if (is_string($request)) {
17+
$resolverClass = isset($options['resolver']['class']) ? $options['resolver']['class'] : null;
18+
$resolverOptions = isset($options['resolver']['options']) ? $options['resolver']['options'] : null;
19+
20+
$request = new Request($request, $resolverClass, $resolverOptions);
21+
} elseif (!($request instanceof Request)) {
1922
throw new \InvalidArgumentException("Embed::create only accepts instances of Embed\\Request or strings");
2023
}
2124

22-
if (!$url->isValid()) {
25+
if (!$request->isValid()) {
2326
return false;
2427
}
2528

2629
//If is a file use File Adapter
27-
if (Adapters\File::check($url)) {
28-
return new Adapters\File($url, $options);
30+
if (Adapters\File::check($request->url)) {
31+
return new Adapters\File($request, $options);
2932
}
3033

3134
//Search the adapter using the domain
32-
$class = 'Embed\\Adapters\\'.str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $url->getDomain()))));
35+
$class = 'Embed\\Adapters\\'.str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $request->url->getDomain()))));
3336

3437
if (class_exists($class)) {
35-
if (call_user_func(array($class, 'check'), $url)) {
36-
return new $class($url, $options);
38+
if (call_user_func(array($class, 'check'), $request->url)) {
39+
return new $class($request, $options);
3740
}
3841
}
3942

40-
if (Adapters\Webpage::check($url)) {
41-
return new Adapters\Webpage($url, $options);
43+
if (Adapters\Webpage::check($request->url)) {
44+
return new Adapters\Webpage($request, $options);
4245
}
4346

4447
return false;
@@ -48,24 +51,28 @@ public static function create($url, array $options = null)
4851
* Gets the info from a source (list of urls)
4952
*
5053
* @param string|Request $url The url or a request with the source url
54+
* @param null|array $options Options passed to the adapter
5155
*
5256
* @return false|Sources\SourceInterface
5357
*/
54-
public static function createSource($url)
58+
public static function createSource($request, array $options = null)
5559
{
56-
if (is_string($url)) {
57-
$url = new Request($url);
58-
} elseif (!($url instanceof Request)) {
59-
throw new \InvalidArgumentException("Embed::create only accepts instances of Embed\\Request or strings");
60+
if (is_string($request)) {
61+
$resolverClass = isset($options['resolver']['class']) ? $options['resolver']['class'] : null;
62+
$resolverOptions = isset($options['resolver']['options']) ? $options['resolver']['options'] : null;
63+
64+
$request = new Request($request, $resolverClass, $resolverOptions);
65+
} elseif (!($request instanceof Request)) {
66+
throw new \InvalidArgumentException("Embed::createSource only accepts instances of Embed\\Request or strings");
6067
}
6168

62-
if (!$url->isValid()) {
69+
if (!$request->isValid()) {
6370
return false;
6471
}
6572

6673
//If is a xml feed (rss/atom)
67-
if (Sources\Feed::check($url)) {
68-
$sources = new Sources\Feed($url);
74+
if (Sources\Feed::check($request->url)) {
75+
$sources = new Sources\Feed($request);
6976

7077
if ($sources->isValid()) {
7178
return $sources;

src/Request.php

Lines changed: 71 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,63 @@
11
<?php
22
/**
3-
* Class to manipulate urls, get the content, etc
3+
* Class to execute request and return the content
44
*/
55
namespace 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

Comments
 (0)