This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
2,121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Assets | ||
|
||
The `Assets` helper allow to add various resourses to html page, such as .css, .js, etc. | ||
|
||
## Basic Usage | ||
|
||
```php | ||
<?php | ||
// setting links in a view script: | ||
$this->assets() | ||
->add('http://com.com/foo.css') | ||
->add('/bar.css') | ||
->add('/bat.js'); | ||
|
||
// rendering the links from the layout: | ||
echo $this->assets(); | ||
?> | ||
``` | ||
|
||
Output: | ||
|
||
```html | ||
<link href="http://com.com/foo.css" type="text/css"> | ||
<link href="/bar.css" type="text/css"> | ||
<script type="application/javascript" src="/bat.js"></script> | ||
``` | ||
|
||
## Add assets to different positions on page | ||
|
||
```php | ||
<?php | ||
// setting links in a view script: | ||
$this->assets() | ||
->addHeader('/foo.css') | ||
->addFooter('/bar.css'); | ||
|
||
// rendering the links from the layout: | ||
echo '<body>'; | ||
echo '<header>'; | ||
echo $this->assets()->renderHeader(); | ||
echo '</header>'; | ||
echo '<article> some content </article>'; | ||
echo '<footer>'; | ||
echo $this->assets()->renderFooter(); | ||
echo '</footer>'; | ||
echo '</body>'; | ||
?> | ||
``` | ||
|
||
Output: | ||
|
||
```html | ||
<body> | ||
<header> | ||
<link href="/foo.css" type="text/css"> | ||
</header> | ||
<article> some content </article> | ||
<footer> | ||
<link href="/bar.css" type="text/css"> | ||
</footer> | ||
</body> | ||
``` | ||
|
||
## Using configurable assets | ||
|
||
```php | ||
<?php | ||
// section in config file: | ||
'assets_manager' => [ | ||
'assets' => [ | ||
'default' => [ | ||
'external.css' => [ | ||
'source' => 'http://com.com/foo.css', | ||
], | ||
'foo.css' => [], | ||
'several_assets' => [ | ||
'assets' => [ | ||
'external.css', | ||
'bar.css', | ||
'bat.js' => [ | ||
'attributes' => [ | ||
'charset' => 'UTF-8', | ||
], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
?> | ||
``` | ||
|
||
```php | ||
<?php | ||
// setting links in a view script: | ||
$this->assets() | ||
->add('foo.css') | ||
->add('several_assets'); | ||
|
||
// rendering the links from the layout: | ||
echo $this->assets(); | ||
?> | ||
``` | ||
|
||
Output: | ||
|
||
```html | ||
<link href="/foo.css" type="text/css"> | ||
<link href="http://com.com/foo.css" type="text/css"> | ||
<link href="/bar.css" type="text/css"> | ||
<script type="application/javascript" charset="UTF-8" src="/bat.js"></script> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\View\Assets; | ||
|
||
abstract class AbstractAsset | ||
{ | ||
const PREFIX_DELIMITER = '::'; | ||
|
||
protected $name; | ||
|
||
protected $target; | ||
|
||
protected $attributes = []; | ||
|
||
protected $filters = []; | ||
|
||
public static function normalizeName($name) | ||
{ | ||
$prefix = null; | ||
if (is_array($name)) { | ||
$prefix = $name[0]; | ||
$name = $name[1]; | ||
} | ||
|
||
$name = str_replace(self::PREFIX_DELIMITER . '/', self::PREFIX_DELIMITER, trim(str_replace('\\', '/', $name), '/')); | ||
if (!$prefix) { | ||
return $name; | ||
} | ||
return $prefix . self::PREFIX_DELIMITER . $name; | ||
} | ||
|
||
public static function factory($name, $options, $assetsManager = null) | ||
{ | ||
if (is_string($options)) { | ||
$options = ['source' => $options]; | ||
} | ||
if (isset($options['assets'])) { | ||
return new Alias($name, $options, $assetsManager); | ||
} | ||
|
||
return new Asset($name, $options); | ||
} | ||
|
||
public function __construct($name, $options) | ||
{ | ||
$this->name = self::normalizeName($name); | ||
|
||
if (isset($options['target'])) { | ||
$this->target = self::normalizeName($options['target']); | ||
} | ||
|
||
if (isset($options['attributes'])) { | ||
$this->attributes = $options['attributes']; | ||
} | ||
if (isset($options['filters'])) { | ||
$this->filters = $options['filters']; | ||
} | ||
} | ||
|
||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getTarget() | ||
{ | ||
return $this->target; | ||
} | ||
|
||
public function getAttributes($key = null) | ||
{ | ||
if ($key == null) { | ||
return $this->attributes; | ||
} | ||
return isset($this->attributes[$key]) | ||
? $this->attributes[$key] | ||
: null; | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return $this->filters; | ||
} | ||
|
||
public function hasFilters() | ||
{ | ||
return !empty($this->filters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\View\Assets; | ||
|
||
class Alias extends AbstractAsset implements \Iterator, \Countable | ||
{ | ||
/** | ||
* @var AssetsManager | ||
*/ | ||
protected $assetsManager; | ||
|
||
protected $assets = []; | ||
|
||
public function __construct($name, $options, AssetsManager $assetsManager) | ||
{ | ||
parent::__construct($name, $options); | ||
|
||
$this->assetsManager = $assetsManager; | ||
|
||
if (isset($options['assets'])) { | ||
if (is_string($options['assets'])) { | ||
$options['assets'] = [$options['assets']]; | ||
} | ||
foreach ($options['assets'] as $sourceName => $sourceOptions) { | ||
if (is_numeric($sourceName)) { | ||
$this->set($sourceOptions, []); | ||
} else { | ||
$this->set($sourceName, $sourceOptions); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return bool | ||
*/ | ||
public function has($name) | ||
{ | ||
return isset($this->assets[self::normalizeName($name)]); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return Asset|Alias | ||
*/ | ||
public function get($name) | ||
{ | ||
$name = self::normalizeName($name); | ||
if (!isset($this->assets[$name])) { | ||
return; | ||
} | ||
return $this->resolveAsset($name, $this->assets[$name]); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param array $source | ||
* @return self | ||
*/ | ||
public function set($name, $source) | ||
{ | ||
$name = self::normalizeName($name); | ||
$this->assets[$name] = $source; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param type $name | ||
* @param type $source | ||
* @return Asset | ||
*/ | ||
protected function resolveAsset($name, $source) | ||
{ | ||
if ($source instanceof AbstractAsset) { | ||
return $source; | ||
} | ||
if ($this->assetsManager->has($name)) { | ||
$source = $this->assetsManager->get($name); | ||
} else { | ||
$source = new Asset($name, $source); | ||
} | ||
$this->assets[$name] = $source; | ||
return $source; | ||
} | ||
|
||
public function current() | ||
{ | ||
return $this->resolveAsset( | ||
key($this->assets), | ||
current($this->assets) | ||
); | ||
} | ||
|
||
public function key() | ||
{ | ||
return key($this->assets); | ||
} | ||
|
||
public function next() | ||
{ | ||
$next = next($this->assets); | ||
if (!$next) { | ||
return false; | ||
} | ||
return $this->resolveAsset( | ||
key($this->assets), | ||
$next | ||
); | ||
} | ||
|
||
public function rewind() | ||
{ | ||
reset($this->assets); | ||
} | ||
|
||
public function valid() | ||
{ | ||
return current($this->assets) !== false; | ||
} | ||
|
||
public function count() | ||
{ | ||
return count($this->assets); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\View\Assets; | ||
|
||
class Asset extends AbstractAsset | ||
{ | ||
protected $prefix; | ||
protected $source; | ||
protected $isExternal; | ||
|
||
public function __construct($name, $options = []) | ||
{ | ||
parent::__construct($name, $options); | ||
|
||
if (false !== ($posName = stripos($this->name, self::PREFIX_DELIMITER))) { | ||
$this->prefix = substr($this->name, 0, $posName); | ||
} | ||
|
||
$this->source = isset($options['source']) | ||
? self::normalizeName($options['source']) | ||
: $this->name; | ||
|
||
$this->target = $this->target ?: $this->name; | ||
if (false !== ($posTarget = stripos($this->target, self::PREFIX_DELIMITER))) { | ||
$this->target = substr($this->target, $posTarget + strlen(self::PREFIX_DELIMITER)); | ||
} | ||
|
||
$this->isExternal = (stripos($this->target, 'http') === 0); | ||
} | ||
|
||
public function getPrefix() | ||
{ | ||
return $this->prefix; | ||
} | ||
|
||
public function getSource() | ||
{ | ||
return $this->source; | ||
} | ||
|
||
public function isExternal() | ||
{ | ||
return $this->isExternal; | ||
} | ||
} |
Oops, something went wrong.