Adding more improvements to increase performance, by now, it is just affecting for API Rest Requests

This commit is contained in:
Erik Amaru Ortiz
2014-03-12 18:20:35 -04:00
parent e649e829f5
commit 3026937e08
16 changed files with 2335 additions and 1012 deletions

View File

@@ -0,0 +1,338 @@
<?php
namespace Maveriks\Http;
/**
* Class Response
* Send a http response from a file or string
*
* @package Maveriks\Http
* @author Erik Amaru Ortiz <erik@colosa.com, aortiz.erik@gmail.com>
*/
class Response
{
/**
* @var string content stream
*/
protected $content = "";
protected $sourceName = "";
protected $headers = array();
protected $charset;
protected $statusCode;
protected $extension;
protected $filename;
protected $version;
protected $statusText;
protected static $mimeType = array(
'ai' => 'application/postscript', 'bcpio' => 'application/x-bcpio', 'bin' => 'application/octet-stream',
'ccad' => 'application/clariscad', 'cdf' => 'application/x-netcdf', 'class' => 'application/octet-stream',
'cpio' => 'application/x-cpio', 'cpt' => 'application/mac-compactpro', 'csh' => 'application/x-csh',
'csv' => 'application/csv', 'dcr' => 'application/x-director', 'dir' => 'application/x-director',
'dms' => 'application/octet-stream', 'doc' => 'application/msword', 'drw' => 'application/drafting',
'dvi' => 'application/x-dvi', 'dwg' => 'application/acad', 'dxf' => 'application/dxf',
'dxr' => 'application/x-director', 'eot' => 'application/vnd.ms-fontobject', 'eps' => 'application/postscript',
'exe' => 'application/octet-stream', 'ez' => 'application/andrew-inset',
'flv' => 'video/x-flv', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip',
'bz2' => 'application/x-bzip', '7z' => 'application/x-7z-compressed', 'hdf' => 'application/x-hdf',
'hqx' => 'application/mac-binhex40', 'ico' => 'image/vnd.microsoft.icon', 'ips' => 'application/x-ipscript',
'ipx' => 'application/x-ipix', 'js' => 'application/x-javascript', 'latex' => 'application/x-latex',
'lha' => 'application/octet-stream', 'lsp' => 'application/x-lisp', 'lzh' => 'application/octet-stream',
'man' => 'application/x-troff-man', 'me' => 'application/x-troff-me', 'mif' => 'application/vnd.mif',
'ms' => 'application/x-troff-ms', 'nc' => 'application/x-netcdf', 'oda' => 'application/oda',
'otf' => 'font/otf', 'pdf' => 'application/pdf',
'pgn' => 'application/x-chess-pgn', 'pot' => 'application/mspowerpoint', 'pps' => 'application/mspowerpoint',
'ppt' => 'application/mspowerpoint', 'ppz' => 'application/mspowerpoint', 'pre' => 'application/x-freelance',
'prt' => 'application/pro_eng', 'ps' => 'application/postscript', 'roff' => 'application/x-troff',
'scm' => 'application/x-lotusscreencam', 'set' => 'application/set', 'sh' => 'application/x-sh',
'shar' => 'application/x-shar', 'sit' => 'application/x-stuffit', 'skd' => 'application/x-koan',
'skm' => 'application/x-koan', 'skp' => 'application/x-koan', 'skt' => 'application/x-koan',
'smi' => 'application/smil', 'smil' => 'application/smil', 'sol' => 'application/solids',
'spl' => 'application/x-futuresplash', 'src' => 'application/x-wais-source', 'step' => 'application/STEP',
'stl' => 'application/SLA', 'stp' => 'application/STEP', 'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml',
'swf' => 'application/x-shockwave-flash', 't' => 'application/x-troff',
'tar' => 'application/x-tar', 'tcl' => 'application/x-tcl', 'tex' => 'application/x-tex',
'texi' => 'application/x-texinfo', 'texinfo' => 'application/x-texinfo', 'tr' => 'application/x-troff',
'tsp' => 'application/dsptype', 'ttf' => 'font/ttf',
'unv' => 'application/i-deas', 'ustar' => 'application/x-ustar',
'vcd' => 'application/x-cdlink', 'vda' => 'application/vda', 'xlc' => 'application/vnd.ms-excel',
'xll' => 'application/vnd.ms-excel', 'xlm' => 'application/vnd.ms-excel', 'xls' => 'application/vnd.ms-excel',
'xlw' => 'application/vnd.ms-excel', 'zip' => 'application/zip', 'aif' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff', 'au' => 'audio/basic', 'kar' => 'audio/midi', 'mid' => 'audio/midi',
'midi' => 'audio/midi', 'mp2' => 'audio/mpeg', 'mp3' => 'audio/mpeg', 'mpga' => 'audio/mpeg',
'ra' => 'audio/x-realaudio', 'ram' => 'audio/x-pn-realaudio', 'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin', 'snd' => 'audio/basic', 'tsi' => 'audio/TSP-audio', 'wav' => 'audio/x-wav',
'asc' => 'text/plain', 'c' => 'text/plain', 'cc' => 'text/plain', 'css' => 'text/css', 'etx' => 'text/x-setext',
'f' => 'text/plain', 'f90' => 'text/plain', 'h' => 'text/plain', 'hh' => 'text/plain', 'htm' => 'text/html',
'html' => 'text/html', 'm' => 'text/plain', 'rtf' => 'text/rtf', 'rtx' => 'text/richtext', 'sgm' => 'text/sgml',
'sgml' => 'text/sgml', 'tsv' => 'text/tab-separated-values', 'tpl' => 'text/template', 'txt' => 'text/plain',
'xml' => 'text/xml', 'avi' => 'video/x-msvideo', 'fli' => 'video/x-fli', 'mov' => 'video/quicktime',
'movie' => 'video/x-sgi-movie', 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg',
'qt' => 'video/quicktime', 'viv' => 'video/vnd.vivo', 'vivo' => 'video/vnd.vivo', 'gif' => 'image/gif',
'ief' => 'image/ief', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg',
'pbm' => 'image/x-portable-bitmap', 'pgm' => 'image/x-portable-graymap', 'png' => 'image/png',
'pnm' => 'image/x-portable-anymap', 'ppm' => 'image/x-portable-pixmap', 'ras' => 'image/cmu-raster',
'rgb' => 'image/x-rgb', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap', 'xwd' => 'image/x-xwindowdump', 'ice' => 'x-conference/x-cooltalk',
'iges' => 'model/iges', 'igs' => 'model/iges', 'mesh' => 'model/mesh', 'msh' => 'model/mesh',
'silo' => 'model/mesh', 'vrml' => 'model/vrml', 'wrl' => 'model/vrml',
'mime' => 'www/mime', 'pdb' => 'chemical/x-pdb', 'xyz' => 'chemical/x-pdb'
);
protected static $statusTexts = array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
);
public function __construct($content = "", $status = 200, $headers = array())
{
if (! empty($content)) {
$this->setContent($content);
}
if (! empty($headers)) {
$this->headers = $headers;
if (! isset($this->headers['Date'])) {
$this->setDate(new \DateTime(null, new \DateTimeZone('UTC')));
}
}
$this->setStatusCode($status);
$this->setProtocolVersion("1.0");
}
/**
* @param string $content content string to stream
*/
public function setContent($content)
{
$this->content = $content;
// a string was set as content, now define as text/html
$this->setHeader("Content-Type", "text/html");
}
/**
* @param string $filename file to stream
* @throws \Exception
*/
public function loadFromFile($filename)
{
if (! file_exists($filename)) {
throw new \Exception("Unable to find file: $filename");
}
$this->filename = $filename;
$this->content = file_get_contents($this->filename);
$fileInfo = pathinfo($filename, PATHINFO_EXTENSION);
$this->setExtension($fileInfo);
$this->setSourceName(basename($filename));
}
public function setExtension($extension)
{
$this->extension = $extension;
}
/**
* Returns the extension of the file.
* \SplFileInfo::getExtension() is not available before PHP 5.3.6
*
* @return string The extension
*/
public function getExtension()
{
return $this->extension;
}
/**
* Returns the mime type of the file. (improved by erik)
*
* The mime type is guessed using the functions finfo(), mime_content_type()
* and the system binary "file" (in this order), depending on which of those
* is available on the current operating system.
*
* @author Erik Amaru Ortiz <aortiz.erik@gmail.com>
* @return string|null The guessed mime type (i.e. "application/pdf")
*/
public function getMimeType()
{
if (array_key_exists($this->getExtension(), self::$mimeType)) {
return self::$mimeType[$this->getExtension()];
}
if (! empty($this->filename)) {
if (class_exists('finfo')) {
$finfo = new \finfo;
$mimeType = $finfo->file($this->filename, FILEINFO_MIME);
if (preg_match('/([\w\-]+\/[\w\-]+); charset=(\w+)/', $mimeType, $match)) {
return $match[1];
}
}
if (function_exists('mime_content_type')) {
return mime_content_type($this->filename);
}
}
return 'application/octet-stream';
}
public function setSourceName($sourceName)
{
$this->sourceName = $sourceName;
}
public function setProtocolVersion($version)
{
$this->version = $version;
}
public function setHeader($header, $value)
{
$this->headers[$header] = $value;
}
public function setDate(\DateTime $date)
{
$date->setTimezone(new \DateTimeZone('UTC'));
$this->headers['Date'] = $date->format('D, d M Y H:i:s') . ' GMT';
}
/**
* Retrieves the response charset.
*
* @return string Character set
*/
public function getCharset()
{
return $this->charset;
}
/**
* Sets the response charset.
*
* @param string $charset
* @return string Character set
*/
public function setCharset($charset)
{
$this->charset = $charset;
}
public function setStatusCode($code, $text = null)
{
$this->statusCode = (int) $code;
$this->statusText = false === $text ? '' : (null === $text ? self::$statusTexts[$this->statusCode] : $text);
}
public function prepare()
{
if (! empty($this->sourceName)) {
//TODO define a setter for this
//$this->setHeader('Content-Disposition', 'attachment; filename="'. $this->sourceName . '"' );
}
// Fix Content-Type
$charset = $this->charset ?: 'UTF-8';
if (! isset($this->headers['Content-Type'])) {
$this->headers['Content-Type'] = $this->getMimeType();
}
if (
0 === strpos($this->headers['Content-Type'], 'text/') &&
false === strpos($this->headers['Content-Type'], 'charset')
) {
// add the charset
$this->headers['Content-Type'] = $this->headers['Content-Type'].'; charset='.$charset;
}
// Fix Content-Length
if (isset($this->headers['Transfer-Encoding'])) {
unset($this->headers['Content-Length']);
}
}
public function sendHeaders()
{
// headers have already been sent by the developer
if (headers_sent()) {
return;
}
$this->prepare();
// status
header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText));
// headers
foreach ($this->headers as $name => $value) {
if (is_array($value)) {
foreach ($value as $v) {
header($name.': '.$v, false);
}
} else {
header($name.': '.$value, false);
}
}
}
public function sendContent()
{
echo $this->content;
}
public function send()
{
$this->sendHeaders();
$this->sendContent();
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Maveriks\Pattern\Mvc;
class PhtmlView extends View
{
public static $blocks = array();
private static $instance;
private static $extend = '';
public function __construct($tpl = '')
{
parent::__construct($tpl);
}
//Wrapped
public function render()
{
self::$instance = $this;
$tplFile = $this->getTpl();
if (empty($tplFile)) {
throw new \Exception('ERROR: template file was not set!');
}
foreach ($this->getTemplateDir() as $dir) {
if (file_exists($dir . DIRECTORY_SEPARATOR . $tplFile)) {
$tplFile = $dir . DIRECTORY_SEPARATOR . $tplFile;
break;
}
}
if (! file_exists($tplFile)) {
throw new \Exception('ERROR: template file '.$tplFile.' does not exist!');
}
extract($this->data);
include $tplFile;
if (! empty(self::$extend)) {
$tplFile = ''; //$this->getTemplateDir() . DIRECTORY_SEPARATOR . self::$extend . '.phtml';
foreach ($this->getTemplateDir() as $dir) {
if (file_exists($dir . DIRECTORY_SEPARATOR . self::$extend . '.phtml')) {
$tplFile = $dir . DIRECTORY_SEPARATOR . self::$extend . '.phtml';
break;
}
}
if (! file_exists($tplFile)) {
throw new \Exception('ERROR: Layout template file '.$tplFile.' does not exist!');
}
extract($this->data);
include $tplFile;
}
}
static public function extend($name)
{
self::$extend = $name;
}
static public function block($name, $default = '')
{
if (is_string($default)) {
if (array_key_exists($name, self::$blocks)) {
$callback = self::$blocks[$name];
$callback();
} else {
return $default;
}
return;
} elseif ($default instanceof \Closure) {
self::$blocks[$name] = $default;
}
}
}

View File

@@ -0,0 +1,299 @@
<?php
namespace Maveriks\Pattern\Mvc;
/**
* View
*
* This is the parent class to support view at MVC Pattern
* (Adapted version from PhpAlchemy project)
*
* @version 1.0
* @author Erik Amaru Ortiz <aortiz.erik@gmail.com>
* @link https://github.com/phpalchemy/phpalchemy
* @copyright Copyright 2012 Erik Amaru Ortiz
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @package Maveriks\Pattern\Mvc
*/
abstract class View
{
/**
* Contains all variables that are available on template file
*
* @var array
*/
protected $data = array();
/**
* Contains the absolute paths where templates are stored
*
* @var array
*/
protected $templateDir = array();
/**
* Contains the absolute path where the engine store the cache files
*
* @var string
*/
protected $cacheDir = '';
/**
* String to store the output string that is sent by http response
*
* @var string
*/
protected $content = '';
/**
* Relative path of template file
*
* @var string
*/
protected $tpl = '';
/**
* Cache flag to specify if templating cache is enabled or not
*
* @var string
*/
protected $cache = false;
/**
* Charset Encodig
*
* @var string
*/
protected $charset = 'UTF-8';
/**
* Debug flag to indicate to template engine is a debug environment
* Defaults false
*
* @var bool
*/
public $debug = false;
/**
* @param string $tpl template file
*/
public function __construct($tpl = '')
{
$this->tpl = $tpl;
}
/**
* Sets template file path (absolute or partial path)
*
* @param string $tpl contains the path of template file
*/
public function setTpl($tpl)
{
$this->tpl = $tpl;
}
/**
* Gets template file
*/
public function getTpl()
{
return $this->tpl;
}
/**
* Sets the base path where the engine can be find all templates
*
* @param string $path contains the absolute path where templates are stored
*/
public function setTemplateDir($path)
{
$this->templateDir[] = $path;
}
/**
* Gets the templates files base path
*
* @return array returns the templates base path
*/
public function getTemplateDir()
{
return $this->templateDir;
}
/**
* Sets cache directory path
*
* @param string $path cache directory path
*/
public function setCacheDir($dir)
{
$this->cacheDir = $dir;
if (! is_dir($this->cacheDir)) {
$this->createDir($this->cacheDir);
}
}
/**
* Gets cache directory path
*
* @return string contains cache directory path
*/
public function getCacheDir()
{
return $this->cacheDir;
}
/**
* Enable/Disable templating cache
*
* @param bool $value boolean value to enable or not cache
*/
public function enableCache($value)
{
$this->cache = $value ? true: false;
}
/**
* Enable/Disable debug mode of template engine
*
* @param bool $value boolean value to enable or not debug mode
*/
public function enableDebug($value)
{
$this->debug = $value ? true: false;
}
/**
* Sets charset encoding for template engine
* @param string $charset conatins a valid charset like UTF-8, ISO-8859-1 (latin), etc.
*/
public function setCharset($charset)
{
$this->charset = $charset;
}
/**
* Gets charset encoding
*
* @return string charset encoding
*/
public function getCharset()
{
return $this->charset;
}
/**
* Alias of method assign()
*
* @param string $name name or key to store teh value passed
* @param string $value variable value
*/
public function set($name, $value = null)
{
$this->assign($name, $value);
}
/**
* Assings a variable to the template file
*
* @param string $name name or key to store teh value passed
* @param string $value variable value
*/
public function assign($name, $value = null)
{
if (is_string($name)) {
return $this->data[$name] = $value;
}
if (is_array($name)) {
$this->assignFromArray($name);
return null;
}
throw new \InvalidArgumentException("Invalid data type for key, '" .gettype($name) . "' given.");
}
/**
* Gets a variable that was previously assigned
*
* @param string $name name or key to store teh value passed
* @param string $value variable value
*/
public function get($name)
{
if (!isset($this->data[$name])) {
throw new \InvalidArgumentException("Variable '$name' doesn't exist.");
}
return $this->data[$name];
}
public function exists($name)
{
return array_key_exists($name, $this->data);
}
/**
* Gets final template parsed output string
*
* @return string parsed output
*/
public function getOutput()
{
$output = '';
\ob_start();
$this->render();
$output = \ob_get_contents();
\ob_end_clean();
return $output;
}
/**
* Render the output string (To override by child class)
*/
abstract public function render();
/**
* Multiple variable assignment
*
* @param array $data associative array conatining variables, the keys are used as variables names
*/
protected function assignFromArray($data)
{
if (! is_array($data)) {
throw new \InvalidArgumentException(
"Invalid data type: argument should be array, '" .gettype($data) . "' given."
);
}
foreach ($data as $key => $value) {
$this->assign($key, $value);
}
}
protected function createDir($strPath, $rights = 0777)
{
$folderPath = array($strPath);
$oldumask = umask(0);
while (!@is_dir(dirname(end($folderPath)))
&& dirname(end($folderPath)) != '/'
&& dirname(end($folderPath)) != '.'
&& dirname(end($folderPath)) != ''
) {
array_push($folderPath, dirname(end($folderPath)));
}
while ($parentFolderPath = array_pop($folderPath)) {
if (! @is_dir($parentFolderPath)) {
if (! @mkdir($parentFolderPath, $rights)) {
throw new \Exception("Templating Engine Error: Can't create folder '$parentFolderPath'");
}
}
}
umask($oldumask);
}
}

View File

@@ -0,0 +1,117 @@
<?php
namespace Maveriks\Util;
class ClassLoader
{
private static $includePath = array();
private static $includePathNs = array();
protected static $instance;
/**
* Creates a new <tt>SplClassLoader</tt> that loads classes of the
* specified namespace.
*
* @param string $ns The namespace to use.
*/
public function __construct()
{
defined("DS") || define("DS", DIRECTORY_SEPARATOR);
defined("NS") || define("NS", "\\");
spl_autoload_register(array($this, 'loadClass'));
}
/**
* @return \Maveriks\Util\ClassLoader
*/
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Gets the base include path for all class files in the namespace of this class loader.
*
* @return string $includePath
*/
public function getIncludePaths()
{
return self::$includePath;
}
/**
* Uninstalls this class loader from the SPL autoloader stack.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
public function add($sourceDir, $namespace = "")
{
if (empty($namespace)) {
self::$includePath[] = $sourceDir . (substr($sourceDir, -1) == DS ? "" : DS);
} else {
self::$includePathNs[$namespace] = $sourceDir . (substr($sourceDir, -1) == DS ? "" : DS);
}
}
function loadClass($className)
{
$classPath = str_replace(NS, DS, $className);
if (false !== strpos($className, NS)) {// has namespace?
$lastPos = strpos($className, NS);
$mainNs = substr($className, 0, $lastPos);
if (isset(self::$includePathNs[$mainNs])) {
if (file_exists(self::$includePathNs[$mainNs] . $classPath . ".php")) {
require self::$includePathNs[$mainNs] . $classPath . ".php";
return true;
} else {
return false;
}
}
}
foreach (self::$includePath as $path) {
$filename = $path . $classPath . ".php";
//var_dump($filename);
if (file_exists($filename)) {
require $filename;
return true;
}
}
return false;
}
/**
* Loads the given class or interface.
*
* @param string $className The name of the class to load.
* @return void
*/
public function loadClass2($className)
{
if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) {
$fileName = '';
$namespace = '';
if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
}
}
}

