Skip to content
This repository has been archived by the owner on Sep 28, 2018. It is now read-only.

Commit

Permalink
Changes to how cachebusting works.
Browse files Browse the repository at this point in the history
Cache busting & template search and replacing are now seperate. You don't have to do both. We can also supply many files to be searched and replaced.
  • Loading branch information
Brad Jones committed Mar 17, 2017
1 parent 9b6a282 commit 3985d39
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 47 deletions.
128 changes: 81 additions & 47 deletions src/Tasks/BuildAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,22 @@ class BuildAsset extends Robo\Task\BaseTask implements Robo\Contract\BuilderAwar
protected $debug = false;

/**
* If a valid file path is provided, we will search and replace the file
* for the asset filename and update it with a filename that includes a
* timestamp.
* If set to true and debug is also set to false we will rename the final
* asset to include an md5 hash of it's contents. You may also use this in
* conjuction with the template option.
*
* We will obviously also save the asset with this time stamped filename
* too. This provides us with a way to bust the client side cache.
*
* **IMPORTANT: The asset filename must be unique in the template file.**
*
* **Defaults to:** ```null```
* @var bool
*/
protected $cachebust = false;

/**
* If cachebust is set to true, the assets will be renamed.
* Files in this array will be opened and searched for the old filenames,
* replacing with the new cache busted filenames.
*
* @var null|string
* @var string[]
*/
protected $template = null;
protected $template = [];

/**
* If set to true we will also output a gzipped version of the asset so that
Expand Down Expand Up @@ -113,27 +115,7 @@ public function __construct(string $destination)
*/
public function source($value): self
{
if ($value instanceof Finder)
{
$this->source = [];

foreach ($value as $fileInfo)
{
$this->source[] = $fileInfo->getRealpath();
}
}
else
{
if (!is_array($value)) $value = [$value];

if (count($value) === 0) throw new \UnexpectedValueException;

if (!is_string($value[0])) throw new \UnexpectedValueException;

$this->source = $value;
}

return $this;
$this->source = $this->normaliseSrcInput($value); return $this;
}

/**
Expand All @@ -150,12 +132,15 @@ public function debug(bool $value): self
/**
* Template setter.
*
* @param string $value
* @param string|string[]|Finder $value Can be a single path to a folder or
* file, an array of files of folders,
* or a Finder instance.
*
* @return self
*/
public function template(string $value): self
public function template($value): self
{
$this->template = $value; return $this;
$this->template = $this->normaliseSrcInput($value); return $this;
}

/**
Expand All @@ -180,6 +165,17 @@ public function autoprefix(bool $value): self
$this->autoprefix = $value; return $this;
}

/**
* Cachebust Setter.
*
* @param bool $value
* @return self
*/
public function cachebust(bool $value): self
{
$this->cachebust = $value; return $this;
}

/**
* The main run method.
*
Expand Down Expand Up @@ -229,9 +225,9 @@ public function run(): Robo\Result
}

// If a template file has been set lets update it
if ($this->template !== null && file_exists($this->template))
if ($this->cachebust === true)
{
$this->updateTemplateFile($asset_contents);
$this->bustCacheBalls($asset_contents);
}

// Now write the asset
Expand Down Expand Up @@ -300,11 +296,8 @@ protected function getSourceType(SplFileInfo $file): string
*
* @return void
*/
protected function updateTemplateFile(string $asset_contents)
protected function bustCacheBalls(string $asset_contents)
{
// Tell the world what we are doing
$this->printTaskInfo('Updating template file - <info>'.$this->template.'</info>');

// Get some details about the asset
$asset_ext = $this->destination->getExtension();
$asset_name = $this->destination->getBasename('.'.$asset_ext);
Expand All @@ -322,12 +315,18 @@ protected function updateTemplateFile(string $asset_contents)
// This is the new asset name
$replace_with = $asset_name.'.'.md5($asset_contents).'.'.$asset_ext;

// Run the search and replace
$this->collectionBuilder()
->taskReplaceInFile($this->template)
->regex($search_for)
->to($replace_with)
->run();
foreach ($this->template as $templateFile)
{
// Tell the world what we are doing
$this->printTaskInfo('Updating template file - <info>'.$templateFile.'</info>');

// Run the search and replace
$this->collectionBuilder()
->taskReplaceInFile($templateFile)
->regex($search_for)
->to($replace_with)
->run();
}

// Grab the asset base dir
$asset_base_dir = $this->destination->getPath();
Expand Down Expand Up @@ -389,4 +388,39 @@ protected function writeAsset(string $asset_contents)
}
}
}

/**
* Helper method to convert several possible inputs
* to a simple array of file paths.
*
* @param string|string[]|Finder $input Can be a single path to a folder or
* file, an array of files of folders,
* or a Finder instance.
*
* @return string[]
*/
protected function normaliseSrcInput($input): array
{
$output = [];

if ($input instanceof Finder)
{
foreach ($input as $fileInfo)
{
$output[] = $fileInfo->getRealpath();
}
}
else
{
if (!is_array($input)) $input = [$input];

if (count($input) === 0) throw new \UnexpectedValueException;

if (!is_string($input[0])) throw new \UnexpectedValueException;

$output = $input;
}

return $output;
}
}
1 change: 1 addition & 0 deletions tests/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public function testTemplate()

$this->taskBuildAsset('./tests/output/template.js')
->source('./vendor/bower/jquery/dist/jquery.js')
->cachebust(true)
->template('./tests/output/template.html')
->run();

Expand Down

0 comments on commit 3985d39

Please sign in to comment.