2011-08-03 16:03:31 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EGroupware: Class which manages including js files and modules
|
2021-06-05 20:39:39 +02:00
|
|
|
* (later on this might be extended to css)
|
2011-08-03 16:03:31 +02:00
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package api
|
2016-04-07 22:42:06 +02:00
|
|
|
* @subpackage framework
|
2011-08-03 16:03:31 +02:00
|
|
|
* @author Andreas Stöckel
|
|
|
|
* @copyright (c) 2011 Stylite
|
|
|
|
*/
|
|
|
|
|
2016-04-07 13:16:58 +02:00
|
|
|
namespace EGroupware\Api\Framework;
|
|
|
|
|
2011-08-03 16:03:31 +02:00
|
|
|
/**
|
|
|
|
* Syntax for including JS files form others
|
|
|
|
* -----------------------------------------
|
2011-08-21 22:01:10 +02:00
|
|
|
*
|
2021-06-05 20:39:39 +02:00
|
|
|
* a) ES5 or TypeScript module imports: import {...} from '<relative path or EGroupware absolute path>'
|
|
|
|
*
|
|
|
|
* - import './egw_core.js';
|
|
|
|
* - import { something } from '../../vendor/class.js';
|
|
|
|
* - import { something } from './other'; (in TS without extension!)
|
|
|
|
* @ToDo import { something } from 'module/sub'
|
|
|
|
*
|
|
|
|
* b) Write a comment starting with "/*egw:uses". A linebreak has to follow.
|
|
|
|
* Then write all files which have to be included separated by ";". A JS file
|
2011-08-03 16:03:31 +02:00
|
|
|
* include may have the following syntax:
|
2011-08-21 22:01:10 +02:00
|
|
|
*
|
2011-08-03 16:03:31 +02:00
|
|
|
* 1) File in the same directory as the current file. Simply write the filename
|
|
|
|
* without ".js". Example:
|
|
|
|
* egw_action;
|
|
|
|
* 2) Files in a certain application and package. The syntax is
|
|
|
|
* [$app.]$package.$file
|
|
|
|
* The first "$app" part is optional. It defaults to phpgwapi. Examples:
|
2016-06-06 17:38:20 +02:00
|
|
|
* /vendor/bower-asset/jquery-ui/jquery-ui.js; // Loads /vendor/bower-asset/jquery-ui/jquery-ui.js
|
2011-08-03 16:03:31 +02:00
|
|
|
* stylite.filemanager.filemanager; // Loads /stylite/filemanager/filemanager.js
|
|
|
|
* 3) Absolute file paths starting with "/". Example:
|
2016-04-07 12:38:05 +02:00
|
|
|
* /api/js/jquery/jquery-ui.js;
|
2021-01-03 20:06:45 +01:00
|
|
|
* 4) Absolute path with "*"-wildcard
|
|
|
|
* /myapp/js/plugins/*.js;
|
|
|
|
*
|
2011-08-03 16:03:31 +02:00
|
|
|
* Comments can be started with "//".
|
2011-08-21 22:01:10 +02:00
|
|
|
*
|
2011-08-03 16:03:31 +02:00
|
|
|
* Complete example of such an uses-clause:
|
|
|
|
* /*egw:uses
|
|
|
|
* egw_action_common;
|
|
|
|
* egw_action;
|
2016-06-06 17:38:20 +02:00
|
|
|
* vendor.bower-asset.jquery.dist.jquery; // Includes jquery.js from package jquery in phpgwapi
|
|
|
|
* /vendor/bower-asset/jquery-ui/jquery-ui.js; // Includes jquery-ui.js
|
2016-04-07 12:38:05 +02:00
|
|
|
*
|
|
|
|
* Class can be tested by opening it in browser:
|
|
|
|
*
|
2016-04-07 13:16:58 +02:00
|
|
|
* http://localhost/egroupware/api/src/Framework/IncludeMgr.inc.php?path=/api/js/jsapi/egw_calendar.js
|
2016-04-07 12:38:05 +02:00
|
|
|
*
|
|
|
|
* This will then show the parsed dependencies of the given path.
|
2011-08-03 16:03:31 +02:00
|
|
|
*/
|
2016-04-07 13:16:58 +02:00
|
|
|
class IncludeMgr
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
|
|
|
static private $DEBUG_MODE = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The parsed_files array holds all files which have already been processed
|
|
|
|
* by this class.
|
2013-02-13 17:27:52 +01:00
|
|
|
*
|
|
|
|
* @var array of path => true
|
2011-08-03 16:03:31 +02:00
|
|
|
*/
|
|
|
|
private $parsed_files = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The included files array holds all files which will really be included
|
|
|
|
* as a result of the current request.
|
2013-02-13 17:27:52 +01:00
|
|
|
*
|
|
|
|
* @var array of path => true
|
2011-08-03 16:03:31 +02:00
|
|
|
*/
|
|
|
|
private $included_files = array();
|
|
|
|
|
|
|
|
/**
|
2011-08-03 16:27:45 +02:00
|
|
|
* Set to the file which is currently processed, in order to get usable
|
|
|
|
* debug messages.
|
2011-08-03 16:03:31 +02:00
|
|
|
*/
|
|
|
|
private $debug_processing_file = false;
|
|
|
|
|
2021-06-05 20:39:39 +02:00
|
|
|
/**
|
|
|
|
* @var bool true: echo out debug info, for direct call see end of this file
|
|
|
|
*/
|
|
|
|
private $debug_parsing_imports = false;
|
|
|
|
|
2011-08-03 16:03:31 +02:00
|
|
|
/**
|
|
|
|
* Parses the js file for includes and returns all required files
|
|
|
|
*/
|
|
|
|
private function parse_file($file)
|
|
|
|
{
|
2013-02-13 17:27:52 +01:00
|
|
|
// file is from url and can contain query-params, eg. /phpgwapi/inc/jscalendar-setup.php?dateformat=d.m.Y&lang=de
|
|
|
|
if (strpos($file,'?') !== false) list($file) = explode('?',$file);
|
|
|
|
|
2011-08-03 16:03:31 +02:00
|
|
|
// Mark the file as parsed
|
|
|
|
$this->parsed_files[$file] = true;
|
|
|
|
|
2021-06-05 20:39:39 +02:00
|
|
|
// extension to add, if dependency has none
|
|
|
|
$extension = substr($file, -3) === '.ts' ? '.ts' : '.js';
|
|
|
|
|
|
|
|
$modules = $modules2 = [];
|
2011-08-03 16:03:31 +02:00
|
|
|
// Try to open the given file
|
2021-06-05 20:39:39 +02:00
|
|
|
if (($header = file_get_contents(EGW_SERVER_ROOT.$file, false, null, 0, 2048)))
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
2021-06-05 20:39:39 +02:00
|
|
|
if ($this->debug_parsing_imports) echo "<p>$file</p>\n";
|
|
|
|
$matches = null;
|
|
|
|
if (preg_match_all('/import\s+({[^}]+}\s*from\s+)?["\']([^;\s]+)["\']/', $header, $matches))
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
2021-06-05 20:39:39 +02:00
|
|
|
if ($this->debug_parsing_imports) echo "imports:<div style='white-space: pre'>".json_encode($matches, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)."</div>\n";
|
|
|
|
$modules = $matches[2];
|
|
|
|
}
|
|
|
|
// either use es5 import or old /*egw:uses */ annotation
|
|
|
|
elseif (preg_match('#/\*egw:uses(.*);\s+\*/#sU', $header, $matches) &&
|
|
|
|
($matches = array_filter(preg_split('/;[^\n]*\n\s*/', trim($matches[1])), static function($use)
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
2021-06-05 20:39:39 +02:00
|
|
|
return !preg_match('#^\s*//#', $use); // filter out commented out ones
|
|
|
|
})))
|
|
|
|
{
|
|
|
|
if ($this->debug_parsing_imports) echo "egw:uses:<div style='white-space: pre'>".json_encode($matches, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)."</div>\n";
|
|
|
|
$modules = array_merge($modules, $matches);
|
2011-08-03 16:03:31 +02:00
|
|
|
}
|
2011-08-03 16:27:45 +02:00
|
|
|
// Split all modules at "." and remove entries with empty parts
|
2011-08-03 16:03:31 +02:00
|
|
|
foreach ($modules as $mod)
|
|
|
|
{
|
|
|
|
if ($mod)
|
|
|
|
{
|
2021-06-05 20:39:39 +02:00
|
|
|
if ($mod !== trim($mod))
|
|
|
|
{
|
|
|
|
throw new \Exception("'$mod' contains whitespace!");
|
|
|
|
}
|
2011-08-03 16:03:31 +02:00
|
|
|
// Split the given module string at the dot, if this isn't
|
2021-06-05 20:39:39 +02:00
|
|
|
// an relative (starting ./) or absolute path (initialized by "/").
|
|
|
|
if (substr($mod, 0, 2) === './')
|
|
|
|
{
|
|
|
|
if (!preg_match('/\.(js|ts)$/', $mod)) $mod .= $extension;
|
|
|
|
$mod = [dirname($file).substr($mod, 1)];
|
|
|
|
}
|
|
|
|
elseif (substr($mod, 0, 3) === '../')
|
|
|
|
{
|
|
|
|
if (!preg_match('/\.(js|ts)$/', $mod)) $mod .= $extension;
|
|
|
|
$dir = dirname($file);
|
|
|
|
while(substr($mod, 0, 3) === '../')
|
|
|
|
{
|
|
|
|
$mod = substr($mod, 3);
|
|
|
|
$dir = dirname($dir);
|
|
|
|
}
|
|
|
|
$mod = [($dir !== '/' ? $dir : '').'/'.$mod];
|
|
|
|
}
|
|
|
|
elseif ($mod[0] != '/')
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
2011-09-09 13:41:14 +02:00
|
|
|
$mod = explode(".", $mod, 3);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$mod = array($mod);
|
2011-08-03 16:03:31 +02:00
|
|
|
}
|
|
|
|
$empty = false;
|
|
|
|
|
|
|
|
// Remove all space characters
|
|
|
|
foreach ($mod as &$entry)
|
|
|
|
{
|
|
|
|
$entry = trim($entry);
|
|
|
|
$empty = $empty || !$entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$empty)
|
|
|
|
{
|
|
|
|
$modules2[] = $mod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $modules2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function file_processed($file)
|
|
|
|
{
|
2016-04-07 13:16:58 +02:00
|
|
|
return array_key_exists($file, $this->included_files) ||
|
|
|
|
array_key_exists($file, $this->parsed_files);
|
2011-08-03 16:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the given files and adds all dependencies to the passed modules array
|
|
|
|
*/
|
|
|
|
private function parse_deps($path, array &$module)
|
|
|
|
{
|
|
|
|
$this->debug_processing_file = $path;
|
|
|
|
|
|
|
|
// Parse the given file for dependencies
|
|
|
|
$uses = $this->parse_file($path);
|
|
|
|
|
2011-11-10 15:39:07 +01:00
|
|
|
foreach ((array)$uses as $entry)
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
|
|
|
$uses_path = false;
|
|
|
|
|
|
|
|
// Check whether just a filename was given - if yes, check whether
|
|
|
|
// a file with the given name exists inside the directory of the
|
|
|
|
// base file
|
|
|
|
if (count($entry) == 1)
|
|
|
|
{
|
2011-09-09 16:03:18 +02:00
|
|
|
if ($entry[0][0] == "/")
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
2021-01-03 20:06:45 +01:00
|
|
|
if (!($uses_path = $this->translate_params($entry[0], null, '')) &&
|
|
|
|
strpos($entry[0], '*') !== false)
|
|
|
|
{
|
|
|
|
$start = strlen(EGW_SERVER_ROOT);
|
|
|
|
foreach(glob(EGW_SERVER_ROOT.$entry[0]) as $file)
|
|
|
|
{
|
|
|
|
$this->parse_deps(substr($file, $start), $module);
|
|
|
|
}
|
|
|
|
}
|
2011-08-03 16:03:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-09-09 16:03:18 +02:00
|
|
|
// Assemble a filename
|
|
|
|
$filename = dirname($path).'/'.$entry[0].'.js';
|
|
|
|
|
|
|
|
if (is_readable(EGW_SERVER_ROOT.($filename)))
|
|
|
|
{
|
|
|
|
if (!$this->file_processed($filename))
|
|
|
|
{
|
|
|
|
$uses_path = $filename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-07 12:38:05 +02:00
|
|
|
$uses_path = $this->translate_params($entry[0]);
|
2011-09-09 16:03:18 +02:00
|
|
|
}
|
2011-08-03 16:03:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (count($entry) == 2)
|
|
|
|
{
|
2016-04-07 12:38:05 +02:00
|
|
|
$uses_path = $this->translate_params($entry[0], $entry[1]);
|
2011-08-03 16:03:31 +02:00
|
|
|
}
|
|
|
|
else if (count($entry) == 3)
|
|
|
|
{
|
|
|
|
$uses_path = $this->translate_params($entry[1], $entry[2], $entry[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-13 17:27:52 +01:00
|
|
|
error_log(__METHOD__." invalid egw:require in js_file '$path' -> ".array2string($entry));
|
2011-08-03 16:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($uses_path)
|
|
|
|
{
|
|
|
|
$this->parse_deps($uses_path, $module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the file to the top of the list
|
|
|
|
array_push($module, $path);
|
|
|
|
|
|
|
|
$this->debug_processing_file = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Includes the given module files - this function will have the task to
|
|
|
|
* cache/shrink/concatenate the files in the future.
|
2016-08-15 18:35:26 +02:00
|
|
|
*
|
|
|
|
* @param array $modules pathes to include
|
|
|
|
* @param boolean $append =true false: prepend modules before already included ones, keeping their order
|
2011-08-03 16:03:31 +02:00
|
|
|
*/
|
2016-08-15 18:35:26 +02:00
|
|
|
private function include_module(array $modules, $append=true)
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
|
|
|
if (self::$DEBUG_MODE)
|
|
|
|
{
|
2016-08-15 18:35:26 +02:00
|
|
|
if ($append)
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
2016-08-15 18:35:26 +02:00
|
|
|
foreach ($modules as $path)
|
|
|
|
{
|
|
|
|
$this->included_files[$path] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->included_files = array_merge(array_combine($modules, array_fill(0, count($modules), true)),
|
|
|
|
$this->included_files);
|
2011-08-03 16:03:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates the given parameters into a path and checks it for validity.
|
|
|
|
*
|
|
|
|
* Example call syntax:
|
|
|
|
* a) egw_framework::validate_file('jscalendar','calendar')
|
|
|
|
* --> /phpgwapi/js/jscalendar/calendar.js
|
|
|
|
* b) egw_framework::validate_file('/phpgwapi/inc/calendar-setup.js',array('lang'=>'de'))
|
|
|
|
* --> /phpgwapi/inc/calendar-setup.js?lang=de
|
|
|
|
*
|
|
|
|
* @param string $package package or complete path (relative to EGW_SERVER_ROOT) to be included
|
2016-03-01 21:45:31 +01:00
|
|
|
* @param string|array $file =null file to be included - no ".js" on the end or array with get params
|
2016-04-07 12:38:05 +02:00
|
|
|
* @param string $app ='api' application directory to search - default = phpgwapi
|
2011-08-03 16:03:31 +02:00
|
|
|
*
|
|
|
|
* @returns the correct path on the server if the file is found or false, if the
|
|
|
|
* file is not found or no further processing is needed.
|
|
|
|
*/
|
2016-04-07 12:38:05 +02:00
|
|
|
private function translate_params($package, $file=null, $app='api')
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
2016-04-07 12:38:05 +02:00
|
|
|
if ($package[0] == '/' && is_readable(EGW_SERVER_ROOT.parse_url($path = $package, PHP_URL_PATH)) ||
|
|
|
|
// fix old /phpgwapi/js/ path by replacing it with /api/js/
|
|
|
|
substr($package, 0, 13) == '/phpgwapi/js/' && is_readable(EGW_SERVER_ROOT.parse_url($path = str_replace('/phpgwapi/js/', '/api/js/', $package), PHP_URL_PATH)) ||
|
|
|
|
$package[0] == '/' && is_readable(EGW_SERVER_ROOT.($path = $package)) ||
|
2013-11-11 09:19:00 +01:00
|
|
|
$package == '.' && is_readable(EGW_SERVER_ROOT.($path="/$app/js/$file.js")) ||
|
2011-08-03 16:03:31 +02:00
|
|
|
is_readable(EGW_SERVER_ROOT.($path="/$app/js/$package/$file.js")) ||
|
2016-04-07 12:38:05 +02:00
|
|
|
// fix not found by using app='api'
|
|
|
|
$app != 'api' && is_readable(EGW_SERVER_ROOT.($path="/api/js/$package/$file.js")) ||
|
2011-08-03 16:03:31 +02:00
|
|
|
$app != 'phpgwapi' && is_readable(EGW_SERVER_ROOT.($path="/phpgwapi/js/$package/$file.js")))
|
|
|
|
{
|
2014-01-11 19:49:51 +01:00
|
|
|
// normalise /./ to /
|
|
|
|
$path = str_replace('/./', '/', $path);
|
2011-08-03 16:03:31 +02:00
|
|
|
|
|
|
|
// Handle the special case, that the file is an url - in this case
|
|
|
|
// we will do no further processing but just include the file
|
|
|
|
// XXX: Is this case still used? If yes, it will not work with
|
|
|
|
// adding the ctime to all js files...
|
|
|
|
if (is_array($file))
|
|
|
|
{
|
|
|
|
foreach($file as $name => $val)
|
|
|
|
{
|
|
|
|
$args .= (empty($args) ? '?' : '&').$name.'='.urlencode($val);
|
|
|
|
}
|
|
|
|
$path .= $args;
|
|
|
|
|
|
|
|
$this->included_files[$path] = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return false if the file is already included or parsed
|
|
|
|
if ($this->file_processed($path))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self::$DEBUG_MODE) // DEBUG_MODE is currently ALWAYS true. Comment this code out if you don't want error messages.
|
|
|
|
{
|
2011-11-10 15:39:07 +01:00
|
|
|
//error_log(__METHOD__."($package,$file,$app) $path NOT found".($this->debug_processing_file ? " while processing file '{$this->debug_processing_file}'." : "!").' '.function_backtrace());
|
2011-08-03 16:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-13 17:27:52 +01:00
|
|
|
/**
|
|
|
|
* Include a javascript file
|
|
|
|
*
|
|
|
|
* Example call syntax:
|
|
|
|
* a) include_js_file('jscalendar','calendar')
|
|
|
|
* --> /phpgwapi/js/jscalendar/calendar.js
|
|
|
|
* b) include_js_file('/phpgwapi/inc/calendar-setup.js',array('lang'=>'de'))
|
|
|
|
* --> /phpgwapi/inc/calendar-setup.js?lang=de
|
|
|
|
*
|
|
|
|
* @param string $package package or complete path (relative to EGW_SERVER_ROOT) to be included
|
2016-03-01 21:45:31 +01:00
|
|
|
* @param string|array $file =null file to be included - no ".js" on the end or array with get params
|
|
|
|
* @param string $app ='phpgwapi' application directory to search - default = phpgwapi
|
2016-08-15 18:35:26 +02:00
|
|
|
* @param boolean $append =true true append file, false prepend (add as first) file used eg. for template itself
|
2013-02-13 17:27:52 +01:00
|
|
|
*/
|
2016-08-15 18:35:26 +02:00
|
|
|
public function include_js_file($package, $file = null, $app = 'phpgwapi', $append=true)
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
|
|
|
// Translate the given parameters into a valid path - false is returned
|
|
|
|
// if the file is not found or the file is already included/has already
|
|
|
|
// been parsed by this class.
|
|
|
|
$path = $this->translate_params($package, $file, $app);
|
|
|
|
|
|
|
|
if ($path !== false)
|
|
|
|
{
|
|
|
|
// TODO: Check whether the given path is cached, if yes, include the
|
|
|
|
// cached module file and add all files this module contains
|
|
|
|
// to the "parsed_files" array
|
|
|
|
|
|
|
|
// Collect the possible list of dependant modules of this file in
|
|
|
|
// the "module" array.
|
|
|
|
$module = array();
|
|
|
|
$this->parse_deps($path, $module);
|
|
|
|
|
2016-08-15 18:55:29 +02:00
|
|
|
$this->include_module($module, $append);
|
2011-08-03 16:03:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 17:27:52 +01:00
|
|
|
/**
|
|
|
|
* Include given files, optionally clear list of files to include
|
|
|
|
*
|
|
|
|
* @param array $files
|
2016-03-01 21:45:31 +01:00
|
|
|
* @param boolean $clear_files =false if true clear list of files, before including given ones
|
2013-02-13 17:27:52 +01:00
|
|
|
*/
|
|
|
|
public function include_files(array $files, $clear_files=false)
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
2016-03-01 21:45:31 +01:00
|
|
|
if ($clear_files) $this->included_files = array();
|
2013-02-13 17:27:52 +01:00
|
|
|
|
2011-08-03 16:03:31 +02:00
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
$this->include_js_file($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 17:27:52 +01:00
|
|
|
/**
|
|
|
|
* Return all files
|
|
|
|
*
|
2016-03-01 21:45:31 +01:00
|
|
|
* @param boolean $clear_files =false if true clear list of files after returning them
|
2013-02-13 17:27:52 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_included_files($clear_files=false)
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
2013-02-13 17:27:52 +01:00
|
|
|
$ret = array_keys($this->included_files);
|
2013-07-19 17:24:55 +02:00
|
|
|
if ($clear_files) $this->included_files = array();
|
2013-02-13 17:27:52 +01:00
|
|
|
return $ret;
|
2011-08-03 16:03:31 +02:00
|
|
|
}
|
|
|
|
|
2021-06-05 20:39:39 +02:00
|
|
|
/**
|
|
|
|
* Get importMap for browser
|
|
|
|
*/
|
|
|
|
public function getImportMap()
|
|
|
|
{
|
|
|
|
$files = $this->get_included_files();
|
|
|
|
$imports = array_combine(array_map(static function($url)
|
|
|
|
{
|
|
|
|
return parse_url($url, PHP_URL_PATH);
|
|
|
|
}, $files), $files);
|
|
|
|
|
|
|
|
return ['imports' => $imports];
|
|
|
|
}
|
|
|
|
|
2013-02-13 17:27:52 +01:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2016-03-01 21:45:31 +01:00
|
|
|
* @param array $files =null optional files to include as for include_files method
|
2021-06-05 20:39:39 +02:00
|
|
|
* @param bool $debug_parsing_imports =false true: echo out debug infos
|
2013-02-13 17:27:52 +01:00
|
|
|
*/
|
2021-06-05 20:39:39 +02:00
|
|
|
public function __construct(array $files = null, bool $debug_parsing_imports=false)
|
2011-08-03 16:03:31 +02:00
|
|
|
{
|
2021-06-05 20:39:39 +02:00
|
|
|
$this->debug_parsing_imports = $debug_parsing_imports;
|
2011-08-03 16:03:31 +02:00
|
|
|
if (isset($files) && is_array($files))
|
|
|
|
{
|
|
|
|
$this->include_files($files);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Small test for this class
|
2013-07-19 17:24:55 +02:00
|
|
|
// specify one or more files in url, eg. path[]=/phpgwapi/js/jsapi/egw.js&path[]=/etemplate/js/etemplate2.js
|
|
|
|
if (isset($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME'] == __FILE__)
|
|
|
|
{
|
2016-04-07 13:16:58 +02:00
|
|
|
define('EGW_SERVER_ROOT', dirname(dirname(dirname(__DIR__))));
|
2021-06-05 20:39:39 +02:00
|
|
|
define('EGW_INCLUDE_ROOT', dirname(dirname(dirname(__DIR__))));
|
|
|
|
include_once(EGW_SERVER_ROOT.'/api/src/loader/common.php');
|
2011-08-03 16:03:31 +02:00
|
|
|
|
2021-06-05 20:39:39 +02:00
|
|
|
$mgr = new IncludeMgr(null, true);
|
2013-07-19 17:24:55 +02:00
|
|
|
echo "<html>\n<head>\n\t<title>Dependencies</title>\n</head>\n<body>\n";
|
2011-08-03 16:03:31 +02:00
|
|
|
|
2013-07-19 17:24:55 +02:00
|
|
|
$paths = !empty($_GET['path']) ? (array)$_GET['path'] : (array)'/stylite/js/filemanager/filemanager.js';
|
2011-08-03 16:03:31 +02:00
|
|
|
|
2013-07-19 17:24:55 +02:00
|
|
|
foreach($paths as $path)
|
|
|
|
{
|
|
|
|
echo "\t<h1>".htmlspecialchars($path)."</h1>\n";
|
|
|
|
$mgr->include_js_file($path);
|
|
|
|
foreach ($mgr->get_included_files(true) as $file)
|
|
|
|
{
|
|
|
|
echo "\t<a href='".$_SERVER['PHP_SELF'].'?path='.$file."'>$file</a><br/>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo "</body>\n</html>\n";
|
2011-08-03 16:03:31 +02:00
|
|
|
}
|