View File

@@ -0,0 +1,128 @@
<?php
namespace Maveriks\Util;
class Common
{
/**
* Recursive version of glob php standard function
*
* @author Erik Amaru Ortiz <erik@colosa.com, aortiz.erik@gmail.com>
* @param string $pattern pattern for native glob function
* @param int|string $flags any valid flag for glob function
* @param bool $onlyFiles to filter return array with only matched files, or all matched results
* @return array array containing the recursive glob results
*
* Example:
*
* Common::rglob("/example/path/*");
*
* it will returns:
*
* Array
* (
* [0] => /example/path/README.txt
* [1] => /example/path/composer.json
* [4] => /example/path/one/one_text.txt
* [6] => /example/path/two/two_text.txt
* [7] => /example/path/two/two_one/two_one_text.txt
* [8] => /example/path/two/two_one/build.json
* )
*
* Example 2:
*
* Common::rglob("/example/path/*.json");
*
* It will returns:
*
* Array
* (
* [0] => /example/path/composer.json
* [1] => /example/path/two/two_one/build.json
* )
*/
public static function rglob($pattern, $flags = 0, $onlyFiles = false)
{
$singlePattern = basename($pattern);
if (strpos($singlePattern, "*") !== false) {
$path = rtrim(str_replace($singlePattern, "", $pattern), DIRECTORY_SEPARATOR);
} else {
$singlePattern = "";
$path = $pattern;
}
$files = glob("$path/$singlePattern", $flags);
$dirs = glob("$path/*", GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);
foreach ($dirs as $dir) {
$files = array_merge($files, self::rglob("$dir/$singlePattern", $flags));
}
if ($onlyFiles) {
$files = array_filter($files, function($v) { return is_dir($v) ? false : true;});
}
return $files;
}
/**
* Returns the last version given a pattern of file name
*
* @param string $pattern a valid pattern for glob(...) native function
* @param int $flag php flags for glob(...) native function
* @return int|string
*
* Example:
* - Given the following files inside a directory:
* /example/path/myApplication-v1.tar
* /example/path/myApplication-v2.tar
* /example/path/myApplication-v3.tar
* /example/path/myApplication-v5.tar
* /example/path/myApplication-v7.tar
*
* $lastVer = ProcessMaker\Util\Common::getLastVersion("/example/path/myApplication-*.tar");
*
* It will returns: 7
*/
public static function getLastVersion($pattern, $flag = 0)
{
$files = glob($pattern, $flag);
$maxVersion = 0;
$pattern = str_replace("*", '([0-9\.]+)', basename($pattern));
foreach ($files as $file) {
$filename = basename($file);
if (preg_match('/'.$pattern.'/', $filename, $match)) {
if ($maxVersion < $match[1]) {
$maxVersion = $match[1];
}
}
}
return $maxVersion;
}
public static function parseIniFile($filename)
{
$data = @parse_ini_file($filename, true);
$result = array();
if ($data === false) {
throw new \Exception("Error parsing ini file: $filename");
}
foreach ($data as $key => $value) {
if (strpos($key, ':') !== false) {
list($key, $subSection) = explode(':', $key);
$result[trim($key)][trim($subSection)] = $value;
} else {
$result[$key] = $value;
}
}
return $result;
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace Maveriks\Util;
/**
* Singleton Class Logger
*
* This Utility is useful to log local messages
* @package ProcessMaker\Util
* @author Erik Amaru Ortiz <aortiz.erik@gmail.com, erik@colosa.com>
*/
class Logger
{
private static $instance;
private $logFile;
private $fp;
protected function __construct()
{
$this->logFile = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'processmaker.log';
if (! file_exists($this->logFile)) {
if (! touch($this->logFile)) {
error_log("ProcessMaker Log file can't be created!");
}
chmod($this->logFile, 0777);
}
$this->fp = fopen($this->logFile, "a+");
}
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new Logger();
}
return self::$instance;
}
public function setLogLine()
{
$args = func_get_args();
$this->setLog(date('Y-m-d H:i:s') . " ");
foreach ($args as $str) {
$this->setLog((is_string($str) ? $str : var_export($str, true)) . PHP_EOL);
}
}
public function setLogInline()
{
$args = func_get_args();
$this->setLog(date('Y-m-d H:i:s') . " ");
foreach ($args as $str) {
$this->setLog((is_string($str) ? $str : var_export($str, true)) . " ");
}
}
public function setLog($str)
{
fwrite($this->fp, $str);
}
public static function log()
{
$me = Logger::getInstance();
$args = func_get_args();
call_user_func_array(array($me, 'setLogLine'), $args);
}
}

