Skip to content

Commit

Permalink
group names, file removal
Browse files Browse the repository at this point in the history
  • Loading branch information
Crecket committed Apr 28, 2016
1 parent d4d5cc8 commit a8046f7
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 25 deletions.
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,35 @@ Add a file to the manager

use Crecket\DependencyManager\Loader;

Loader::addJsFile('testFile.js');
Loader::addCssFile('testFile.css');
Loader::addJsFile('testFile.js'); // string
Loader::addJsFiles(array('testFile.js')); // array

Loader::addCssFile('testFile.css'); // string
Loader::addCssFile(array('testFile.css')); // array

Function accepts strings and array with file locations. Second parameter sets the group for this file

Loader::addFiles('testFile.css', 'cssGroupName');
Loader::addFiles(array('testFile.css'), 'cssGroupName2'); // different group

Remove all files for a group name

Loader::removeFiles('cssGroupName2');


In your twig template create a script source. The first parameter is optional and enables/disables minifying your code.

`<script src="/minify.php{{ getJsList(true) }}">`
<script src="/minify.php{{ getJsList(true) }}">
<link href="/minify.php<?php echo Crecket\DependencyManager\Loader::getCssLink(true); ?>" rel="stylesheet">

Or if you are using groups, use the ` getFileList($minify, $group_name)` function

<script src="/minify.php{{ getFilesLink(true, 'jsGroupName') }}">
<link href="/minify.php<?php echo Crecket\DependencyManager\Loader::getFilesLink(true, 'cssGroupName'); ?>" rel="stylesheet">


Or if you aren't using twig
Youc can also load only the files for a specific group

`<link href="/minify.php<?php echo Crecket\DependencyManager\Loader::GetCssLink(true); ?>" rel="stylesheet">`

Now create a new file named minify.php for example and add the following line.

Expand Down
77 changes: 65 additions & 12 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,76 @@ class Loader

private static $jsFiles = array();
private static $cssFiles = array();
private static $files = array();

private static $secret = false;

/**
* @param $file
* @param $files
* @param string $group
*/
public static function addJsFile($file)
public static function addFiles($files, $group = 'default')
{
self::$jsFiles[] = $file;
if (is_array($files)) {
foreach ($files as $file) {
self::$files[$group][] = $file;
}
} else {
self::$files[$group][] = $files;
}
}

/**
* @param $file
* @param string $group
*/
public static function addCssFile($file)
public static function removeFiles($group = 'default')
{
self::$cssFiles[] = $file;
self::$files[$group] = array();
}

/**
* @param $files
* @param $minify
* @param string $group
* @return string
*/
public static function addJsFiles($files)
public static function getFilesLink($minify = false, $group = 'default')
{
foreach ($files as $file) {
self::$jsFiles[] = $file;
if (empty(self::$files[$group])) {
self::$files[$group] = array();
}
// create hash for given file list and secret
$hash = hash('sha256', serialize(self::$files[$group]) . self::$secret);

// add to the link
$link = "?secret=" . $hash;

// now rehash again with the secret and store filelist in session
$_SESSION['crecket_dependency_manager'][$hash] = self::$files[$group];

// Add minify to link
if ($minify === true) {
$link .= "&minify=true";
}

return $link;

}

/**
* @param $file
*/
public static function addJsFile($file)
{
self::$jsFiles[] = $file;
}

/**
* @param $files
*/
public static function addCssFiles($files)
public static function addJsFiles($files)
{
foreach ($files as $file) {
self::$cssFiles[] = $file;
self::$jsFiles[] = $file;
}
}

Expand All @@ -69,6 +103,25 @@ public static function getJsLink($minify = false)
return $link;
}

/**
* @param $file
*/
public static function addCssFile($file)
{
self::$cssFiles[] = $file;
}

/**
* @param $files
*/
public static function addCssFiles($files)
{
foreach ($files as $file) {
self::$cssFiles[] = $file;
}
}


/**
* @param $minify
* @return string
Expand Down
7 changes: 0 additions & 7 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ final class Response
*/
private $cache;

/**
* @var bool
* Location to store new remote files
*/
private $remote_storage = false;

/**
* @var bool|array
* Contains the current response type, used to prevent js/css mixing in one file
Expand Down Expand Up @@ -125,7 +119,6 @@ public function __construct($options, $input_file_list = false)
// Check if file_data is correct
if ($this->file_data === false) {
Utilities::sendHeaders();
exit;
}

}
Expand Down
3 changes: 3 additions & 0 deletions src/TwigResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function getFunctions()
new \Twig_SimpleFunction('getCssLink', function ($minify = false) {
return Loader::getCssLink($minify);
}),
new \Twig_SimpleFunction('getFilesLink', function ($minify = false, $group = 'default') {
return Loader::getFilesLink($minify, $group);
}),
);
}

Expand Down
21 changes: 20 additions & 1 deletion tests/test.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
date_default_timezone_set('Europe/Amsterdam');

require __DIR__ . '/../vendor/autoload.php';

class testMain extends PHPUnit_Framework_TestCase
Expand All @@ -16,6 +18,19 @@ public function testLoading()
'/bower_components/jquery/dist/jquery.min.js',
'/bower_components/bootstrap/dist/js/bootstrap.min.js'
));

Crecket\DependencyManager\Loader::addFiles(array(
'/bower_components/jquery/dist/jquery.js',
'/bower_components/bootstrap/dist/js/bootstrap.js'
), 'genericjs');

Crecket\DependencyManager\Loader::addFiles(array(
'/bower_components/bootstrap-sass/assets/stylesheets/_bootstrap.scss',
'/bower_components/bootstrap/less/bootstrap.less'
), 'genericcss');

// test removal for group
Crecket\DependencyManager\Loader::removeFiles('genericcss');
}

public function testSecret()
Expand All @@ -28,16 +43,20 @@ public function testGetLinks()
// TODO create better test case for link generation
$this->assertNotEmpty(\Crecket\DependencyManager\Loader::getCssLink(true));
$this->assertNotEmpty(\Crecket\DependencyManager\Loader::getCssLink(false));

$this->assertNotEmpty(\Crecket\DependencyManager\Loader::getJsLink(true));
$this->assertNotEmpty(\Crecket\DependencyManager\Loader::getJsLink(false));

$this->assertNotEmpty(\Crecket\DependencyManager\Loader::getFilesLink(false, 'genericjs'));
$this->assertNotEmpty(\Crecket\DependencyManager\Loader::getFilesLink(true, 'genericjs'));
}

public function testResponse()
{
define('ROOT', __DIR__);

$options = array(
'Cache' => ROOT.'/cache'
'Cache' => ROOT . '/cache'
);

$jsList = array(
Expand Down

0 comments on commit a8046f7

Please sign in to comment.