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,214 +11,93 @@ 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 {
protected $cfg = array();
/**
* @property array $cfg Config parameters. protected $scripts = array();
*
* bool concat Concatenate scripts. protected $output;
*
* bool function_wrapper Wrap in function to keep out of global scope. public function __construct($cfg = array()) {
*/ $this->cfg = array_merge($this->get_default_cfg() , $cfg);
}
protected $cfg = array();
protected function get_default_cfg(){
$default_cfg = array(
/// array Scripts to be processed. 'file_system_path' => "./",
'concat' => false,
protected $scripts = array(); 'function_wrapper' => false,
'indent_string' => " ",
/// string Generated output. // False for an external script or for concatenating output from multiple
// instances.
protected $output;
'script_wrapper' => true,
'with_header' => true
/** );
* Constructor.
* return $default_cfg;
* @param array $cfg Configuration parameters. }
*
* @return void public function parse_scripts($scripts) {
*/ $scripts = explode("\n", $scripts);
foreach($scripts as $script_i => & $script) {
public function __construct( $cfg = array() ) { if (!strlen($script = trim($script)) || $script[0] === "#") {
unset($scripts[$script_i]);
$this->cfg = array_merge( $this->get_default_cfg(), $cfg ); }
}
}
// __construct return $scripts;
}
protected function get_default_cfg() { public function set_scripts($scripts, $cfg = array()) {
$default_cfg = array(
$default_cfg = array( 'add' => false
);
'file_system_path' => "./", $cfg = array_merge($default_cfg, $cfg);
if (!is_array($scripts)) {
'concat' => false, $scripts = $this->parse_scripts($scripts);
}
'function_wrapper' => false,
if (!$cfg['add']) {
'indent_string' => " ", $this->scripts = array();
}
// False for an external script or for concatenating output from multiple
// instances. $this->scripts = array_merge($this->scripts, $scripts);
}
'script_wrapper' => true,
public function add_scripts($scripts)
'with_header' => true {
$this->set_scripts($scripts, array(
); 'add' => true
));
}
return $default_cfg;
public function get_output() {
} $scripts = $this->scripts ? $this->scripts : array();
// get_default_cfg if ($this->cfg['concat']) {
foreach($scripts as & $script) {
$file = $script;
public function parse_scripts( $scripts ) { $file = rtrim($this->cfg['file_system_path'], "/") . "/{$file}";
$script = file_get_contents($file);
$scripts = explode( "\n", $scripts ); }
$scripts = join("\n\n", $scripts);
foreach ( $scripts as $script_i => &$script ) { if ($this->cfg['function_wrapper']) {
$scripts = explode("\n", $scripts);
if ( foreach($scripts as & $line) {
if ($line !== "") {
! strlen( $script = trim( $script ) ) || $line = "{$this->cfg['indent_string']}{$line}";
}
$script[0] === "#" }
) { $scripts = join("\n", $scripts);
$scripts = <<<DOCHERE
unset( $scripts[ $script_i ] );
}
}
// foreach
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() ) {
$default_cfg = array(
'add' => false
);
$cfg = array_merge( $default_cfg, $cfg );
if ( ! is_array( $scripts ) ) {
$scripts = $this->parse_scripts( $scripts );
}
// if
if ( ! $cfg[ 'add' ] ) {
$this->scripts = array();
}
// if
$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 ) );
}
// add_scripts
public function get_output() {
$scripts = $this->scripts ?: array();
if ( $this->cfg[ 'concat' ] ) {
foreach ( $scripts as &$script ) {
$file = $script;
$file = rtrim( $this->cfg[ 'file_system_path' ], "/" ) . "/{$file}";
$script = file_get_contents( $file );
}
// foreach
$scripts = join( "\n\n", $scripts );
if ( $this->cfg[ 'function_wrapper' ] ) {
// Indent script content.
$scripts = explode( "\n", $scripts );
foreach ( $scripts as &$line ) {
if ( $line !== "" ) {
$line = "{$this->cfg[ 'indent_string' ]}{$line}";
}
}
// foreach
$scripts = join( "\n", $scripts );
$scripts = <<<DOCHERE
( function () { ( function () {
{$scripts} {$scripts}
@ -228,60 +105,35 @@ class JSLoader {
} )(); } )();
DOCHERE; DOCHERE;
}
} // if
// if
if ($this->cfg['script_wrapper']) {
if ( $this->cfg[ 'script_wrapper' ] ) { $scripts = <<<DOCHERE
$scripts = <<<DOCHERE
<script> <script>
{$scripts} {$scripts}
</script> </script>
DOCHERE; DOCHERE;
} elseif ($this->cfg['with_header']) {
header("Content-Type: text/javascript");
}
} $scripts = array(
// if $scripts
);
}
elseif ( $this->cfg[ 'with_header' ] ) { else {
foreach($scripts as & $script) {
header( "Content-Type: text/javascript" ); $script = <<<DOCHERE
}
// elseif
$scripts = array( $scripts );
}
// if
else {
foreach ( $scripts as &$script ) {
$script = <<<DOCHERE
<script src="{$script}"></script> <script src="{$script}"></script>
DOCHERE; DOCHERE;
}
}
} $this->output = join("\n\n\n", $scripts);
// foreach return $this->output;
}
}
// else
$this->output = join( "\n\n\n", $scripts );
return $this->output;
}
// get_output
} }
// JSLoader