View File

@@ -0,0 +1,359 @@
<?php
namespace Maveriks;
use Maveriks\Util;
class WebApplication
{
protected $rootDir = "";
protected $workflowDir = "";
protected $requestUri = "";
const RUNNING_WORKFLOW = "workflow.running";
const RUNNING_API = "api.running";
const SERVICE_API = "service.api";
public function __construct()
{
defined("DS") || define("DS", DIRECTORY_SEPARATOR);
}
/**
* @param string $rootDir
*/
public function setRootDir($rootDir)
{
$this->rootDir = $rootDir;
$this->workflowDir = $rootDir . DS . "workflow" . DS;
}
/**
* @return string
*/
public function getRootDir()
{
return $this->rootDir;
}
/**
* @param string $requestUri
*/
public function setRequestUri($requestUri)
{
$this->requestUri = $requestUri;
}
/**
* @return string
*/
public function getRequestUri()
{
return $this->requestUri;
}
public function route()
{
if (substr($this->requestUri, 1, 3) == "api") {
return self::RUNNING_API;
} else {
return self::RUNNING_WORKFLOW;
}
}
public function run($type = "")
{
switch ($type) {
case self::SERVICE_API:
$request = $this->parseApiRequestUri();
$this->loadEnvironment($request["workspace"]);
Util\Logger::log("API::Dispatching ".$_SERVER["REQUEST_METHOD"]." ".$request["uri"]);
$this->dispatchApiRequest($request["uri"], $request["version"]);
Util\Logger::log("API::End Dispatching ".$_SERVER["REQUEST_METHOD"]." ".$request["uri"]);
break;
}
}
/**
* This method dispatch rest/api service
*
* @author Erik Amaru Ortiz <erik@colosa.com>
*/
public function dispatchApiRequest($uri, $version = "1.0")
{
// to handle a request with "OPTIONS" method
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, HEADERS');
header('Access-Control-Allow-Headers: authorization, content-type');
header("Access-Control-Allow-Credentials", "false");
header('Access-Control-Max-Age: 60');
die();
}
/*
* Enable this header to allow "Cross Domain AJAX" requests;
* This works because processmaker is handling correctly requests with method 'OPTIONS'
* that automatically is sent by a client using XmlHttpRequest or similar.
*/
header('Access-Control-Allow-Origin: *');
// $servicesDir contains directory where Services Classes are allocated
$servicesDir = $this->workflowDir . 'engine' . DS . 'src' . DS . 'Services' . DS;
// $apiDir - contains directory to scan classes and add them to Restler
$apiDir = $servicesDir . 'Api' . DS;
// $apiIniFile - contains file name of api ini configuration
$apiIniFile = $servicesDir . DS . 'api.ini';
// $authenticationClass - contains the class name that validate the authentication for Restler
$authenticationClass = 'Services\\Api\\OAuth2\\Server';
// $pmOauthClientId - contains PM Local OAuth Id (Web Designer)
$pmOauthClientId = 'x-pm-local-client';
/*
* Load Api ini file for Rest Service
*/
$apiIniConf = array();
if (file_exists($apiIniFile)) {
$apiIniConf = Util\Common::parseIniFile($apiIniFile);
}
// Setting current workspace to Api class
\ProcessMaker\Services\Api::setWorkspace(SYS_SYS);
// TODO remove this setting on the future, it is not needed, but if it is not present is throwing a warning
//\Luracast\Restler\Format\HtmlFormat::$viewPath = $servicesDir . 'oauth2/views';
// create a new Restler instance
$rest = new \Luracast\Restler\Restler();
// setting api version to Restler
$rest->setAPIVersion($version);
// adding $authenticationClass to Restler
$rest->addAuthenticationClass($authenticationClass, '');
// Setting database connection source
list($host, $port) = strpos(DB_HOST, ':') !== false ? explode(':', DB_HOST) : array(DB_HOST, '');
$port = empty($port) ? '' : ";port=$port";
\Services\Api\OAuth2\Server::setDatabaseSource(DB_USER, DB_PASS, DB_ADAPTER.":host=$host;dbname=".DB_NAME.$port);
// Setting default OAuth Client id, for local PM Web Designer
\Services\Api\OAuth2\Server::setPmClientId($pmOauthClientId);
require_once $this->workflowDir . "engine/src/Extension/Restler/UploadFormat.php";
//require_once PATH_CORE
//$rest->setSupportedFormats('JsonFormat', 'XmlFormat', 'UploadFormat');
//$rest->setOverridingFormats('UploadFormat', 'JsonFormat', 'XmlFormat', 'HtmlFormat');
$rest->setOverridingFormats('JsonFormat', 'UploadFormat');
// Override $_SERVER['REQUEST_URI'] to Restler handles the current url correctly
$isPluginRequest = strpos($uri, '/plugin-') !== false ? true : false;
if ($isPluginRequest) {
$tmp = explode('/', $uri);
array_shift($tmp);
$tmp = array_shift($tmp);
$tmp = explode('-', $tmp);
$pluginName = $tmp[1];
$uri = str_replace('/plugin-'.$pluginName, '', $uri);
}
$_SERVER['REQUEST_URI'] = $uri;
if (! $isPluginRequest) { // if it is not a request for a plugin endpoint
// scan all api directory to find api classes
$classesList = Util\Common::rglob($apiDir . "/*");
foreach ($classesList as $classFile) {
if (pathinfo($classFile, PATHINFO_EXTENSION) === 'php') {
$namespace = '\\Services\\' . str_replace(
DIRECTORY_SEPARATOR,
'\\',
str_replace('.php', '', str_replace($servicesDir, '', $classFile))
);
//var_dump($namespace);
$rest->addAPIClass($namespace);
}
}
// adding aliases for Restler
if (array_key_exists('alias', $apiIniConf)) {
foreach ($apiIniConf['alias'] as $alias => $aliasData) {
if (is_array($aliasData)) {
foreach ($aliasData as $label => $namespace) {
$namespace = '\\' . ltrim($namespace, '\\');
$rest->addAPIClass($namespace, $alias);
}
}
}
}
} else {
// hook to get rest api classes from plugins
// if (class_exists('PMPluginRegistry')) {
// $pluginRegistry = & PMPluginRegistry::getSingleton();
// $plugins = $pluginRegistry->getRegisteredRestServices();
//
// if (is_array($plugins) && array_key_exists($pluginName, $plugins)) {
// foreach ($plugins[$pluginName] as $class) {
// $rest->addAPIClass($class['namespace']);
// }
// }
// }
}
$rest->handle();
}
public function parseApiRequestUri()
{
$url = explode("/", $this->requestUri);
array_shift($url);
array_shift($url);
$version = array_shift($url);
$workspace = array_shift($url);
$restUri = "";
foreach ($url as $urlPart) {
$restUri .= "/" . $urlPart;
}
return array(
"uri" => $restUri,
"version" => $version,
"workspace" => $workspace
);
}
public function loadEnvironment($workspace)
{
$lang = "en";
define('SYS_LANG', $lang);
define('PATH_SEP', DIRECTORY_SEPARATOR);
define('PATH_TRUNK', $this->rootDir . PATH_SEP);
define('PATH_OUTTRUNK', realpath($this->rootDir . '/../') . PATH_SEP);
define('PATH_HOME', $this->rootDir . PATH_SEP . 'workflow' . PATH_SEP);
define('PATH_HTML', PATH_HOME . 'public_html' . PATH_SEP);
define('PATH_RBAC_HOME', PATH_TRUNK . 'rbac' . PATH_SEP);
define('PATH_GULLIVER_HOME', PATH_TRUNK . 'gulliver' . PATH_SEP);
define('PATH_GULLIVER', PATH_GULLIVER_HOME . 'system' . PATH_SEP); //gulliver system classes
define('PATH_GULLIVER_BIN', PATH_GULLIVER_HOME . 'bin' . PATH_SEP); //gulliver bin classes
define('PATH_TEMPLATE', PATH_GULLIVER_HOME . 'templates' . PATH_SEP);
define('PATH_THIRDPARTY', PATH_GULLIVER_HOME . 'thirdparty' . PATH_SEP);
define('PATH_RBAC', PATH_RBAC_HOME . 'engine' . PATH_SEP . 'classes' . PATH_SEP); //to enable rbac version 2
define('PATH_RBAC_CORE', PATH_RBAC_HOME . 'engine' . PATH_SEP);
define('PATH_CORE', PATH_HOME . 'engine' . PATH_SEP);
define('PATH_SKINS', PATH_CORE . 'skins' . PATH_SEP);
define('PATH_SKIN_ENGINE', PATH_CORE . 'skinEngine' . PATH_SEP);
define('PATH_METHODS', PATH_CORE . 'methods' . PATH_SEP);
define('PATH_XMLFORM', PATH_CORE . 'xmlform' . PATH_SEP);
define('PATH_CONFIG', PATH_CORE . 'config' . PATH_SEP);
define('PATH_PLUGINS', PATH_CORE . 'plugins' . PATH_SEP);
define('PATH_HTMLMAIL', PATH_CORE . 'html_templates' . PATH_SEP);
define('PATH_TPL', PATH_CORE . 'templates' . PATH_SEP);
define('PATH_TEST', PATH_CORE . 'test' . PATH_SEP);
define('PATH_FIXTURES', PATH_TEST . 'fixtures' . PATH_SEP);
define('PATH_RTFDOCS', PATH_CORE . 'rtf_templates' . PATH_SEP);
define('PATH_DYNACONT', PATH_CORE . 'content' . PATH_SEP . 'dynaform' . PATH_SEP);
define('SYS_UPLOAD_PATH', PATH_HOME . "public_html/files/" );
define('PATH_UPLOAD', PATH_HTML . 'files' . PATH_SEP);
define('PATH_WORKFLOW_MYSQL_DATA', PATH_CORE . 'data' . PATH_SEP . 'mysql' . PATH_SEP);
define('PATH_RBAC_MYSQL_DATA', PATH_RBAC_CORE . 'data' . PATH_SEP . 'mysql' . PATH_SEP);
define('FILE_PATHS_INSTALLED', PATH_CORE . 'config' . PATH_SEP . 'paths_installed.php' );
define('PATH_WORKFLOW_MSSQL_DATA', PATH_CORE . 'data' . PATH_SEP . 'mssql' . PATH_SEP);
define('PATH_RBAC_MSSQL_DATA', PATH_RBAC_CORE . 'data' . PATH_SEP . 'mssql' . PATH_SEP);
define('PATH_CONTROLLERS', PATH_CORE . 'controllers' . PATH_SEP);
define('PATH_SERVICES_REST', PATH_CORE . 'services' . PATH_SEP . 'rest' . PATH_SEP);
require_once PATH_GULLIVER . PATH_SEP . 'class.bootstrap.php';
spl_autoload_register(array("Bootstrap", "autoloadClass"));
\Bootstrap::registerClass("G", PATH_GULLIVER . "class.g.php");
\Bootstrap::registerClass("System", PATH_HOME . "engine/classes/class.system.php");
// define autoloading for others
\Bootstrap::registerClass("wsBase", PATH_HOME . "engine/classes/class.wsBase.php");
\Bootstrap::registerClass('Xml_Node', PATH_GULLIVER . "class.xmlDocument.php");
\Bootstrap::registerClass('XmlForm_Field_TextPM', PATH_HOME . "engine/classes/class.XmlForm_Field_TextPM.php");
\Bootstrap::registerClass('XmlForm_Field_SimpleText', PATH_GULLIVER . "class.xmlformExtension.php");
\Bootstrap::registerClass('XmlForm_Field', PATH_GULLIVER . "class.xmlform.php");
//\Bootstrap::registerDir('model', PATH_CORE . 'classes' . PATH_SEP . 'model');
//\Bootstrap::registerDir('rbac/model', PATH_RBAC_HOME . 'engine' . PATH_SEP . 'classes' . PATH_SEP . 'model');
//\Bootstrap::LoadThirdParty("smarty/libs", "Smarty.class");
\Bootstrap::registerSystemClasses();
$config = \System::getSystemConfiguration();
define('DEBUG_SQL_LOG', $config['debug_sql']);
define('DEBUG_TIME_LOG', $config['debug_time']);
define('DEBUG_CALENDAR_LOG', $config['debug_calendar']);
define('MEMCACHED_ENABLED', $config['memcached']);
define('MEMCACHED_SERVER', $config['memcached_server']);
define('TIME_ZONE', $config['time_zone']);
// set include path
set_include_path(
PATH_CORE . PATH_SEPARATOR .
PATH_THIRDPARTY . PATH_SEPARATOR .
PATH_THIRDPARTY . 'pear' . PATH_SEPARATOR .
PATH_RBAC_CORE . PATH_SEPARATOR .
get_include_path()
);
/*
* Setting Up Workspace
*/
// include the server installed configuration
require_once FILE_PATHS_INSTALLED;
define('SYS_SYS', $workspace);
// defining system constant when a valid server environment exists
define( 'PATH_LANGUAGECONT', PATH_DATA . "META-INF" . PATH_SEP );
define( 'PATH_CUSTOM_SKINS', PATH_DATA . 'skins' . PATH_SEP );
define( 'PATH_TEMPORAL', PATH_C . 'dynEditor/' );
define( 'PATH_DB', PATH_DATA . 'sites' . PATH_SEP );
$workspaceDir = PATH_DB . $workspace;
// smarty constants
define( 'PATH_SMARTY_C', PATH_C . 'smarty' . PATH_SEP . 'c' );
define( 'PATH_SMARTY_CACHE', PATH_C . 'smarty' . PATH_SEP . 'cache' );
//***************** PM Paths DATA **************************
define('PATH_DATA_SITE', PATH_DATA . 'sites/' . SYS_SYS . '/');
define('PATH_DOCUMENT', PATH_DATA_SITE . 'files/');
define('PATH_DATA_MAILTEMPLATES', PATH_DATA_SITE . 'mailTemplates/');
define('PATH_DATA_PUBLIC', PATH_DATA_SITE . 'public/');
define('PATH_DATA_REPORTS', PATH_DATA_SITE . 'reports/');
define('PATH_DYNAFORM', PATH_DATA_SITE . 'xmlForms/');
define('PATH_IMAGES_ENVIRONMENT_FILES', PATH_DATA_SITE . 'usersFiles' . PATH_SEP);
define('PATH_IMAGES_ENVIRONMENT_USERS', PATH_DATA_SITE . 'usersPhotographies' . PATH_SEP);
if (is_file(PATH_DATA_SITE.PATH_SEP . '.server_info')) {
$SERVER_INFO = file_get_contents(PATH_DATA_SITE.PATH_SEP.'.server_info');
$SERVER_INFO = unserialize($SERVER_INFO);
define('SERVER_NAME', $SERVER_INFO ['SERVER_NAME']);
define('SERVER_PORT', $SERVER_INFO ['SERVER_PORT']);
} else {
echo "WARNING! No server info found!";
}
// create memcached singleton
\Bootstrap::LoadClass('memcached');
//$memcache = PMmemcached::getSingleton( SYS_SYS );
\Propel::init(PATH_CONFIG . "databases.php");
}
}

