fix JSLoader class bug

This commit is contained in:
Namhyeon Go 2018-08-27 02:18:29 +09:00 committed by GitHub
parent 356b9e758f
commit 3834dbb8b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,5 @@
<?php <?php
/* /*
JavaScript loader. Load a sequence of JavaScript files using individual JavaScript loader. Load a sequence of JavaScript files using individual
SCRIPT elements, or concatenated, with or without a wrapper function (to keep SCRIPT elements, or concatenated, with or without a wrapper function (to keep
variables out of the global scope) and with or without a SCRIPT element wrapper variables out of the global scope) and with or without a SCRIPT element wrapper
@ -13,213 +11,92 @@ This software may be used under the MIT (aka X11) license or
Simplified BSD (aka FreeBSD) license. See LICENSE. Simplified BSD (aka FreeBSD) license. See LICENSE.
*/ */
/** /**
* JavaScript loader. * JavaScript loader.
* *
* @version 0.5.0 * @version 0.5.0
*/ */
class JSLoader { class JSLoader {
/**
* @property array $cfg Config parameters.
*
* bool concat Concatenate scripts.
*
* bool function_wrapper Wrap in function to keep out of global scope.
*/
protected $cfg = array(); protected $cfg = array();
/// array Scripts to be processed.
protected $scripts = array(); protected $scripts = array();
/// string Generated output.
protected $output; protected $output;
/**
* Constructor.
*
* @param array $cfg Configuration parameters.
*
* @return void
*/
public function __construct($cfg = array()) { public function __construct($cfg = array()) {
$this->cfg = array_merge($this->get_default_cfg() , $cfg); $this->cfg = array_merge($this->get_default_cfg() , $cfg);
} }
// __construct
protected function get_default_cfg(){ protected function get_default_cfg(){
$default_cfg = array( $default_cfg = array(
'file_system_path' => "./", 'file_system_path' => "./",
'concat' => false, 'concat' => false,
'function_wrapper' => false, 'function_wrapper' => false,
'indent_string' => " ", 'indent_string' => " ",
// False for an external script or for concatenating output from multiple // False for an external script or for concatenating output from multiple
// instances. // instances.
'script_wrapper' => true, 'script_wrapper' => true,
'with_header' => true 'with_header' => true
); );
return $default_cfg; return $default_cfg;
} }
// get_default_cfg
public function parse_scripts($scripts) { public function parse_scripts($scripts) {
$scripts = explode("\n", $scripts); $scripts = explode("\n", $scripts);
foreach($scripts as $script_i => & $script) { foreach($scripts as $script_i => & $script) {
if (!strlen($script = trim($script)) || $script[0] === "#") {
if (
! strlen( $script = trim( $script ) ) ||
$script[0] === "#"
) {
unset($scripts[$script_i]); unset($scripts[$script_i]);
} }
} }
// foreach
return $scripts; return $scripts;
} }
// parse_scripts
/**
* Set $this->scripts, replacing previous value.
*
* @param string|array $scripts New scripts.
*
* @param array $cfg Configuration parameters.
*
* @return void.
*/
public function set_scripts($scripts, $cfg = array()) { public function set_scripts($scripts, $cfg = array()) {
$default_cfg = array( $default_cfg = array(
'add' => false 'add' => false
); );
$cfg = array_merge($default_cfg, $cfg); $cfg = array_merge($default_cfg, $cfg);
if (!is_array($scripts)) { if (!is_array($scripts)) {
$scripts = $this->parse_scripts($scripts); $scripts = $this->parse_scripts($scripts);
} }
// if
if (!$cfg['add']) { if (!$cfg['add']) {
$this->scripts = array(); $this->scripts = array();
} }
// if
$this->scripts = array_merge($this->scripts, $scripts); $this->scripts = array_merge($this->scripts, $scripts);
} }
// set_scripts
/**
* Add to $this->scripts.
*
* @param string|array $scripts New scripts.
*
* @return void
*/
public function add_scripts( $scripts ) {
$this->set_scripts( $scripts, array( 'add' => true ) );
public function add_scripts($scripts)
{
$this->set_scripts($scripts, array(
'add' => true
));
} }
// add_scripts
public function get_output() { public function get_output() {
$scripts = $this->scripts ? $this->scripts : array();
$scripts = $this->scripts ?: array();
if ($this->cfg['concat']) { if ($this->cfg['concat']) {
foreach($scripts as & $script) { foreach($scripts as & $script) {
$file = $script; $file = $script;
$file = rtrim($this->cfg['file_system_path'], "/") . "/{$file}"; $file = rtrim($this->cfg['file_system_path'], "/") . "/{$file}";
$script = file_get_contents($file); $script = file_get_contents($file);
} }
// foreach
$scripts = join("\n\n", $scripts); $scripts = join("\n\n", $scripts);
if ($this->cfg['function_wrapper']) { if ($this->cfg['function_wrapper']) {
// Indent script content.
$scripts = explode("\n", $scripts); $scripts = explode("\n", $scripts);
foreach($scripts as & $line) { foreach($scripts as & $line) {
if ($line !== "") { if ($line !== "") {
$line = "{$this->cfg['indent_string']}{$line}"; $line = "{$this->cfg['indent_string']}{$line}";
} }
} }
// foreach
$scripts = join("\n", $scripts); $scripts = join("\n", $scripts);
$scripts = <<<DOCHERE $scripts = <<<DOCHERE
( function () { ( function () {
@ -228,13 +105,11 @@ class JSLoader {
} )(); } )();
DOCHERE; DOCHERE;
} }
// if // if
if ($this->cfg['script_wrapper']) { if ($this->cfg['script_wrapper']) {
$scripts = <<<DOCHERE $scripts = <<<DOCHERE
<script> <script>
@ -242,46 +117,23 @@ DOCHERE;
</script> </script>
DOCHERE; DOCHERE;
} elseif ($this->cfg['with_header']) {
}
// if
elseif ( $this->cfg[ 'with_header' ] ) {
header("Content-Type: text/javascript"); header("Content-Type: text/javascript");
} }
// elseif
$scripts = array( $scripts );
$scripts = array(
$scripts
);
} }
// if
else { else {
foreach($scripts as & $script) { foreach($scripts as & $script) {
$script = <<<DOCHERE $script = <<<DOCHERE
<script src="{$script}"></script> <script src="{$script}"></script>
DOCHERE; DOCHERE;
} }
// foreach
} }
// else
$this->output = join("\n\n\n", $scripts); $this->output = join("\n\n\n", $scripts);
return $this->output; return $this->output;
} }
// get_output
} }
// JSLoader