Skip to content
This repository has been archived by the owner on Feb 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7 from halfpastfouram/dev-master
Browse files Browse the repository at this point in the history
Further optimisations
  • Loading branch information
halfpastfouram authored Sep 6, 2018
2 parents 91f1fb2 + 6463d85 commit 6e112ce
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 48 deletions.
68 changes: 46 additions & 22 deletions src/View/Helper/HeadLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class HeadLink extends \Zend\View\Helper\HeadLink
*/
private $options = [];

/**
* @var string
*/
private $publicDir = '';

/**
* @var string
*/
private $cacheDir = '';

/**
* @var string
*/
Expand All @@ -34,8 +44,10 @@ class HeadLink extends \Zend\View\Helper\HeadLink
*/
public function __construct(array $config, string $baseUrl)
{
$this->options = $config;
$this->baseUrl = $baseUrl;
$this->options = $config;
$this->publicDir = $this->options['directories']['public'];
$this->cacheDir = $this->options['directories']['cache'];
$this->baseUrl = $baseUrl;

parent::__construct();
}
Expand All @@ -53,12 +65,9 @@ public function toString($indent = null): string
}

$cacheItems = [];
$publicDir = $this->options['directories']['public'];
$cacheDir = $this->options['directories']['cache'];

// Process all items. The items that don't require any changes will be returned in $items. The items that will
// be cached will be returned in $cacheItems.
$items = $this->processItems($publicDir, $cacheItems);
$items = $this->processItems($cacheItems);

$indent = (null !== $indent)
? $this->getWhitespace($indent)
Expand All @@ -69,27 +78,26 @@ public function toString($indent = null): string

// Create a minified file containing all cache items. Return the name of the minified file as the last item in
// returned in $items.
$links = $this->minifyFile($minifiedFile, $cacheDir, $cacheItems, $items)
$links = $this->minifyFile($minifiedFile, $cacheItems, $items)
// Generate the links
->generateLinks($items);

return $indent . implode($this->escape($this->getSeparator()) . $indent, $links);
}