View File

@@ -0,0 +1,88 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="robots" content="noindex,nofollow" />
<title>Runtime Exception.</title>
<style>
/* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
html{ color:#000;background:#FFF; }body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{ margin:0;padding:0; }table{ border-collapse:collapse;border-spacing:0; }fieldset,img{ border:0; }address,caption,cite,code,dfn,em,strong,th,var{ font-style:normal;font-weight:normal; }li{ list-style:none; }caption,th{ text-align:left; }h1,h2,h3,h4,h5,h6{ font-size:100%;font-weight:normal; }q:before,q:after{ content:''; }abbr,acronym{ border:0;font-variant:normal; }sup{ vertical-align:text-top; }sub{ vertical-align:text-bottom; }input,textarea,select{ font-family:inherit;font-size:inherit;font-weight:inherit; }input,textarea,select{ *font-size:100%; }legend{ color:#000; }
html { background: #eee; padding: 10px }
body { font: 11px Verdana, Arial, sans-serif; color: #333 }
img { border: 0; }
.clear { clear:both; height:0; font-size:0; line-height:0; }
.clear_fix:after { display:block; height:0; clear:both; visibility:hidden; }
.clear_fix { display:inline-block; }
* html .clear_fix { height:1%; }
.clear_fix { display:block; }
#content { width:970px; margin:0 auto; }
.sf-exceptionreset, .sf-exceptionreset .block { margin: auto }
.sf-exceptionreset abbr { border-bottom: 1px dotted #000; cursor: help; }
.sf-exceptionreset p { font-size:14px; line-height:20px; color:#868686; padding-bottom:20px }
.sf-exceptionreset strong { font-weight:bold; }
.sf-exceptionreset a { color:#6c6159; }
.sf-exceptionreset a img { border:none; }
.sf-exceptionreset a:hover { text-decoration:underline; }
.sf-exceptionreset em { font-style:italic; }
.sf-exceptionreset h1, .sf-exceptionreset h2 { font: 20px Georgia, "Times New Roman", Times, serif }
.sf-exceptionreset h2 span { background-color: #fff; color: #333; padding: 6px; float: left; margin-right: 10px; }
.sf-exceptionreset .traces li { font-size:12px; padding: 2px 4px; list-style-type:decimal; margin-left:20px; }
.sf-exceptionreset .block { background-color:#FFFFFF; padding:10px 28px; margin-bottom:20px;
-webkit-border-bottom-right-radius: 16px;
-webkit-border-bottom-left-radius: 16px;
-moz-border-radius-bottomright: 16px;
-moz-border-radius-bottomleft: 16px;
border-bottom-right-radius: 16px;
border-bottom-left-radius: 16px;
border-bottom:1px solid #ccc;
border-right:1px solid #ccc;
border-left:1px solid #ccc;
}
.sf-exceptionreset .block_exception { background-color:#ddd; color: #333; padding:20px;
-webkit-border-top-left-radius: 16px;
-webkit-border-top-right-radius: 16px;
-moz-border-radius-topleft: 16px;
-moz-border-radius-topright: 16px;
border-top-left-radius: 16px;
border-top-right-radius: 16px;
border-top:1px solid #ccc;
border-right:1px solid #ccc;
border-left:1px solid #ccc;
}
.sf-exceptionreset li a { background:none; color:#868686; text-decoration:none; }
.sf-exceptionreset li a:hover { background:none; color:#313131; text-decoration:underline; }
.sf-exceptionreset ol { padding: 10px 0; }
.sf-exceptionreset h1 { background-color:#FFFFFF; padding: 15px 28px; margin-bottom: 20px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="content" class="sf-exceptionreset">
<h1>System Exception.</h1>
<div class="block_exception clear_fix">
<h2>
<abbr title="RuntimeException">RuntimeException</abbr>:
<?php echo $message?>
</h2>
</div>
<div class="block">
<ol class="traces list_exception">
<?php foreach ($exception->getTrace() as $line ) {
if (isset($line['class'])) {
echo '<li>At ' . $line['class'].$line['type'].$line['function']
. '()</b> <br/>in '.$line['file'].' line '.$line['line'].'</li>';
} else{
echo '<li>At ' . $line['function'] . '() in '.$line['file'].' line '.$line['line'].'</li>';
}
} ?>
</ol>
</div>
</div>
</body>
</html>

View File

@@ -1,157 +0,0 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
/**
* SplClassLoader implementation that implements the technical interoperability
* standards for PHP 5.3 namespaces and class names.
*
* http://groups.google.com/group/php-standards/web/psr-0-final-proposal?pli=1
*
* // Example which loads classes for the Doctrine Common package in the
* // Doctrine\Common namespace.
* $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
* $classLoader->register();
*
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman S. Borschel <roman@code-factory.org>
* @author Matthew Weier O'Phinney <matthew@zend.com>
* @author Kris Wallsmith <kris.wallsmith@gmail.com>
* @author Fabien Potencier <fabien.potencier@symfony-project.org>
*/
class SplClassLoader
{
private $_fileExtension = '.php';
private $_namespace;
private $_includePath;
private $_namespaceSeparator = '\\';
/**
* Creates a new <tt>SplClassLoader</tt> that loads classes of the
* specified namespace.
*
* @param string $ns The namespace to use.
*/
public function __construct($ns = null, $includePath = null)
{
$this->_namespace = $ns;
$this->_includePath = $includePath;
}
/**
* Sets the namespace separator used by classes in the namespace of this class loader.
*
* @param string $sep The separator to use.
*/
public function setNamespaceSeparator($sep)
{
$this->_namespaceSeparator = $sep;
}
/**
* Gets the namespace seperator used by classes in the namespace of this class loader.
*
* @return string
*/
public function getNamespaceSeparator()
{
return $this->_namespaceSeparator;
}
/**
* Sets the base include path for all class files in the namespace of this class loader.
*
* @param string $includePath
*/
public function setIncludePath($includePath)
{
$this->_includePath = $includePath;
}
/**
* Gets the base include path for all class files in the namespace of this class loader.
*
* @return string $includePath
*/
public function getIncludePath()
{
return $this->_includePath;
}
/**
* Sets the file extension of class files in the namespace of this class loader.
*
* @param string $fileExtension
*/
public function setFileExtension($fileExtension)
{
$this->_fileExtension = $fileExtension;
}
/**
* Gets the file extension of class files in the namespace of this class loader.
*
* @return string $fileExtension
*/
public function getFileExtension()
{
return $this->_fileExtension;
}
/**
* Installs this class loader on the SPL autoload stack.
*/
public function register()
{
spl_autoload_register(array($this, 'loadClass'));
}
/**
* Uninstalls this class loader from the SPL autoloader stack.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $className The name of the class to load.
* @return void
*/
public function loadClass($className)
{
if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) {
$fileName = '';
$namespace = '';
if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
}
}
}

