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
/*
JavaScript loader. Load a sequence of JavaScript files using individual
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
@ -13,214 +11,93 @@ This software may be used under the MIT (aka X11) license or
Simplified BSD (aka FreeBSD) license. See LICENSE.
*/
/**
* JavaScript loader.
*
* @version 0.5.0
*/
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();
/// array Scripts to be processed.
protected $scripts = array();
/// string Generated output.
protected $output;
/**
* Constructor.
*
* @param array $cfg Configuration parameters.
*
* @return void
*/
public function __construct( $cfg = array() ) {
$this->cfg = array_merge( $this->get_default_cfg(), $cfg );
}
// __construct
protected function get_default_cfg() {
$default_cfg = array(
'file_system_path' => "./",
'concat' => false,
'function_wrapper' => false,
'indent_string' => " ",
// False for an external script or for concatenating output from multiple
// instances.
'script_wrapper' => true,
'with_header' => true
);
return $default_cfg;
}
// get_default_cfg
public function parse_scripts( $scripts ) {
$scripts = explode( "\n", $scripts );
foreach ( $scripts as $script_i => &$script ) {
if (
! strlen( $script = trim( $script ) ) ||
$script[0] === "#"
) {
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
protected $cfg = array();
protected $scripts = array();
protected $output;
public function __construct($cfg = array()) {
$this->cfg = array_merge($this->get_default_cfg() , $cfg);
}
protected function get_default_cfg(){
$default_cfg = array(
'file_system_path' => "./",
'concat' => false,
'function_wrapper' => false,
'indent_string' => " ",
// False for an external script or for concatenating output from multiple
// instances.
'script_wrapper' => true,
'with_header' => true
);
return $default_cfg;
}
public function parse_scripts($scripts) {
$scripts = explode("\n", $scripts);
foreach($scripts as $script_i => & $script) {
if (!strlen($script = trim($script)) || $script[0] === "#") {
unset($scripts[$script_i]);
}
}
return $scripts;
}
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 (!$cfg['add']) {
$this->scripts = array();
}
$this->scripts = array_merge($this->scripts, $scripts);
}
public function add_scripts($scripts)
{
$this->set_scripts($scripts, array(
'add' => true
));
}
public function get_output() {
$scripts = $this->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);
}
$scripts = join("\n\n", $scripts);
if ($this->cfg['function_wrapper']) {
$scripts = explode("\n", $scripts);
foreach($scripts as & $line) {
if ($line !== "") {
$line = "{$this->cfg['indent_string']}{$line}";
}
}
$scripts = join("\n", $scripts);
$scripts = <<<DOCHERE
( function () {
{$scripts}
@ -228,60 +105,35 @@ class JSLoader {
} )();
DOCHERE;
}
}
// if
// if
if ( $this->cfg[ 'script_wrapper' ] ) {
$scripts = <<<DOCHERE
if ($this->cfg['script_wrapper']) {
$scripts = <<<DOCHERE
<script>
{$scripts}
</script>
DOCHERE;
} elseif ($this->cfg['with_header']) {
header("Content-Type: text/javascript");
}
}
// if
elseif ( $this->cfg[ 'with_header' ] ) {
header( "Content-Type: text/javascript" );
}
// elseif
$scripts = array( $scripts );
}
// if
else {
foreach ( $scripts as &$script ) {
$script = <<<DOCHERE
$scripts = array(
$scripts
);
}
else {
foreach($scripts as & $script) {
$script = <<<DOCHERE
<script src="{$script}"></script>
DOCHERE;
}
}
}
// foreach
}
// else
$this->output = join( "\n\n\n", $scripts );
return $this->output;
}
// get_output
$this->output = join("\n\n\n", $scripts);
return $this->output;
}
}
// JSLoader