Skip to content

Commit

Permalink
Standards updates
Browse files Browse the repository at this point in the history
  • Loading branch information
billtomczak committed Feb 4, 2022
1 parent 341c74e commit b5094ff
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 99 deletions.
20 changes: 12 additions & 8 deletions src/tasks/MergeFilesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ class MergeFilesTask extends Task
* Set the base path
*
* @param string $path
*
* @return void
*/
public function setBasePath($path)
public function setBasePath(string $path)
{
$this->basePath = $path;
}
Expand All @@ -60,8 +62,10 @@ public function setBasePath($path)
* Set the pattern to locate the file pair
*
* @param string $pattern The pattern
*
* @return void
*/
public function setPattern($pattern)
public function setPattern(string $pattern)
{
$this->pattern = $pattern;
}
Expand All @@ -70,22 +74,22 @@ public function setPattern($pattern)
* Set the string to replace the pattern
*
* @param string $replace The string
*
* @return void
*/
public function setReplace($replace = '')
public function setReplace(string $replace = '')
{
$this->replace = $replace;
}

/**
* The method that runs the task
*
* @return void
* @inheritDoc
*/
public function main()
{
// Locate files on the base path that match the pattern
$files = scandir($this->basePath);
$toMerge = array();
$files = scandir($this->basePath);
$toMerge = [];

foreach ($files as $file) {
if (substr_count($file, $this->pattern)) {
Expand Down
168 changes: 77 additions & 91 deletions src/tasks/MergeMinifyTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,161 +22,147 @@
*/

require_once 'phing/Task.php';
require_once 'TraitShack.php';

class MergeMinifyTask extends Task
{
use TraitShack;

/**
* XML Manifest file
*
* @var string
*/
protected $manifest;
protected $manifest = null;

/**
* Base path, usually the path to the src folder.
*
* @var string
*/
protected $basePath;
protected $basePath = null;

/**
* Suffix for minified files
*
* @var string
*/
protected $suffix = '.min';

/**
* The setter for the attribute "manifest". It should point to
* the composer.json file
*
* @param string $path The path for the manifest xml file
* @param string $path
*
* @return void
* @throws Exception
*/
public function setManifest($path)
public function setManifest(string $path)
{
if (empty($path) || !file_exists(realpath($path))) {
throw new Exception("Invalid XML file path");
$this->throwError('Invalid XML file path');
}

$this->manifest = $path;
}

/**
* The setter for the attribute "basePath". It should point to
* the composer.json file
*
* @param string $path The path for the basePath xml file
* @param string $path
*
* @return void
* @throws Exception
*/
public function setBasePath($path)
public function setBasePath(string $path)
{
if (empty($path) || !file_exists(realpath($path))) {
throw new Exception("Invalid base path");
if (empty($path) || is_dir(realpath($path)) == false) {
$this->throwError('Invalid base path');
}

$this->basePath = $path;
}

/**
* The setter for the attribute "suffix".
*
* @param string $suffix
*
* @return void
*/
public function setSuffix($suffix)
public function setSuffix(string $suffix)
{
$this->suffix = $suffix;
}

/**
* The init method
*
* @return void
*/
public function init()
{
}

/**
* The method that runs the task
*
* @return void
* @throws Exception
* @inheritDoc
*/
public function main()
{
$xml = simplexml_load_file($this->manifest);
try {
$xml = simplexml_load_file($this->manifest);

// Single files
if (!empty($xml->alledia->minify->script)) {
foreach ($xml->alledia->minify->script as $script) {
$this->minify($script);
// Single files
if ($xml->alledia->minify->script) {
foreach ($xml->alledia->minify->script as $script) {
$this->minify($script);
}
}
}

// Scripts bundle
if (!empty($xml->alledia->minify->scripts)) {
/** @var AppendTask $append */
$append = $this->project->createTask("append");
$append->setOwningTarget($this->getOwningTarget());
$append->setTaskName($this->getTaskName());
$append->setLocation($this->getLocation());
$append->init();

foreach ($xml->alledia->minify->scripts as $bundle) {
if (!empty($bundle->script)) {
$destination = $bundle['destination'];

// Remove destination, if exists
$fullPath = $this->basePath . '/' . $destination;
if (file_exists($fullPath)) {
unlink($fullPath);
}

// Merge files
foreach ($bundle->script as $script) {
$append->setDestFile(new PhingFile($fullPath));
$append->setOverwrite(false);
$append->setFixlastline(true);
$append->setEol('unix');
$append->setFile(new PhingFile($this->basePath . '/' . $script));
$append->main();
// Scripts bundle
if ($xml->alledia->minify->scripts) {
/** @var AppendTask $append */
$append = $this->project->createTask('append');
$append->setOwningTarget($this->getOwningTarget());
$append->setTaskName($this->getTaskName());
$append->setLocation($this->getLocation());
$append->init();

foreach ($xml->alledia->minify->scripts as $bundle) {
if (!empty($bundle->script)) {
$destination = $bundle['destination'];

// Remove destination, if exists
$fullPath = $this->basePath . '/' . $destination;
if (file_exists($fullPath)) {
unlink($fullPath);
}

// Merge files
foreach ($bundle->script as $script) {
$append->setDestFile(new PhingFile($fullPath));
$append->setOverwrite(false);
$append->setFixLastLine(true);
$append->setEol('unix');
$append->setFile(new PhingFile($this->basePath . '/' . $script));
$append->main();
}

$this->minify($destination);
}

$this->minify($destination);
}
}

} catch (Throwable $error) {
$this->throwError($error->getMessage());
}
}

/**
* Minify a file
*
* @param string $file
*
* @return void
* @throws Exception
*/
protected function minify($file)
protected function minify(string $file)
{
/** @var JsMinTask $minify */
$minify = $this->project->createTask('jsMin');
$minify->init();
$minify->setTargetDir($this->basePath);
$minify->setFailonerror(true);
$minify->setSuffix($this->suffix);

$fileset = new Fileset();
$fileset->setDir($this->basePath);
$fileset->setIncludes($file);

$minify->addFileSet($fileset);
$minify->main();
try {
/** @var JsMinTask $minify */
$minify = $this->project->createTask('jsMin');
if ($minify->init()) {
$minify->setTargetDir($this->basePath);
$minify->setFailonerror(true);
$minify->setSuffix($this->suffix);

$fileset = new Fileset();
$fileset->setDir($this->basePath);
$fileset->setIncludes($file);

$minify->addFileSet($fileset);
$minify->main();

} else {
$this->throwError('Unable to initialize JShrink');
}

} catch (Throwable $error) {
$this->throwError($error->getMessage());
}
}
}

0 comments on commit b5094ff

Please sign in to comment.