View File

@@ -0,0 +1,111 @@
<?php
namespace ProcessMaker\Core;
class ClassLoader
{
private static $includePath;
protected static $instance;
/**
* Creates a new <tt>SplClassLoader</tt> that loads classes of the
* specified namespace.
*
* @param string $ns The namespace to use.
*/
public function __construct()
{
defined("DS") || define("DS", DIRECTORY_SEPARATOR);
defined("NS") || define("NS", "\\");
spl_autoload_register(array($this, 'loadClass'));
}
/**
* @return \ProcessMaker\Core\ClassLoader
*/
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Gets the base include path for all class files in the namespace of this class loader.
*
* @return string $includePath
*/
public function getIncludePaths()
{
return self::$includePath;
}
/**
* Uninstalls this class loader from the SPL autoloader stack.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
public function add($namespace, $sourceDir)
{
if (empty($namespace)) {
self::$includePath[] = $sourceDir . (substr($sourceDir, -1) == DS ? "" : DS);
} else {
self::$includePath[$namespace] = $sourceDir . (substr($sourceDir, -1) == DS ? "" : DS);
}
}
function loadClass($className)
{
var_dump(self::$includePath); die;
$className = ltrim($className, NS);
foreach (self::$includePath as $path) {
if ($lastPos = strrpos($className, NS)) {
$namespace = substr($className, 0, $lastPos);
var_dump($namespace);
$className = substr($className, $lastPos + 1);
$subpath = str_replace(NS, DS, $namespace) . DS;
}
$filename = $path . $subpath . $className . ".php";
var_dump($filename);
if (file_exists($filename)) {
require $filename . ".php";
return true;
}
}
return false;
}
/**
* Loads the given class or interface.
*
* @param string $className The name of the class to load.
* @return void
*/
public function loadClass2($className)
{
if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) {
$fileName = '';
$namespace = '';
if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
}
}
}