/**
* @param string $publicDir
* @param array $cacheItems
* @param array $cacheItems
*
* @return array
*/
private function processItems($publicDir, array &$cacheItems)
private function processItems(array &$cacheItems): array
{
$items = [];
foreach ($this as $index => $item) {
if (! $item->href || $item->type != 'text/css') {
continue;
}
$localUri = str_replace($this->baseUrl, '', preg_replace('/\?.*/', '', $publicDir . $item->href));
$localUri = str_replace($this->baseUrl, '', preg_replace('/\?.*/', '', $this->publicDir . $item->href));
$remoteUri = $item->href;
$handle = curl_init($remoteUri);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
Expand All @@ -111,27 +119,43 @@ private function processItems($publicDir, array &$cacheItems)
}

/**
* @param string $minifiedFile
* @param string $cacheDir
* @param string $minifiedFileName
*
* @return string
*/
private function generateMinifiedFilePath($minifiedFileName): string
{
$minifiedFilePath = $this->cacheDir . $minifiedFileName;
if (strpos($minifiedFilePath, $this->publicDir) === 0) {
$minifiedFilePath = substr($minifiedFilePath, strlen($this->publicDir)) ?: $minifiedFilePath;
}

return $this->baseUrl . $minifiedFilePath;
}

/**
* @param string $minifiedFileName
* @param array $cacheItems
* @param array $items
*
* @return $this
*/
private function minifyFile($minifiedFile, $cacheDir, array $cacheItems, array &$items)
private function minifyFile(string $minifiedFileName, array $cacheItems, array &$items): HeadLink
{
if (! is_file($cacheDir . $minifiedFile) && ! empty($cacheItems)) {
$minifier = new Minify\CSS();
array_map(function ($uri) use ($minifier) {
$minifier->add($uri);
}, $cacheItems);
$minifier->minify($cacheDir . $minifiedFile);
if (! empty($cacheItems)) {
if (! is_file($this->cacheDir . $minifiedFileName)) {
$minifier = new Minify\CSS();
array_map(function ($uri) use ($minifier) {
$minifier->add($uri);
}, $cacheItems);
$minifier->minify($this->cacheDir . $minifiedFileName);
}

// Add the minified file tot the list of items.
$items[] = $this->createData([
'type' => 'text/css',
'rel' => 'stylesheet',
'href' => $cacheDir . $minifiedFile,
'href' => $this->generateMinifiedFilePath($minifiedFileName),
'conditionalStylesheet' => false,
]);
}
Expand All @@ -144,7 +168,7 @@ private function minifyFile($minifiedFile, $cacheDir, array $cacheItems, array &
*
* @return array
*/
private function generateLinks(array $items)
private function generateLinks(array $items): array
{
if (empty($items)) {
return [];
Expand Down
76 changes: 50 additions & 26 deletions src/View/Helper/HeadScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ class HeadScript extends \Zend\View\Helper\HeadScript
*/
private $options = [];

/**
* @var string
*/
private $publicDir = '';

/**
* @var string
*/
private $cacheDir = '';

/**
* @var string
*/
Expand All @@ -35,14 +45,16 @@ class HeadScript extends \Zend\View\Helper\HeadScript
*/
public function __construct(array $config, string $baseUrl)
{
$this->options = $config;
$this->baseUrl = $baseUrl;
$this->options = $config;
$this->publicDir = $this->options['directories']['public'];
$this->cacheDir = $this->options['directories']['cache'];
$this->baseUrl = $baseUrl;

parent::__construct();
}

/**
* @param null $indent
* @param string $indent
*
* @return string
*/
Expand All @@ -54,12 +66,9 @@ public function toString($indent = null): string
}

$cacheItems = [];
$publicDir = $this->options['directories']['public'];
$cacheDir = $this->options['directories']['cache'];

// Process all items. The items that don't require any changes will be returned in $items. The items that will
// be cached will be returned in $cacheItems.
$items = $this->processItems($publicDir, $cacheItems);
$items = $this->processItems($cacheItems);

$indent = (null !== $indent)
? $this->getWhitespace($indent)
Expand All @@ -70,20 +79,19 @@ public function toString($indent = null): string

// Create a minified file containing all cache items. Return the name of the minified file as the last item in
// returned in $items.
$scripts = $this->minifyFile($minifiedFile, $cacheDir, $cacheItems, $items)
$scripts = $this->minifyFile($minifiedFile, $cacheItems, $items)
// Generate the script tags.
->generateScripts($items, $indent);

return $indent . implode($this->escape($this->getSeparator()) . $indent, $scripts);
}

/**
* @param string $publicDir
* @param array $cacheItems
* @param array $cacheItems
*
* @return array
*/
private function processItems($publicDir, array &$cacheItems)
private function processItems(array &$cacheItems): array
{
$items = [];
foreach ($this as $index => $item) {
Expand All @@ -94,7 +102,7 @@ private function processItems($publicDir, array &$cacheItems)
$localUri = str_replace(
$this->baseUrl,
'',
preg_replace('/\?.*/', '', $publicDir . @$item->attributes['src'])
preg_replace('/\?.*/', '', $this->publicDir . @$item->attributes['src'])
);

$remoteUri = @$item->attributes['src'];
Expand All @@ -120,24 +128,41 @@ private function processItems($publicDir, array &$cacheItems)
}

/**
* @param string $minifiedFile
* @param string $cacheDir
* @param string $minifiedFileName
*
* @return string
*/
private function generateMinifiedFilePath(string $minifiedFileName): string
{
$minifiedFilePath = $this->cacheDir . $minifiedFileName;
if (strpos($minifiedFilePath, $this->publicDir) === 0) {
$minifiedFilePath = substr($minifiedFilePath, strlen($this->publicDir)) ?: $minifiedFilePath;
}

return $this->baseUrl . $minifiedFilePath;
}

/**
* @param string $minifiedFileName
* @param array $cacheItems
* @param array $items
*
* @return $this
*/
private function minifyfile($minifiedFile, $cacheDir, array $cacheItems, array &$items)
private function minifyFile(string $minifiedFileName, array $cacheItems, array &$items): HeadScript
{
if (! is_file($cacheDir . $minifiedFile) && ! empty($cacheItems)) {
$minifier = new Minify\JS();
array_map(function ($uri) use ($minifier) {
$minifier->add($uri);
}, $cacheItems);
$minifier->minify($cacheDir . $minifiedFile);
if (! empty($cacheItems)) {
if (! is_file($this->cacheDir . $minifiedFileName)) {
$minifier = new Minify\JS();
array_map(function ($uri) use ($minifier) {
$minifier->add($uri);
}, $cacheItems);
$minifier->minify($this->cacheDir . $minifiedFileName);
}

// Add the minified file to the list of items.
$items[] = $this->createData('text/javascript', [
'src' => $cacheDir . $minifiedFile,
'src' => $this->generateMinifiedFilePath($minifiedFileName),
]);
}

Expand All @@ -147,7 +172,7 @@ private function minifyfile($minifiedFile, $cacheDir, array $cacheItems, array &
/**
* @return bool
*/
private function isUseCdata()
private function isUseCdata(): bool
{
$view = $this->view;
$useCdata = $this->useCdata;
Expand All @@ -166,7 +191,7 @@ private function isUseCdata()
*
* @return array
*/
private function generateScripts(array $items, $indent)
private function generateScripts(array $items, $indent): array
{
$useCdata = $this->isUseCdata();
$escapeStart = ($useCdata) ? '//<![CDATA[' : '//<!--';
Expand All @@ -178,7 +203,6 @@ private function generateScripts(array $items, $indent)
$scripts[] = $this->itemToString($item, $indent, $escapeStart, $escapeEnd);
}

// Make sure the scripts are in the correct order.
return array_reverse($scripts, true);
return $scripts;
}
}

0 comments on commit 6e112ce

Please sign in to comment.