Skip to content

Commit 2a0708c

Browse files
committed
Added configuration to requestResolvers #21
1 parent e8f94d8 commit 2a0708c

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

Embed/Request.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
class Request extends Url
88
{
9-
private static $defaultResolver = 'Embed\\RequestResolvers\\Curl';
9+
private static $resolverClass = 'Embed\\RequestResolvers\\Curl';
10+
private static $resolverConfig;
1011

1112
private $resolver;
1213
private $xmlContent;
@@ -19,7 +20,7 @@ class Request extends Url
1920
*
2021
* @param string $className
2122
*/
22-
public static function setDefaultResolver($className)
23+
public static function setDefaultResolver($className, array $config = null)
2324
{
2425
if (!class_exists($className)) {
2526
throw new \Exception("This class does not exists");
@@ -31,7 +32,22 @@ public static function setDefaultResolver($className)
3132
throw new \Exception("The resolver class must implement the Embed\\RequestResolvers\\RequestResolverInterface interface");
3233
}
3334

34-
self::$defaultResolver = $className;
35+
self::$resolverClass = $className;
36+
37+
if ($config !== null) {
38+
self::setResolverConfig($config);
39+
}
40+
}
41+
42+
43+
/**
44+
* Set the resolver configuration used for http requests
45+
*
46+
* @param array $config
47+
*/
48+
public static function setResolverConfig(array $config)
49+
{
50+
self::$resolverConfig = $config;
3551
}
3652

3753

@@ -45,7 +61,11 @@ public function setUrl($url)
4561
if ($url instanceof RequestResolvers\RequestResolverInterface) {
4662
$this->resolver = $url;
4763
} else {
48-
$this->resolver = new self::$defaultResolver($url);
64+
$this->resolver = new self::$resolverClass($url);
65+
}
66+
67+
if (self::$resolverConfig) {
68+
$this->resolver->setConfig(self::$resolverConfig);
4969
}
5070

5171
$this->parseUrl($this->resolver->getLatestUrl());

Embed/RequestResolvers/Curl.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ class Curl implements RequestResolverInterface
99
protected $content;
1010
protected $result;
1111
protected $url;
12+
protected $config = array(
13+
'user_agent' => 'Embed PHP Library',
14+
'max_redirections' => 20,
15+
'connection_timeout' => 10,
16+
'timeout' => 10,
17+
);
1218

1319

1420
/**
@@ -20,6 +26,15 @@ public function __construct($url)
2026
}
2127

2228

29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function setConfig(array $config)
33+
{
34+
$this->config = $config + $this->config;
35+
}
36+
37+
2338
/**
2439
* {@inheritdoc}
2540
*/
@@ -122,16 +137,16 @@ protected function resolve()
122137
CURLOPT_URL => $this->url,
123138
CURLOPT_RETURNTRANSFER => true,
124139
CURLOPT_FOLLOWLOCATION => true,
125-
CURLOPT_MAXREDIRS => 20,
126-
CURLOPT_CONNECTTIMEOUT => 10,
127-
CURLOPT_TIMEOUT => 10,
140+
CURLOPT_MAXREDIRS => $this->config['max_redirections'],
141+
CURLOPT_CONNECTTIMEOUT => $this->config['connection_timeout'],
142+
CURLOPT_TIMEOUT => $this->config['timeout'],
128143
CURLOPT_SSL_VERIFYPEER => false,
129144
CURLOPT_SSL_VERIFYHOST => false,
130145
CURLOPT_ENCODING => '',
131146
CURLOPT_AUTOREFERER => true,
132147
CURLOPT_COOKIEJAR => $tmpCookies,
133148
CURLOPT_COOKIEFILE => $tmpCookies,
134-
CURLOPT_USERAGENT => 'Embed PHP Library'
149+
CURLOPT_USERAGENT => $this->config['user_agent']
135150
));
136151

137152
$this->content = curl_exec($connection);

Embed/RequestResolvers/RequestResolverInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ interface RequestResolverInterface
1515
public function __construct($url);
1616

1717

18+
/**
19+
* Sets the configuration
20+
*
21+
* @param array $config
22+
*/
23+
public function setConfig(array $config);
24+
25+
1826
/**
1927
* Set a new url (and clear data of previous url)
2028
*/

0 commit comments

Comments
 (0)