View File

@@ -1,128 +1,6 @@
<?php
namespace ProcessMaker\Util;
class Common
class Common extends \Maveriks\Util\Common
{
/**
* Recursive version of glob php standard function
*
* @author Erik Amaru Ortiz <erik@colosa.com, aortiz.erik@gmail.com>
* @param string $pattern pattern for native glob function
* @param int|string $flags any valid flag for glob function
* @param bool $onlyFiles to filter return array with only matched files, or all matched results
* @return array array containing the recursive glob results
*
* Example:
*
* Common::rglob("/example/path/*");
*
* it will returns:
*
* Array
* (
* [0] => /example/path/README.txt
* [1] => /example/path/composer.json
* [4] => /example/path/one/one_text.txt
* [6] => /example/path/two/two_text.txt
* [7] => /example/path/two/two_one/two_one_text.txt
* [8] => /example/path/two/two_one/build.json
* )
*
* Example 2:
*
* Common::rglob("/example/path/*.json");
*
* It will returns:
*
* Array
* (
* [0] => /example/path/composer.json
* [1] => /example/path/two/two_one/build.json
* )
*/
public static function rglob($pattern, $flags = 0, $onlyFiles = false)
{
$singlePattern = basename($pattern);
if (strpos($singlePattern, "*") !== false) {
$path = rtrim(str_replace($singlePattern, "", $pattern), DIRECTORY_SEPARATOR);
} else {
$singlePattern = "";
$path = $pattern;
}
$files = glob("$path/$singlePattern", $flags);
$dirs = glob("$path/*", GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);
foreach ($dirs as $dir) {
$files = array_merge($files, self::rglob("$dir/$singlePattern", $flags));
}
if ($onlyFiles) {
$files = array_filter($files, function($v) { return is_dir($v) ? false : true;});
}
return $files;
}
/**
* Returns the last version given a pattern of file name
*
* @param string $pattern a valid pattern for glob(...) native function
* @param int $flag php flags for glob(...) native function
* @return int|string
*
* Example:
* - Given the following files inside a directory:
* /example/path/myApplication-v1.tar
* /example/path/myApplication-v2.tar
* /example/path/myApplication-v3.tar
* /example/path/myApplication-v5.tar
* /example/path/myApplication-v7.tar
*
* $lastVer = ProcessMaker\Util\Common::getLastVersion("/example/path/myApplication-*.tar");
*
* It will returns: 7
*/
public static function getLastVersion($pattern, $flag = 0)
{
$files = glob($pattern, $flag);
$maxVersion = 0;
$pattern = str_replace("*", '([0-9\.]+)', basename($pattern));
foreach ($files as $file) {
$filename = basename($file);
if (preg_match('/'.$pattern.'/', $filename, $match)) {
if ($maxVersion < $match[1]) {
$maxVersion = $match[1];
}
}
}
return $maxVersion;
}
public static function parseIniFile($filename)
{
$data = @parse_ini_file($filename, true);
$result = array();
if ($data === false) {
throw new \Exception("Error parsing ini file: $filename");
}
foreach ($data as $key => $value) {
if (strpos($key, ':') !== false) {
list($key, $subSection) = explode(':', $key);
$result[trim($key)][trim($subSection)] = $value;
} else {
$result[$key] = $value;
}
}
return $result;
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace ProcessMaker\Util;
/**
* Singleton Class Logger
*
* This Utility is useful to log local messages
* @package ProcessMaker\Util
* @author Erik Amaru Ortiz <aortiz.erik@gmail.com, erik@colosa.com>
*/
class Logger extends \Maveriks\Util\Logger
{
}

View File

@@ -30,13 +30,19 @@ try {
);
}
/** @var Composer\Autoload\ClassLoader $loader */
$loader = include $rootDir . "vendor" . DIRECTORY_SEPARATOR . "autoload.php";
$loader->add("", $rootDir . 'src/');
$loader->add("", $rootDir . 'workflow/engine/src/');
$loader->add("", $rootDir . 'workflow/engine/classes/model/');
require $rootDir . "framework/src/Maveriks/Util/ClassLoader.php";
$app = new ProcessMaker\WebApplication();
$loader = Maveriks\Util\ClassLoader::getInstance();
$loader->add($rootDir . 'framework/src/', "Maveriks");
$loader->add($rootDir . 'workflow/engine/src/', "ProcessMaker");
$loader->add($rootDir . 'workflow/engine/src/');
$loader->add($rootDir . 'workflow/engine/classes/model/');
// and vendors to autoloader
$loader->add($rootDir . 'vendor/luracast/restler/vendor', "Luracast");
$loader->add($rootDir . 'vendor/bshaffer/oauth2-server-php/src/', "OAuth2");
$app = new Maveriks\WebApplication();
$app->setRootDir($rootDir);
$app->setRequestUri($_SERVER['REQUEST_URI']);
@@ -44,14 +50,20 @@ try {
switch ($stat)
{
case ProcessMaker\WebApplication::RUNNING_WORKFLOW:
case Maveriks\WebApplication::RUNNING_WORKFLOW:
include "sysGeneric.php";
break;
case ProcessMaker\WebApplication::RUNNING_API:
$app->run(ProcessMaker\WebApplication::SERVICE_API);
case Maveriks\WebApplication::RUNNING_API:
$app->run(Maveriks\WebApplication::SERVICE_API);
break;
}
} catch (Exception $e) {
die($e->getMessage());
$view = new Maveriks\Pattern\Mvc\PhtmlView($rootDir . "framework/src/templates/Exception.phtml");
$view->set("message", $e->getMessage());
$view->set("exception", $e);
$response = new Maveriks\Http\Response($view->getOutput(), 503);
$response->send();
}

File diff suppressed because it is too large Load Diff