Skip to content

Commit 2f2af67

Browse files
committed
Merge pull request #126 from wshafer/master
Added AliasPathStackResolver for name spacing paths to assets
2 parents 67514c4 + c5937a5 commit 2f2af67

File tree

5 files changed

+530
-12
lines changed

5 files changed

+530
-12
lines changed

config/module.config.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,18 @@
1010
'AssetManager\Resolver\PathStackResolver' => 'AssetManager\Service\PathStackResolverServiceFactory',
1111
'AssetManager\Resolver\PrioritizedPathsResolver' => 'AssetManager\Service\PrioritizedPathsResolverServiceFactory',
1212
'AssetManager\Resolver\CollectionResolver' => 'AssetManager\Service\CollectionResolverServiceFactory',
13-
'AssetManager\Resolver\ConcatResolver' => 'AssetManager\Service\ConcatResolverServiceFactory',
1413
),
15-
1614
'invokables' => array(
17-
'AssetManager\Service\MimeResolver' => 'AssetManager\Service\MimeResolver',
15+
'mime_resolver' => 'AssetManager\Service\MimeResolver',
1816
),
19-
20-
'aliases' => array(
21-
//Alias left here for BC
22-
'mime_resolver' => 'AssetManager\Service\MimeResolver',
23-
)
2417
),
2518
'asset_manager' => array(
2619
'resolvers' => array(
27-
'AssetManager\Resolver\MapResolver' => 2000,
28-
'AssetManager\Resolver\ConcatResolver' => 1750,
29-
'AssetManager\Resolver\CollectionResolver' => 1500,
30-
'AssetManager\Resolver\PrioritizedPathsResolver' => 1000,
20+
'AssetManager\Resolver\MapResolver' => 3000,
21+
'AssetManager\Resolver\ConcatResolver' => 2500,
22+
'AssetManager\Resolver\CollectionResolver' => 2000,
23+
'AssetManager\Resolver\PrioritizedPathsResolver' => 1500,
24+
'AssetManager\Resolver\AliasPathStackResolver' => 1000,
3125
'AssetManager\Resolver\PathStackResolver' => 500,
3226
),
3327
),
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
namespace AssetManager\Resolver;
4+
5+
use SplFileInfo;
6+
use Traversable;
7+
use Zend\Stdlib\SplStack;
8+
use Assetic\Asset\FileAsset;
9+
use AssetManager\Exception;
10+
use AssetManager\Service\MimeResolver;
11+
12+
/**
13+
* This resolver allows you to resolve from a stack of aliases to a path.
14+
*/
15+
class AliasPathStackResolver implements ResolverInterface, MimeResolverAwareInterface
16+
{
17+
/**
18+
* @var Array
19+
*/
20+
protected $aliases = array();
21+
22+
/**
23+
* Flag indicating whether or not LFI protection for rendering view scripts is enabled
24+
*
25+
* @var bool
26+
*/
27+
protected $lfiProtectionOn = true;
28+
29+
/**
30+
* The mime resolver.
31+
*
32+
* @var MimeResolver
33+
*/
34+
protected $mimeResolver;
35+
36+
/**
37+
* Constructor
38+
*
39+
* Populate the array stack with a list of aliases and their corresponding paths
40+
*
41+
* @param array $aliases
42+
* @throws Exception\InvalidArgumentException
43+
*/
44+
public function __construct(Array $aliases)
45+
{
46+
foreach ($aliases as $alias => $path) {
47+
$this->addAlias($alias, $path);
48+
}
49+
}
50+
51+
/**
52+
* Add a single alias to the stack
53+
*
54+
* @param string $alias
55+
* @param string $path
56+
* @throws Exception\InvalidArgumentException
57+
*/
58+
private function addAlias($alias, $path)
59+
{
60+
if (!is_string($path)) {
61+
throw new Exception\InvalidArgumentException(sprintf(
62+
'Invalid path provided; must be a string, received %s',
63+
gettype($path)
64+
));
65+
}
66+
67+
if (!is_string($alias)) {
68+
throw new Exception\InvalidArgumentException(sprintf(
69+
'Invalid alias provided; must be a string, received %s',
70+
gettype($alias)
71+
));
72+
}
73+
74+
$this->aliases[$alias] = $this->normalizePath($path);
75+
}
76+
77+
/**
78+
* Normalize a path for insertion in the stack
79+
*
80+
* @param string $path
81+
* @return string
82+
*/
83+
private function normalizePath($path)
84+
{
85+
return rtrim($path, '/\\') . DIRECTORY_SEPARATOR;
86+
}
87+
88+
/**
89+
* Set the mime resolver
90+
*
91+
* @param MimeResolver $resolver
92+
*/
93+
public function setMimeResolver(MimeResolver $resolver)
94+
{
95+
$this->mimeResolver = $resolver;
96+
}
97+
98+
/**
99+
* Get the mime resolver
100+
*
101+
* @return MimeResolver
102+
*/
103+
public function getMimeResolver()
104+
{
105+
return $this->mimeResolver;
106+
}
107+
108+
/**
109+
* Set LFI protection flag
110+
*
111+
* @param bool $flag
112+
* @return self
113+
*/
114+
public function setLfiProtection($flag)
115+
{
116+
$this->lfiProtectionOn = (bool) $flag;
117+
}
118+
119+
/**
120+
* Return status of LFI protection flag
121+
*
122+
* @return bool
123+
*/
124+
public function isLfiProtectionOn()
125+
{
126+
return $this->lfiProtectionOn;
127+
}
128+
129+
/**
130+
* {@inheritDoc}
131+
*/
132+
public function resolve($name)
133+
{
134+
if ($this->isLfiProtectionOn() && preg_match('#\.\.[\\\/]#', $name)) {
135+
return null;
136+
}
137+
138+
foreach ($this->aliases as $alias => $path) {
139+
if (strpos($name, $alias) === false) {
140+
continue;
141+
}
142+
143+
$name = str_replace($alias, '', $name);
144+
145+
$file = new SplFileInfo($path . $name);
146+
147+
if ($file->isReadable() && !$file->isDir()) {
148+
$filePath = $file->getRealPath();
149+
$mimeType = $this->getMimeResolver()->getMimeType($filePath);
150+
$asset = new FileAsset($filePath);
151+
152+
$asset->mimetype = $mimeType;
153+
154+
return $asset;
155+
}
156+
}
157+
158+
return null;
159+
}
160+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace AssetManager\Service;
4+
5+
use AssetManager\Resolver\AliasPathStackResolver;
6+
use Zend\ServiceManager\FactoryInterface;
7+
use Zend\ServiceManager\ServiceLocatorInterface;
8+
use AssetManager\Resolver\PathStackResolver;
9+
10+
class AliasPathStackResolverServiceFactory implements FactoryInterface
11+
{
12+
/**
13+
* {@inheritDoc}
14+
*
15+
* @return PathStackResolver
16+
*/
17+
public function createService(ServiceLocatorInterface $serviceLocator)
18+
{
19+
$config = $serviceLocator->get('config');
20+
$aliases = array();
21+
22+
if (isset($config['asset_manager']['resolver_configs']['aliases'])) {
23+
$aliases = $config['asset_manager']['resolver_configs']['aliases'];
24+
}
25+
26+
return new AliasPathStackResolver($aliases);
27+
}
28+
}

0 commit comments

Comments
 (0)