Skip to content

Commit

Permalink
Unit tests and general improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Mar 6, 2014
1 parent 814b5f9 commit cf35455
Show file tree
Hide file tree
Showing 21 changed files with 889 additions and 63 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
],
"require": {
"php": ">=5.4.0",
"packaged/helpers": "0.0.1",
"packaged/config": "0.0.1",
"packaged/helpers": "0.0.*",
"packaged/config": "0.0.*",
"symfony/http-kernel": "2.4.*"
},
"autoload": {
Expand Down
34 changes: 28 additions & 6 deletions src/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public static function vendorType($vendor, $package)
* @param object $callee
* @param string $forceType DirectoryMapper::MAP_*
* @param mixed $lookupParts
*
* @throws \Exception
*/
public function __construct(
$callee = null, $forceType = null, $lookupParts = null
Expand All @@ -98,7 +100,15 @@ public function __construct(

if($forceType === null)
{
$this->_mapType = $this->mapType($callee);
if(!is_object($callee))
{
throw new \Exception(
"You cannot construct an asset manager without specifying " .
"either a callee or forceType"
);
}

$this->_mapType = $this->lookupMapType($callee);
}

//If provided, use the lookup parts
Expand All @@ -108,24 +118,36 @@ public function __construct(
}
}

/**
* Retrieve the map type currently set
*
* @return string
*/
public function getMapType()
{
return $this->_mapType;
}

protected function ownFile()
{
return __FILE__;
}

/**
* Find the map type based on the provided object
*
* @param $object
*
* @return string
*/
public function mapType($object)
public function lookupMapType($object)
{
$reflection = new \ReflectionObject($object);
$filename = $reflection->getFileName();

//Find the common start to the filename of the callee and this file, which
//is known to be in the vendor directory
$prefix = Strings::commonPrefix(
$filename,
'/Websites/cubex/skeleton/vendor/packaged/dispatch/asset.php'
);
$prefix = Strings::commonPrefix($filename, $this->ownFile());

//Account for other packaged repos that may offer resources
if(ends_with($prefix, 'packaged/'))
Expand Down
44 changes: 40 additions & 4 deletions src/AssetResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

use Packaged\Dispatch\Assets\AbstractAsset;
use Packaged\Dispatch\Assets\IAsset;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class AssetResponse
{
protected static $assetMap = [
public static $assetMap = [
'js' => 'Javascript',
'json' => 'Json',
'css' => 'Css',
'swf' => 'Flash',
'pdf' => 'Pdf',
Expand All @@ -25,7 +27,7 @@ class AssetResponse
'mov' => 'Video\Quicktime',
'afm' => 'Font\Afm',
'dfont' => 'Font\Dfont',
'eot' => 'Font\eot',
'eot' => 'Font\Eot',
'otf' => 'Font\OpenType',
'pfa' => 'Font\Pfa',
'pfb' => 'Font\Pfb',
Expand All @@ -52,10 +54,44 @@ public function assetByExtension($extension)
return null;
}

public function createResponse(IAsset $asset)
public function createResponse(IAsset $asset, Request $request)
{
$response = new Response($asset->getContent(), 200);
$response = new Response();

//Set the correct content type based on the asset
$response->headers->set('Content-Type', $asset->getContentType());

//Ensure the cache varies on the language and encoding
//Domain specific content will vary on the uri itself
$response->headers->set("Vary", "Accept-Encoding,Accept-Language");

//Set the etag to the hash of the request uri, as it is in itself a hash
$response->setEtag(md5($request->getPathInfo()));
$response->setPublic();

//This resource should last for 30 days in cache
$response->setMaxAge(2592000);
$response->setSharedMaxAge(2592000);
$response->setExpires((new \DateTime())->add(new \DateInterval('P30D')));

//Set the last modified date to now
$date = new \DateTime();
$date->setTimezone(new \DateTimeZone('UTC'));
$response->headers->set(
'Last-Modified',
$date->format('D, d M Y H:i:s') . ' GMT'
);

//Check to see if the client already has the content
if($request->server->has('HTTP_IF_MODIFIED_SINCE'))
{
$response->setNotModified();
}
else
{
$response->setContent($asset->getContent());
}

return $response;
}
}
57 changes: 57 additions & 0 deletions src/Assets/AbstractAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,85 @@ abstract class AbstractAsset implements IAsset
protected $_content;
protected $_options;

/**
* Get the content for this asset
*
* @return mixed
*/
public function getContent()
{
return $this->_content;
}

/**
* Set the asset content
*
* @param $content
*
* @return $this
*/
public function setContent($content)
{
$this->_content = $content;
return $this;
}

/**
* Get the current options set
*
* @return mixed
*/
public function getOptions()
{
return $this->_options;
}

/**
* Remove all options from the asset
*
* @return $this
*/
public function clearOptions()
{
$this->_options = [];
return $this;
}

/**
* Append an options array onto the default options
*
* @param array $options
*
* @return $this
*/
public function setOptions(array $options)
{
$this->_options = array_merge($this->_options, $options);
return $this;
}

/**
* Set a single option
*
* @param $key
* @param $value
*
* @return $this
*/
public function setOption($key, $value)
{
$this->_options[$key] = $value;
return $this;
}

/**
* Retrieve an option
*
* @param $key
* @param $default
*
* @return mixed
*/
public function getOption($key, $default)
{
return isset($this->_options[$key]) ? $this->_options[$key] : $default;
Expand Down
4 changes: 2 additions & 2 deletions src/Assets/Font/PfmAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ class PfmAsset extends AbstractFontAsset
{
public function getExtension()
{
return 'pmf';
return 'pfm';
}

public function getContentType()
{
return "application/x-font-pmf";
return "application/x-font-pfm";
}
}
15 changes: 15 additions & 0 deletions src/Assets/JsonAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Packaged\Dispatch\Assets;

class JsonAsset extends AbstractAsset
{
public function getExtension()
{
return 'json';
}

public function getContentType()
{
return "application/json";
}
}
33 changes: 4 additions & 29 deletions src/DirectoryMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class DirectoryMapper

const MAP_VENDOR = 'v';
const MAP_SOURCE = 's';
const MAP_HASH = 'h';
const MAP_ALIAS = 'a';
const MAP_ASSET = 'p';

Expand Down Expand Up @@ -49,20 +48,12 @@ public function urlToPath($path)
$parts = explode('/', $path);
$partCount = count($parts);

if($partCount > 3 && $parts[0] == self::MAP_HASH)
{
//Handle Fully Hash URLs
// h/hash/b/filehash/filepath
$pathHash = 'b';
$filename = array_slice($parts, 4);
$base = $this->hashedPath($parts);
}
else if($partCount > 4 && $parts[0] == self::MAP_ALIAS)
if($partCount > 4 && $parts[0] == self::MAP_ALIAS)
{
//Handle Alias URLs
// a/alias/domain/b/filehash/filepath
$pathHash = 'b';
$filename = array_slice($parts, 4);
$filename = array_slice($parts, 5);
$base = $this->aliasPath($parts);
}
else if($partCount > 3 && $parts[0] == self::MAP_SOURCE)
Expand Down Expand Up @@ -97,22 +88,6 @@ public function urlToPath($path)
return $this->processDirHash($base, $pathHash, $filename);
}

/**
* Convert a hash cache url to a pth
*
* @param $parts
*
* @return null|string
*/
public function hashedPath($parts)
{
if(isset($this->_hashMap[$parts[1]]))
{
return $this->_hashMap[$parts[1]];
}
return null;
}

/**
* Convert a vendor provider to a path
*
Expand Down Expand Up @@ -156,7 +131,7 @@ public function assetPath()
public function aliasPath($parts)
{
$check = $parts[1];
$aliases = ValueAs::arr($this->_config['aliases']);
$aliases = ValueAs::arr($this->_config->getItem('aliases'));
if(isset($aliases[$check]))
{
return $aliases[$check];
Expand Down Expand Up @@ -252,7 +227,7 @@ public function findPathFromHash($base, $hash)
//Loop over the directories to match the path
foreach($dirs as $path)
{
$folder = [substr($path, strlen($base) + 2)];
$folder = [substr($path, strlen($base) + 1)];
if($part == $this->hashDirectoryArray($folder, strlen($part) - 2))
{
$base = $path;
Expand Down
12 changes: 8 additions & 4 deletions src/Dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ public function handle(
if($this->isDispatchRequest($request))
{
//Process the response for a dispatchable
return $this->getResponseForPath($this->getDispatchablePath($request));
return $this->getResponseForPath(
$this->getDispatchablePath($request),
$request
);
}
else
{
Expand Down Expand Up @@ -172,11 +175,12 @@ public function unsupportedResponse($extension)
/**
* Create the response for the given path
*
* @param $path
* @param $path
* @param Request $request
*
* @return Response
*/
public function getResponseForPath($path)
public function getResponseForPath($path, Request $request)
{
if(empty($path))
{
Expand Down Expand Up @@ -228,7 +232,7 @@ public function getResponseForPath($path)
$asset->setContent(file_get_contents($filePath));

//Create and return the response
return $response->createResponse($asset);
return $response->createResponse($asset, $request);
}

/**
Expand Down
Loading

0 comments on commit cf35455

Please sign in to comment.