Permalink
Browse files

Changes:

* Removed DependencyNotMetException in favor of RuntimeException
* Varnish helper now uses a single curl handler per request.
  • Loading branch information...
1 parent 52e337d commit 068c73b34a05de65e680e984d79eed415ede5911 @jnonon jnonon committed Oct 10, 2012
Showing with 38 additions and 60 deletions.
  1. +4 −4 DependencyInjection/LiipCacheControlExtension.php
  2. +0 −15 DependencyNotMetException.php
  3. +34 −41 Helper/Varnish.php
@@ -2,15 +2,14 @@
namespace Liip\CacheControlBundle\DependencyInjection;
-use Liip\CacheControlBundle\DependencyNotMetException;
-
use Symfony\Component\Config\Definition\Processor,
Symfony\Component\Config\FileLocator,
Symfony\Component\HttpKernel\DependencyInjection\Extension,
Symfony\Component\DependencyInjection\Loader\XmlFileLoader,
Symfony\Component\DependencyInjection\ContainerBuilder,
Symfony\Component\DependencyInjection\Reference,
- Symfony\Component\DependencyInjection\DefinitionDecorator;
+ Symfony\Component\DependencyInjection\DefinitionDecorator,
+ Symfony\Component\DependencyInjection\Exception\RuntimeException;
class LiipCacheControlExtension extends Extension
{
@@ -50,7 +49,8 @@ public function load(array $configs, ContainerBuilder $container)
if (!empty($config['varnish'])) {
if (!extension_loaded('curl')) {
- throw new DependencyNotMetException('Varnish Helper requires cUrl php extension. Please install it to continue');
+ throw new RuntimeException('Varnish Helper requires cUrl php extension. Please install it to continue');
+
}
$loader->load('varnish_helper.xml');
@@ -1,15 +0,0 @@
-<?php
-namespace Liip\CacheControlBundle;
-
-/**
- * Thrown when a bundle dependency is not met.
- *
- * @author jnonon <jnonon@github.com>
- *
- */
-use Exception;
-
-class DependencyNotMetException extends Exception
-{
-
-}
View
@@ -56,99 +56,92 @@ public function __construct($domain, array $ips, $port)
if (isset($url['port'])) {
$this->domain .= ':' . $url['port'];
}
- $this->ips = $ips;
+ $this->ips = $ips;
$this->port = $port;
- $this->curlHandler = curl_init($this->domain);
- //Default Option
- curl_setopt($this->curlHandler, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($this->curlHandler, CURLOPT_HEADER, true); // Display headers
-
}
/**
* Purge this absolute path at all registered cache server
*
- * @param string $path Must be an absolute path
+ * @param string $path Must be an absolute path
+ * @param array $options Options for cUrl Request
+ *
+ * @return array An associative array with keys 'headers' and 'body' which holds a raw response from the server
+ *
* @throws \RuntimeException if connection to one of the varnish servers fails.
*/
- public function invalidatePath($path)
+ public function invalidatePath($path, $options = array())
{
- $this->setRequestOptions(array(CURLOPT_CUSTOMREQUEST => 'PURGE'));
+ //Garanteed to be a purge request
+ $options[CURLOPT_CUSTOMREQUEST] = 'PURGE';
$request = array('path' => $path);
- return $this->sendRequestToAllVarnishes($request);
+ return $this->sendRequestToAllVarnishes($request, $options);
}
/**
* Force this absolute path to be refreshed
*
* @param string $path Must be an absolute path
+ * @param array $options Options for cUrl Request
+ *
+ * @return array An associative array with keys 'headers' and 'body' which holds a raw response from the server
* @throws \RuntimeException if connection to one of the varnish servers fails.
*/
- public function refreshPath($path)
+ public function refreshPath($path, $options = array())
{
- $options = array();
$headers = array("Cache-Control: no-cache, no-store, max-age=0, must-revalidate");
$options[CURLOPT_HTTPHEADER] = $headers;
$options[CURLOPT_CUSTOMREQUEST] = 'GET';
- $this->setRequestOptions($options);
-
$request = array('path' => $path);
- return $this->sendRequestToAllVarnishes($request);
+ return $this->sendRequestToAllVarnishes($request, $options);
}
/**
* Send a request to all configured varnishes
*
* @param array $request request string
+ * @param array $options Options for request
+ *
+ * @return array An associative array with keys 'headers' and 'body' which holds a raw response from the server
* @throws \RuntimeException if connection to one of the varnish servers fails. TODO: should we be more tolerant?
*/
- protected function sendRequestToAllVarnishes($request)
+ protected function sendRequestToAllVarnishes($request, $options = array())
{
$requestResponseByIp = array();
+ $curlHandler = curl_init($this->domain);
+ //Default Options
+ curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($curlHandler, CURLOPT_HEADER, true); // Display headers
+
+ foreach($options as $option => $value) {
+
+ curl_setopt($curlHandler, (int)$option, $value);
+ }
+
foreach ($this->ips as $ip) {
- curl_setopt($this->curlHandler, CURLOPT_URL, $ip.':'.$this->port.$request['path']);
+ curl_setopt($curlHandler, CURLOPT_URL, $ip.':'.$this->port.$request['path']);
- $response = curl_exec($this->curlHandler);
+ $response = curl_exec($curlHandler);
list($header, $body) = explode("\r\n\r\n", $response, 2);
$requestResponseByIp[$ip] = array('headers' => $header, 'body' => $body);
}
- return $requestResponseByIp;
+ curl_close($curlHandler);
+ return $requestResponseByIp;
}
- /**
- * Override or modify default cUrl Options
- * @param array $options
- */
- public function setRequestOptions($options)
- {
-
- foreach($options as $option => $value) {
-
- curl_setopt($this->curlHandler, (int)$option, $value);
- }
- }
- /**
- * Desctructor
- */
- public function __destruct()
- {
- if ($this->curlHandler) {
- curl_close($this->curlHandler);
- }
- }
-}
+}

0 comments on commit 068c73b

Please sign in to comment.