FIX Update Restler API to 3.1.1 to fix error with php8

This commit is contained in:
Laurent Destailleur 2020-12-23 17:25:38 +01:00
parent b036641ded
commit fd8bfcfd3b
58 changed files with 437 additions and 298 deletions

View File

@ -229,11 +229,21 @@ Change content of file htdocs/includes/restler/framework/Luracast/Restler/explor
public static function getShortName($className) public static function getShortName($className)
{ {
// @CHANGE LDR // @CHANGE LDR
if (! is_string($className)) return; if (!is_string($className)) return;
//var_dump($className); //var_dump($className);
* Add line into Data/Text.php to complete function
public static function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
// @CHANGE LDR
if (!is_string($haystack)) return false;
PARSEDOWN PARSEDOWN
--------- ---------

View File

@ -707,7 +707,7 @@ class Documents extends DolibarrApi
if (!empty($createdirifnotexists)) { if (!empty($createdirifnotexists)) {
if (dol_mkdir($upload_dir) < 0) { // needed by products if (dol_mkdir($upload_dir) < 0) { // needed by products
throw new RestException(500, 'Error while trying to create directory.'); throw new RestException(500, 'Error while trying to create directory '.$upload_dir);
} }
} }

View File

@ -109,7 +109,7 @@ class Login
} }
// Generate token for user // Generate token for user
$token = dol_hash($login.uniqid().$conf->global->MAIN_API_KEY, 1); $token = dol_hash($login.uniqid().(empty($conf->global->MAIN_API_KEY)?'':$conf->global->MAIN_API_KEY), 1);
// We store API token into database // We store API token into database
$sql = "UPDATE ".MAIN_DB_PREFIX."user"; $sql = "UPDATE ".MAIN_DB_PREFIX."user";

View File

@ -13,7 +13,7 @@ use Luracast\Restler\iCache;
* @copyright 2013 Luracast * @copyright 2013 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class ApcCache implements iCache class ApcCache implements iCache
{ {

View File

@ -12,7 +12,7 @@ namespace Luracast\Restler {
* @subpackage helper * @subpackage helper
* @author Nick Lombard <github@jigsoft.co.za> * @author Nick Lombard <github@jigsoft.co.za>
* @copyright 2012 Luracast * @copyright 2012 Luracast
* @version 3.0.0rc6 *
*/ */
class AutoLoader class AutoLoader
{ {

View File

@ -1,4 +1,5 @@
<?php <?php
namespace Luracast\Restler; namespace Luracast\Restler;
use Exception; use Exception;
@ -14,7 +15,7 @@ use Luracast\Restler\Data\Text;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class CommentParser class CommentParser
{ {
@ -55,8 +56,8 @@ class CommentParser
* @var array annotations that support array value * @var array annotations that support array value
*/ */
public static $allowsArrayValue = array( public static $allowsArrayValue = array(
'choice' => true, 'choice' => true,
'select' => true, 'select' => true,
'properties' => true, 'properties' => true,
); );
@ -220,7 +221,7 @@ class CommentParser
*/ */
private function parseParam($param, array $value, array $embedded) private function parseParam($param, array $value, array $embedded)
{ {
$data = & $this->_data; $data = &$this->_data;
$allowMultiple = false; $allowMultiple = false;
switch ($param) { switch ($param) {
case 'param' : case 'param' :
@ -237,7 +238,7 @@ class CommentParser
$value = $this->formatReturn($value); $value = $this->formatReturn($value);
break; break;
case 'class' : case 'class' :
$data = & $data[$param]; $data = &$data[$param];
list ($param, $value) = $this->formatClass($value); list ($param, $value) = $this->formatClass($value);
break; break;
case 'access' : case 'access' :
@ -294,7 +295,7 @@ class CommentParser
+= $data[$param][self::$embeddedDataName]; += $data[$param][self::$embeddedDataName];
} }
if (!is_array($data[$param])) { if (!is_array($data[$param])) {
$data[$param] = array('description' => (string) $data[$param]); $data[$param] = array('description' => (string)$data[$param]);
} }
if (is_array($value)) { if (is_array($value)) {
$data[$param] = $value + $data[$param]; $data[$param] = $value + $data[$param];
@ -320,19 +321,24 @@ class CommentParser
$data['pattern'] = $matches[2]; $data['pattern'] = $matches[2];
} }
while (preg_match('/{@(\w+)\s?([^}]*)}/ms', $subject, $matches)) { while (preg_match('/{@(\w+)\s?([^}]*)}/ms', $subject, $matches)) {
$name = $matches[1];
$value = $matches[2];
$subject = str_replace($matches[0], '', $subject); $subject = str_replace($matches[0], '', $subject);
if ($matches[1] == 'pattern') { if ($name == 'pattern') {
throw new Exception('Inline pattern tag should follow {@pattern /REGEX_PATTERN_HERE/} format and can optionally include PCRE modifiers following the ending `/`'); throw new Exception('Inline pattern tag should follow {@pattern /REGEX_PATTERN_HERE/} format and can optionally include PCRE modifiers following the ending `/`');
} elseif (isset(static::$allowsArrayValue[$matches[1]])) { } elseif (isset(static::$allowsArrayValue[$name])) {
$matches[2] = explode(static::$arrayDelimiter, $matches[2]); $value = explode(static::$arrayDelimiter, $value);
} elseif ($matches[2] == 'true' || $matches[2] == 'false') { } elseif ($value == 'true' || $value == 'false') {
$matches[2] = $matches[2] == 'true'; $value = $value == 'true';
} elseif ($matches[2] == '') { } elseif ($value == '') {
$matches[2] = true; $value = true;
} elseif ($matches[1] == 'required') { } elseif ($name == 'required') {
$matches[2] = explode(static::$arrayDelimiter, $matches[2]); $value = explode(static::$arrayDelimiter, $value);
} }
$data[$matches[1]] = $matches[2]; if (defined('Luracast\\Restler\\UI\\HtmlForm::'.$name)) {
$value = constant($value);
}
$data[$name] = $value;
} }
while (preg_match(self::$embeddedDataPattern, $subject, $matches)) { while (preg_match(self::$embeddedDataPattern, $subject, $matches)) {
@ -340,9 +346,9 @@ class CommentParser
$str = $matches[self::$embeddedDataIndex]; $str = $matches[self::$embeddedDataIndex];
if (isset ($this->restler) if (isset ($this->restler)
&& self::$embeddedDataIndex > 1 && self::$embeddedDataIndex > 1
&& !empty ($matches[1]) && !empty ($name)
) { ) {
$extension = $matches[1]; $extension = $name;
$formatMap = $this->restler->getFormatMap(); $formatMap = $this->restler->getFormatMap();
if (isset ($formatMap[$extension])) { if (isset ($formatMap[$extension])) {
/** /**
@ -397,15 +403,15 @@ class CommentParser
{ {
$code = 500; $code = 500;
$exception = 'Exception'; $exception = 'Exception';
if(count($value)>1){ if (count($value) > 1) {
$v1 = $value[0]; $v1 = $value[0];
$v2 = $value[1]; $v2 = $value[1];
if(is_numeric($v1)){ if (is_numeric($v1)) {
$code = $v1; $code = $v1;
$exception = $v2; $exception = $v2;
array_shift($value); array_shift($value);
array_shift($value); array_shift($value);
} elseif(is_numeric($v2)){ } elseif (is_numeric($v2)) {
$code = $v2; $code = $v2;
$exception = $v1; $exception = $v1;
array_shift($value); array_shift($value);
@ -414,17 +420,17 @@ class CommentParser
$exception = $v1; $exception = $v1;
array_shift($value); array_shift($value);
} }
} elseif(count($value) && is_numeric($value[0])) { } elseif (count($value) && is_numeric($value[0])) {
$code = $value[0]; $code = $value[0];
array_shift($value); array_shift($value);
} }
$message = implode(' ', $value); $message = implode(' ', $value);
if(!isset(RestException::$codes[$code])){ if (!isset(RestException::$codes[$code])) {
$code = 500; $code = 500;
} elseif(empty($message)){ } elseif (empty($message)) {
$message = RestException::$codes[$code]; $message = RestException::$codes[$code];
} }
return compact('code','message','exception'); return compact('code', 'message', 'exception');
} }
private function formatClass(array $value) private function formatClass(array $value)

View File

@ -11,7 +11,7 @@ namespace Luracast\Restler;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Compose implements iCompose class Compose implements iCompose
{ {

View File

@ -11,7 +11,7 @@ namespace Luracast\Restler\Data;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class ApiMethodInfo extends ValueObject class ApiMethodInfo extends ValueObject
{ {

View File

@ -10,7 +10,7 @@ namespace Luracast\Restler\Data;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Arr class Arr
{ {

View File

@ -12,7 +12,7 @@ use Exception;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Invalid extends Exception class Invalid extends Exception
{ {

View File

@ -11,7 +11,7 @@ namespace Luracast\Restler\Data;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Obj class Obj
{ {

View File

@ -10,87 +10,91 @@ namespace Luracast\Restler\Data;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Text class Text
{ {
/** /**
* Given haystack contains the needle or not? * Given haystack contains the needle or not?
* *
* @param string $haystack * @param string $haystack
* @param string $needle * @param string $needle
* @param bool $caseSensitive * @param bool $caseSensitive
* *
* @return bool * @return bool
*/ */
public static function contains($haystack, $needle, $caseSensitive = true) public static function contains($haystack, $needle, $caseSensitive = true)
{ {
if (empty($needle)) if (empty($needle))
return true; return true;
return $caseSensitive return $caseSensitive
? strpos($haystack, $needle) !== false ? strpos($haystack, $needle) !== false
: stripos($haystack, $needle) !== false; : stripos($haystack, $needle) !== false;
} }
/** /**
* Given haystack begins with the needle or not? * Given haystack begins with the needle or not?
* *
* @param string $haystack * @param string $haystack
* @param string $needle * @param string $needle
* *
* @return bool * @return bool
*/ */
public static function beginsWith($haystack, $needle) public static function beginsWith($haystack, $needle)
{ {
$length = strlen($needle); $length = strlen($needle);
return (substr($haystack, 0, $length) === $needle); return (substr($haystack, 0, $length) === $needle);
} }
/** /**
* Given haystack ends with the needle or not? * Given haystack ends with the needle or not?
* *
* @param string $haystack * @param string $haystack
* @param string $needle * @param string $needle
* *
* @return bool * @return bool
*/ */
public static function endsWith($haystack, $needle) public static function endsWith($haystack, $needle)
{ {
$length = strlen($needle); $length = strlen($needle);
if ($length == 0) { if ($length == 0) {
return true; return true;
} }
return (substr($haystack, -$length) === $needle);
} // @CHANGE LDR
if (!is_string($haystack)) return false;
/** return (substr($haystack, -$length) === $needle);
* Convert camelCased or underscored string in to a title }
*
* @param string $name
* /**
* @return string * Convert camelCased or underscored string in to a title
*/ *
public static function title($name) * @param string $name
{ *
return * @return string
ucwords( */
preg_replace( public static function title($name)
array('/(?<=[^A-Z])([A-Z])/', '/(?<=[^0-9])([0-9])/', '/([_-])/', '/[^a-zA-Z0-9\s]|\s\s+/'), {
array(' $0', ' $0', ' ', ' '), return
$name ucwords(
) preg_replace(
); array('/(?<=[^A-Z])([A-Z])/', '/(?<=[^0-9])([0-9])/', '/([_-])/', '/[^a-zA-Z0-9\s]|\s\s+/'),
} array(' $0', ' $0', ' ', ' '),
$name
/** )
* Convert given string to be used as a slug or css class );
* }
* @param string $name
* @return string /**
*/ * Convert given string to be used as a slug or css class
public static function slug($name) *
{ * @param string $name
return preg_replace('/[^a-zA-Z]+/', '-', strtolower(strip_tags($name))); * @return string
} */
public static function slug($name)
{
return preg_replace('/[^a-zA-Z]+/', '-', strtolower(strip_tags($name)));
}
} }

View File

@ -15,7 +15,7 @@ use Luracast\Restler\Util;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class ValidationInfo implements iValueObject class ValidationInfo implements iValueObject
{ {

View File

@ -18,7 +18,7 @@ use Luracast\Restler\Util;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Validator implements iValidate class Validator implements iValidate
{ {

View File

@ -11,7 +11,7 @@ namespace Luracast\Restler\Data;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class ValueObject implements iValueObject class ValueObject implements iValueObject
{ {

View File

@ -10,7 +10,7 @@ namespace Luracast\Restler\Data;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
interface iValidate { interface iValidate {

View File

@ -11,7 +11,7 @@ namespace Luracast\Restler\Data;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
interface iValueObject interface iValueObject
{ {

View File

@ -15,7 +15,7 @@ use Luracast\Restler\Data\Validator;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Defaults class Defaults
{ {

View File

@ -9,7 +9,7 @@ namespace Luracast\Restler;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
use Closure; use Closure;

View File

@ -15,7 +15,7 @@ use Luracast\Restler\RestException;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class RateLimit implements iFilter, iUseAuthentication class RateLimit implements iFilter, iUseAuthentication
{ {

View File

@ -15,7 +15,7 @@ use ArrayAccess;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Flash implements ArrayAccess class Flash implements ArrayAccess
{ {

View File

@ -17,7 +17,7 @@ use ZendAmf\Parser\OutputStream;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class AmfFormat extends DependentFormat class AmfFormat extends DependentFormat
{ {

View File

@ -15,7 +15,7 @@ use Luracast\Restler\RestException;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class CsvFormat extends Format implements iDecodeStream class CsvFormat extends Format implements iDecodeStream
{ {

View File

@ -10,7 +10,7 @@ namespace Luracast\Restler\Format;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
abstract class Format implements iFormat abstract class Format implements iFormat
{ {

View File

@ -1,4 +1,5 @@
<?php <?php
namespace Luracast\Restler\Format; namespace Luracast\Restler\Format;
use Exception; use Exception;
@ -6,8 +7,8 @@ use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem; use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler; use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Engines\CompilerEngine; use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\View\Engines\EngineResolver; use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\View\Factory; use Illuminate\View\Factory;
use Illuminate\View\FileViewFinder; use Illuminate\View\FileViewFinder;
use Illuminate\View\View; use Illuminate\View\View;
@ -18,6 +19,10 @@ use Luracast\Restler\Restler;
use Luracast\Restler\Scope; use Luracast\Restler\Scope;
use Luracast\Restler\UI\Nav; use Luracast\Restler\UI\Nav;
use Luracast\Restler\Util; use Luracast\Restler\Util;
use Twig\Environment;
use Twig\Extension\DebugExtension;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFunction;
/** /**
* Html template format * Html template format
@ -29,10 +34,14 @@ use Luracast\Restler\Util;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class HtmlFormat extends DependentFormat class HtmlFormat extends DependentFormat
{ {
const BLADE = 'Illuminate\View\View';
const TWIG = 'Twig\Environment';
const MUSTACHE = 'Mustache_Engine';
public static $mime = 'text/html'; public static $mime = 'text/html';
public static $extension = 'html'; public static $extension = 'html';
public static $view; public static $view;
@ -84,11 +93,12 @@ class HtmlFormat extends DependentFormat
} }
} }
public function getDependencyMap(){ public function getDependencyMap()
{
return array( return array(
'Illuminate\View\View' => 'illuminate/view:4.2.*', self::BLADE => 'illuminate/view:^8',
'Twig_Environment' => 'twig/twig:v1.13.*', self::TWIG => 'twig/twig:^3',
'Mustache_Engine' => 'mustache/mustache:dev-master', self::MUSTACHE => 'mustache/mustache:dev-master',
); );
} }
@ -101,7 +111,7 @@ class HtmlFormat extends DependentFormat
$resolver->register('blade', function () use ($engine) { $resolver->register('blade', function () use ($engine) {
return $engine; return $engine;
}); });
$phpEngine = new PhpEngine(); $phpEngine = new PhpEngine($files);
$resolver->register('php', function () use ($phpEngine) { $resolver->register('php', function () use ($phpEngine) {
return $phpEngine; return $phpEngine;
}); });
@ -128,32 +138,43 @@ class HtmlFormat extends DependentFormat
return $view->render(); return $view->render();
} }
public static function twig(array $data, $debug = true) /**
* @param array|object $data
* @param bool $debug
*
* @return string
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public static function twig($data, $debug = true)
{ {
$loader = new \Twig_Loader_Filesystem(static::$viewPath); $loader = new FilesystemLoader(static::$viewPath);
$twig = new \Twig_Environment($loader, array( $twig = new Environment($loader, array(
'cache' => static::$cacheDirectory, 'cache' => is_string(static::$cacheDirectory)
'debug' => $debug, ? static::$cacheDirectory : false,
'debug' => $debug,
'use_strict_variables' => $debug, 'use_strict_variables' => $debug,
)); ));
if ($debug) if ($debug) {
$twig->addExtension(new \Twig_Extension_Debug()); $twig->addExtension(new DebugExtension());
}
$twig->addFunction( $twig->addFunction(
new \Twig_SimpleFunction( new TwigFunction(
'form', 'form',
'Luracast\Restler\UI\Forms::get', 'Luracast\Restler\UI\Forms::get',
array('is_safe' => array('html')) array('is_safe' => array('html'))
) )
); );
$twig->addFunction( $twig->addFunction(
new \Twig_SimpleFunction( new TwigFunction(
'form_key', 'form_key',
'Luracast\Restler\UI\Forms::key' 'Luracast\Restler\UI\Forms::key'
) )
); );
$twig->addFunction( $twig->addFunction(
new \Twig_SimpleFunction( new TwigFunction(
'nav', 'nav',
'Luracast\Restler\UI\Nav::get' 'Luracast\Restler\UI\Nav::get'
) )
@ -164,7 +185,7 @@ class HtmlFormat extends DependentFormat
isset(HtmlFormat::$data[$name]) && isset(HtmlFormat::$data[$name]) &&
is_callable(HtmlFormat::$data[$name]) is_callable(HtmlFormat::$data[$name])
) { ) {
return new \Twig_SimpleFunction( return new TwigFunction(
$name, $name,
HtmlFormat::$data[$name] HtmlFormat::$data[$name]
); );
@ -172,21 +193,35 @@ class HtmlFormat extends DependentFormat
return false; return false;
}); });
$template = $twig->loadTemplate(static::getViewFile()); $template = $twig->load(static::getViewFile());
return $template->render($data); return $template->render((array)$data);
} }
public static function handlebar(array $data, $debug = true) /**
* @param array|object $data
* @param bool $debug
*
* @return string
*/
public static function handlebar($data, $debug = true)
{ {
return static::mustache($data, $debug); return static::mustache($data, $debug);
} }
public static function mustache(array $data, $debug = true) /**
* @param array|object $data
* @param bool $debug
*
* @return string
*/
public static function mustache($data, $debug = true)
{ {
if (!isset($data['nav'])) $data = (array)$data;
if (!isset($data['nav'])) {
$data['nav'] = array_values(Nav::get()); $data['nav'] = array_values(Nav::get());
}
$options = array( $options = array(
'loader' => new \Mustache_Loader_FilesystemLoader( 'loader' => new \Mustache_Loader_FilesystemLoader(
static::$viewPath, static::$viewPath,
array('extension' => static::getViewExtension()) array('extension' => static::getViewExtension())
), ),
@ -200,16 +235,25 @@ class HtmlFormat extends DependentFormat
}, },
) )
); );
if (!$debug) if (!$debug) {
$options['cache'] = static::$cacheDirectory; $options['cache'] = static::$cacheDirectory;
}
$m = new \Mustache_Engine($options); $m = new \Mustache_Engine($options);
return $m->render(static::getViewFile(), $data); return $m->render(static::getViewFile(), $data);
} }
public static function php(array $data, $debug = true) /**
* @param array|object $data
* @param bool $debug
*
* @return string
* @throws RestException
*/
public static function php($data, $debug = true)
{ {
if (static::$view == 'debug') if (static::$view == 'debug') {
static::$viewPath = dirname(__DIR__) . '/views'; static::$viewPath = dirname(__DIR__) . '/views';
}
$view = static::getViewFile(true); $view = static::getViewFile(true);
if (!is_readable($view)) { if (!is_readable($view)) {
@ -222,22 +266,26 @@ class HtmlFormat extends DependentFormat
$path = static::$viewPath . DIRECTORY_SEPARATOR; $path = static::$viewPath . DIRECTORY_SEPARATOR;
$template = function ($view) use ($data, $path) { $template = function ($view) use ($data, $path) {
$data = (array)$data;
$form = function () { $form = function () {
return call_user_func_array( return call_user_func_array(
'Luracast\Restler\UI\Forms::get', 'Luracast\Restler\UI\Forms::get',
func_get_args() func_get_args()
); );
}; };
if (!isset($data['form'])) if (!isset($data['form'])) {
$data['form'] = $form; $data['form'] = $form;
}
$nav = function () { $nav = function () {
return call_user_func_array( return call_user_func_array(
'Luracast\Restler\UI\Nav::get', 'Luracast\Restler\UI\Nav::get',
func_get_args() func_get_args()
); );
}; };
if (!isset($data['nav'])) if (!isset($data['nav'])) {
$data['nav'] = $nav; $data['nav'] = $nav;
}
$_ = function () use ($data, $path) { $_ = function () use ($data, $path) {
extract($data); extract($data);
@ -254,7 +302,7 @@ class HtmlFormat extends DependentFormat
) { ) {
$str = ''; $str = '';
foreach ($arrays as $arr) { foreach ($arrays as $arr) {
extract($arr); extract((array)$arr);
$str .= include $file; $str .= include $file;
} }
return $str; return $str;
@ -264,15 +312,18 @@ class HtmlFormat extends DependentFormat
} }
break; break;
case 'if': case 'if':
if (count($args) < 2) if (count($args) < 2) {
$args[1] = ''; $args[1] = '';
if (count($args) < 3) }
if (count($args) < 3) {
$args[2] = ''; $args[2] = '';
}
return $args[0] ? $args[1] : $args[2]; return $args[0] ? $args[1] : $args[2];
break; break;
default: default:
if (isset($data[$task]) && is_callable($data[$task])) if (isset($data[$task]) && is_callable($data[$task])) {
return call_user_func_array($data[$task], $args); return call_user_func_array($data[$task], $args);
}
} }
return ''; return '';
}; };
@ -280,8 +331,9 @@ class HtmlFormat extends DependentFormat
return @include $view; return @include $view;
}; };
$value = $template($view); $value = $template($view);
if (is_string($value)) if (is_string($value)) {
return $value; return $value;
}
} }
/** /**
@ -294,8 +346,8 @@ class HtmlFormat extends DependentFormat
* Formatter has to make the encoded * Formatter has to make the encoded
* output more human readable * output more human readable
* *
* @throws \Exception
* @return string encoded string * @return string encoded string
* @throws \Exception
*/ */
public function encode($data, $humanReadable = false) public function encode($data, $humanReadable = false)
{ {
@ -317,9 +369,9 @@ class HtmlFormat extends DependentFormat
'response' => static::$convertResponseToArray 'response' => static::$convertResponseToArray
? Obj::toArray($data) ? Obj::toArray($data)
: $data, : $data,
'stages' => $this->restler->getEvents(), 'stages' => $this->restler->getEvents(),
'success' => $success, 'success' => $success,
'error' => $error 'error' => $error
); );
$info = $data['api'] = $this->restler->apiMethodInfo; $info = $data['api'] = $this->restler->apiMethodInfo;
$metadata = Util::nestedValue( $metadata = Util::nestedValue(
@ -358,7 +410,9 @@ class HtmlFormat extends DependentFormat
if ($value) { if ($value) {
$data = Util::nestedValue($data, explode('.', $value)); $data = Util::nestedValue($data, explode('.', $value));
} }
$data += static::$data; if (is_array($data)) {
$data += static::$data;
}
if (false === ($i = strrpos(self::$view, '.'))) { if (false === ($i = strrpos(self::$view, '.'))) {
$template = self::$template; $template = self::$template;
} else { } else {
@ -369,17 +423,18 @@ class HtmlFormat extends DependentFormat
static::$cacheDirectory = Defaults::$cacheDirectory . DIRECTORY_SEPARATOR . $template; static::$cacheDirectory = Defaults::$cacheDirectory . DIRECTORY_SEPARATOR . $template;
if (!file_exists(static::$cacheDirectory)) { if (!file_exists(static::$cacheDirectory)) {
if (!mkdir(static::$cacheDirectory, 0770, true)) { if (!mkdir(static::$cacheDirectory, 0770, true)) {
throw new RestException(500, 'Unable to create cache directory `' . static::$cacheDirectory . '`'); throw new RestException(500,
'Unable to create cache directory `' . static::$cacheDirectory . '`');
} }
} }
} }
if (method_exists($class = get_called_class(), $template)) { if (method_exists($class = get_called_class(), $template)) {
if ($template == 'blade') { if ($template == 'blade') {
$this->checkDependency('Illuminate\View\View'); $this->checkDependency(self::BLADE);
} elseif ($template == 'twig') { } elseif ($template == 'twig') {
$this->checkDependency('Twig_Environment'); $this->checkDependency(self::TWIG);
} elseif ($template == 'mustache' || $template == 'handlebar') { } elseif ($template == 'mustache' || $template == 'handlebar') {
$this->checkDependency('Mustache_Engine'); $this->checkDependency(self::MUSTACHE);
} }
return call_user_func("$class::$template", $data, $humanReadable); return call_user_func("$class::$template", $data, $humanReadable);
} }
@ -416,8 +471,9 @@ class HtmlFormat extends DependentFormat
{ {
$v = $fullPath ? static::$viewPath . '/' : ''; $v = $fullPath ? static::$viewPath . '/' : '';
$v .= static::$view; $v .= static::$view;
if ($includeExtension) if ($includeExtension) {
$v .= '.' . static::getViewExtension(); $v .= '.' . static::getViewExtension();
}
return $v; return $v;
} }

View File

@ -11,7 +11,7 @@ namespace Luracast\Restler\Format;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class JsFormat extends JsonFormat class JsFormat extends JsonFormat
{ {

View File

@ -15,7 +15,7 @@ use Luracast\Restler\RestException;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class JsonFormat extends Format class JsonFormat extends Format
{ {

View File

@ -9,7 +9,7 @@ namespace Luracast\Restler\Format;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
abstract class MultiFormat implements iFormat abstract class MultiFormat implements iFormat
{ {

View File

@ -18,7 +18,7 @@ use CFPropertyList\CFPropertyList;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class PlistFormat extends DependentMultiFormat class PlistFormat extends DependentMultiFormat
{ {

View File

@ -11,7 +11,7 @@ namespace Luracast\Restler\Format;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class TsvFormat extends CsvFormat class TsvFormat extends CsvFormat
{ {

View File

@ -13,7 +13,7 @@ use Luracast\Restler\RestException;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class UploadFormat extends Format class UploadFormat extends Format
{ {

View File

@ -11,7 +11,7 @@ namespace Luracast\Restler\Format;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class UrlEncodedFormat extends Format class UrlEncodedFormat extends Format
{ {

View File

@ -16,7 +16,7 @@ use XMLWriter;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class XmlFormat extends Format class XmlFormat extends Format
{ {

View File

@ -14,7 +14,7 @@ use Luracast\Restler\Data\Obj;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class YamlFormat extends DependentFormat class YamlFormat extends DependentFormat
{ {

View File

@ -11,7 +11,7 @@ namespace Luracast\Restler\Format;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
interface iDecodeStream interface iDecodeStream
{ {

View File

@ -11,7 +11,7 @@ namespace Luracast\Restler\Format;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
interface iFormat interface iFormat
{ {

View File

@ -10,7 +10,7 @@ namespace Luracast\Restler;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class HumanReadableCache implements iCache class HumanReadableCache implements iCache
{ {

View File

@ -12,7 +12,7 @@ namespace Luracast\Restler;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */

View File

@ -10,7 +10,7 @@ namespace Luracast\Restler;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class PassThrough class PassThrough
{ {

View File

@ -12,7 +12,7 @@ use Luracast\Restler\Format\JsonFormat;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Redirect class Redirect
{ {

View File

@ -15,7 +15,7 @@ use stdClass;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Resources implements iUseAuthentication, iProvideMultiVersionApi class Resources implements iUseAuthentication, iProvideMultiVersionApi
{ {

View File

@ -14,7 +14,7 @@ use Exception;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class RestException extends Exception class RestException extends Exception

View File

@ -15,13 +15,14 @@ use Luracast\Restler\Format\UrlEncodedFormat;
* inspired by the RestServer code from * inspired by the RestServer code from
* <http://jacwright.com/blog/resources/RestServer.txt> * <http://jacwright.com/blog/resources/RestServer.txt>
* *
*
* @category Framework * @category Framework
* @package Restler * @package Restler
* @author R.Arul Kumaran <arul@luracast.com> * @author R.Arul Kumaran <arul@luracast.com>
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
* *
* @method static void onGet() onGet(Callable $function) fired before reading the request details * @method static void onGet() onGet(Callable $function) fired before reading the request details
* @method static void onRoute() onRoute(Callable $function) fired before finding the api method * @method static void onRoute() onRoute(Callable $function) fired before finding the api method
@ -48,10 +49,13 @@ use Luracast\Restler\Format\UrlEncodedFormat;
* @method void onRespond() onRespond(Callable $function) fired before sending response * @method void onRespond() onRespond(Callable $function) fired before sending response
* @method void onComplete() onComplete(Callable $function) fired after sending response * @method void onComplete() onComplete(Callable $function) fired after sending response
* @method void onMessage() onMessage(Callable $function) fired before composing error response * @method void onMessage() onMessage(Callable $function) fired before composing error response
*
* @property bool|null _authenticated
* @property bool _authVerified
*/ */
class Restler extends EventDispatcher class Restler extends EventDispatcher
{ {
const VERSION = '3.0.0rc6'; const VERSION = '3.1.0';
// ================================================================== // ==================================================================
// //
@ -693,6 +697,8 @@ class Restler extends EventDispatcher
* - media type * - media type
* - charset * - charset
* - language * - language
*
* @throws RestException
*/ */
protected function negotiate() protected function negotiate()
{ {

View File

@ -17,7 +17,7 @@ use Exception;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Routes class Routes
{ {

View File

@ -1,4 +1,6 @@
<?php <?php
namespace Luracast\Restler; namespace Luracast\Restler;
/** /**
@ -11,7 +13,7 @@ namespace Luracast\Restler;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Scope class Scope
{ {
@ -45,8 +47,8 @@ class Scope
//API classes //API classes
'Resources' => 'Luracast\Restler\Resources', 'Resources' => 'Luracast\Restler\Resources',
'Explorer' => 'Luracast\Restler\Explorer\v2\Explorer', 'Explorer' => 'Luracast\Restler\Explorer\v2\Explorer',
'Explorer1' => 'Luracast\Restler\Explorer\v1\Explorer', 'Explorer1' => 'Luracast\Restler\Explorer\v1\Explorer',
'Explorer2' => 'Luracast\Restler\Explorer\v2\Explorer', 'Explorer2' => 'Luracast\Restler\Explorer\v2\Explorer',
//Cache classes //Cache classes
'HumanReadableCache' => 'Luracast\Restler\HumanReadableCache', 'HumanReadableCache' => 'Luracast\Restler\HumanReadableCache',
@ -120,6 +122,7 @@ class Scope
$r = new $fullName(); $r = new $fullName();
static::$instances[$name] = (object)array('instance' => $r); static::$instances[$name] = (object)array('instance' => $r);
if ($name != 'Restler') { if ($name != 'Restler') {
/** @var Restler restler */
$r->restler = static::get('Restler'); $r->restler = static::get('Restler');
$m = Util::nestedValue($r->restler, 'apiMethodInfo', 'metadata'); $m = Util::nestedValue($r->restler, 'apiMethodInfo', 'metadata');
if ($m) { if ($m) {
@ -138,15 +141,14 @@ class Scope
} }
if ( if (
$r instanceof iUseAuthentication && $r instanceof iUseAuthentication &&
static::get('Restler')->_authVerified && $r->restler && $r->restler->_authVerified &&
!isset(static::$instances[$name]->authVerified) !isset(static::$instances[$name]->authVerified)
) { ) {
static::$instances[$name]->authVerified = true; static::$instances[$name]->authVerified = true;
$r->__setAuthenticationStatus $r->__setAuthenticationStatus($r->restler->_authenticated);
(static::get('Restler')->_authenticated);
} }
if (isset(static::$instances[$name]->initPending)) { if (isset(static::$instances[$name]->initPending)) {
$m = Util::nestedValue(static::get('Restler'), 'apiMethodInfo', 'metadata'); $m = Util::nestedValue($r->restler, 'apiMethodInfo', 'metadata');
$fullName = $name; $fullName = $name;
if (class_exists($name)) { if (class_exists($name)) {
$shortName = Util::getShortName($name); $shortName = Util::getShortName($name);
@ -194,15 +196,15 @@ class Scope
*/ */
public static function resolve($className, array $scope) public static function resolve($className, array $scope)
{ {
if (empty($className) || !is_string($className)) if (empty($className) || !is_string($className)) {
return false; return false;
}
if (self::isPrimitiveDataType($className)) { if (self::isPrimitiveDataType($className)) {
return false; return false;
} }
$divider = '\\'; $divider = '\\';
$qualified = false;
if ($className[0] == $divider) { if ($className[0] == $divider) {
$qualified = trim($className, $divider); $qualified = trim($className, $divider);
} elseif (array_key_exists($className, $scope)) { } elseif (array_key_exists($className, $scope)) {
@ -210,18 +212,21 @@ class Scope
} else { } else {
$qualified = $scope['*'] . $className; $qualified = $scope['*'] . $className;
} }
if (class_exists($qualified)) if (class_exists($qualified)) {
return $qualified; return $qualified;
}
if (isset(static::$classAliases[$className])) { if (isset(static::$classAliases[$className])) {
$qualified = static::$classAliases[$className]; $qualified = static::$classAliases[$className];
if (class_exists($qualified)) if (class_exists($qualified)) {
return $qualified; return $qualified;
}
} }
return false; return false;
} }
/** /**
* @param string $stringName * @param string $stringName
*
* @return boolean * @return boolean
*/ */
private static function isPrimitiveDataType($stringName) private static function isPrimitiveDataType($stringName)

View File

@ -8,7 +8,7 @@ use Luracast\Restler\Util;
* Class Emmet * Class Emmet
* @package Luracast\Restler\UI * @package Luracast\Restler\UI
* *
* @version 3.0.0rc6 * @version 3.1.0
*/ */
class Emmet class Emmet
{ {

View File

@ -1,4 +1,5 @@
<?php <?php
namespace Luracast\Restler\UI; namespace Luracast\Restler\UI;
use Luracast\Restler\CommentParser; use Luracast\Restler\CommentParser;
@ -29,7 +30,7 @@ use Luracast\Restler\Util;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Forms implements iFilter class Forms implements iFilter
{ {
@ -38,7 +39,7 @@ class Forms implements iFilter
public static $excludedPaths = array(); public static $excludedPaths = array();
public static $style; private static $style;
/** /**
* @var bool should we fill up the form using given data? * @var bool should we fill up the form using given data?
*/ */
@ -81,6 +82,11 @@ class Forms implements iFilter
*/ */
private static $info; private static $info;
public static function setStyles(HtmlForm $style)
{
static::$style = get_class($style);
}
/** /**
* Get the form * Get the form
* *
@ -95,18 +101,20 @@ class Forms implements iFilter
* *
* @return array|T * @return array|T
* *
* @throws \Luracast\Restler\RestException * @throws RestException
*/ */
public static function get($method = 'POST', $action = null, $dataOnly = false, $prefix = '', $indent = ' ') public static function get($method = 'POST', $action = null, $dataOnly = false, $prefix = '', $indent = ' ')
{ {
if (!static::$style) if (!static::$style) {
static::$style = FormStyles::$html; static::$style = 'Luracast\\Restler\\UI\HtmlForm';
}
try { try {
/** @var Restler $restler */ /** @var Restler $restler */
$restler = Scope::get('Restler'); $restler = Scope::get('Restler');
if (is_null($action)) if (is_null($action)) {
$action = $restler->url; $action = $restler->url;
}
$info = $restler->url == $action $info = $restler->url == $action
&& Util::getRequestMethod() == $method && Util::getRequestMethod() == $method
@ -126,23 +134,25 @@ class Forms implements iFilter
//echo $e->getErrorMessage(); //echo $e->getErrorMessage();
$info = false; $info = false;
} }
if (!$info) if (!$info) {
throw new RestException(500, 'invalid action path for form `' . $method . ' ' . $action . '`'); throw new RestException(500, 'invalid action path for form `' . $method . ' ' . $action . '`');
}
static::$info = $info; static::$info = $info;
$m = $info->metadata; $m = $info->metadata;
$r = static::fields($dataOnly); $r = static::fields($dataOnly);
if ($method != 'GET' && $method != 'POST') { if ($method != 'GET' && $method != 'POST') {
if (empty(Defaults::$httpMethodOverrideProperty)) if (empty(Defaults::$httpMethodOverrideProperty)) {
throw new RestException( throw new RestException(
500, 500,
'Forms require `Defaults::\$httpMethodOverrideProperty`' . 'Forms require `Defaults::\$httpMethodOverrideProperty`' .
"for supporting HTTP $method" "for supporting HTTP $method"
); );
}
if ($dataOnly) { if ($dataOnly) {
$r[] = array( $r[] = array(
'tag' => 'input', 'tag' => 'input',
'name' => Defaults::$httpMethodOverrideProperty, 'name' => Defaults::$httpMethodOverrideProperty,
'type' => 'hidden', 'type' => 'hidden',
'value' => 'method', 'value' => 'method',
); );
} else { } else {
@ -158,9 +168,9 @@ class Forms implements iFilter
$form_key = static::key($method, $action); $form_key = static::key($method, $action);
if ($dataOnly) { if ($dataOnly) {
$r[] = array( $r[] = array(
'tag' => 'input', 'tag' => 'input',
'name' => static::FORM_KEY, 'name' => static::FORM_KEY,
'type' => 'hidden', 'type' => 'hidden',
'value' => 'hidden', 'value' => 'hidden',
); );
} else { } else {
@ -173,15 +183,16 @@ class Forms implements iFilter
} }
$s = array( $s = array(
'tag' => 'button', 'tag' => 'button',
'type' => 'submit', 'type' => 'submit',
'label' => 'label' =>
Util::nestedValue($m, 'return', CommentParser::$embeddedDataName, 'label') Util::nestedValue($m, 'return', CommentParser::$embeddedDataName, 'label')
? : 'Submit' ?: 'Submit'
); );
if (!$dataOnly) if (!$dataOnly) {
$s = Emmet::make(static::style('submit', $m), $s); $s = Emmet::make(static::style('submit', $m), $s);
}
$r[] = $s; $r[] = $s;
$t = array( $t = array(
'action' => $restler->getBaseUrl() . '/' . rtrim($action, '/'), 'action' => $restler->getBaseUrl() . '/' . rtrim($action, '/'),
@ -207,15 +218,18 @@ class Forms implements iFilter
public static function style($name, array $metadata, $type = '') public static function style($name, array $metadata, $type = '')
{ {
return isset($metadata[CommentParser::$embeddedDataName][$name]) if (isset($metadata[CommentParser::$embeddedDataName][$name])) {
? $metadata[CommentParser::$embeddedDataName][$name] return $metadata[CommentParser::$embeddedDataName][$name];
: (!empty($type) && isset(static::$style["$name-$type"]) }
? static::$style["$name-$type"] $style = static::$style . '::' . $name;
: (isset(static::$style[$name]) $typedStyle = $style . '_' . $type;
? static::$style[$name] if (defined($typedStyle)) {
: null return constant($typedStyle);
) }
); if (defined($style)) {
return constant($style);
}
return null;
} }
public static function fields($dataOnly = false) public static function fields($dataOnly = false)
@ -230,11 +244,13 @@ class Forms implements iFilter
is_scalar($value) || is_scalar($value) ||
($p['type'] == 'array' && is_array($value) && $value == array_values($value)) || ($p['type'] == 'array' && is_array($value) && $value == array_values($value)) ||
is_object($value) && $p['type'] == get_class($value) is_object($value) && $p['type'] == get_class($value)
) ) {
$p['value'] = $value; $p['value'] = $value;
}
static::$validationInfo = $v = new ValidationInfo($p); static::$validationInfo = $v = new ValidationInfo($p);
if ($v->from == 'path') if ($v->from == 'path') {
continue; continue;
}
if (!empty($v->children)) { if (!empty($v->children)) {
$t = Emmet::make(static::style('fieldset', $m), array('label' => $v->label)); $t = Emmet::make(static::style('fieldset', $m), array('label' => $v->label));
foreach ($v->children as $n => $c) { foreach ($v->children as $n => $c) {
@ -243,11 +259,13 @@ class Forms implements iFilter
is_scalar($value) || is_scalar($value) ||
($c['type'] == 'array' && is_array($value) && $value == array_values($value)) || ($c['type'] == 'array' && is_array($value) && $value == array_values($value)) ||
is_object($value) && $c['type'] == get_class($value) is_object($value) && $c['type'] == get_class($value)
) ) {
$c['value'] = $value; $c['value'] = $value;
}
static::$validationInfo = $vc = new ValidationInfo($c); static::$validationInfo = $vc = new ValidationInfo($c);
if ($vc->from == 'path') if ($vc->from == 'path') {
continue; continue;
}
$vc->name = $v->name . '[' . $vc->name . ']'; $vc->name = $v->name . '[' . $vc->name . ']';
$t [] = static::field($vc, $dataOnly); $t [] = static::field($vc, $dataOnly);
} }
@ -275,7 +293,7 @@ class Forms implements iFilter
//prevent XSS attacks //prevent XSS attacks
$p->value = htmlspecialchars($p->value, ENT_QUOTES | ENT_HTML401, 'UTF-8'); $p->value = htmlspecialchars($p->value, ENT_QUOTES | ENT_HTML401, 'UTF-8');
} }
$type = $p->field ? : static::guessFieldType($p); $type = $p->field ?: static::guessFieldType($p);
$tag = in_array($type, static::$inputTypes) $tag = in_array($type, static::$inputTypes)
? 'input' : $type; ? 'input' : $type;
$options = array(); $options = array();
@ -291,25 +309,33 @@ class Forms implements iFilter
$option['text'] = isset($p->rules['select'][$i]) $option['text'] = isset($p->rules['select'][$i])
? $p->rules['select'][$i] ? $p->rules['select'][$i]
: $choice; : $choice;
if ($choice == $p->value) if ($choice == $p->value) {
$option['selected'] = true; $option['selected'] = true;
}
$options[] = $option; $options[] = $option;
} }
} elseif ($p->type == 'boolean' || $p->type == 'bool') { } elseif ($p->type == 'boolean' || $p->type == 'bool') {
if (Text::beginsWith($type, 'radio') || Text::beginsWith($type, 'select')) { if (Text::beginsWith($type, 'radio') || Text::beginsWith($type, 'select')) {
$options[] = array('name' => $p->name, 'text' => ' Yes ', $options[] = array(
'value' => 'true'); 'name' => $p->name,
$options[] = array('name' => $p->name, 'text' => ' No ', 'text' => ' Yes ',
'value' => 'false'); 'value' => 'true'
if ($p->value || $p->default) );
$options[] = array(
'name' => $p->name,
'text' => ' No ',
'value' => 'false'
);
if ($p->value || $p->default) {
$options[0]['selected'] = true; $options[0]['selected'] = true;
}
} else { //checkbox } else { //checkbox
$r = array( $r = array(
'tag' => $tag, 'tag' => $tag,
'name' => $name, 'name' => $name,
'type' => $type, 'type' => $type,
'label' => $p->label, 'label' => $p->label,
'value' => 'true', 'value' => 'true',
'default' => $p->default, 'default' => $p->default,
); );
$r['text'] = 'Yes'; $r['text'] = 'Yes';
@ -323,13 +349,13 @@ class Forms implements iFilter
} }
if (empty($r)) { if (empty($r)) {
$r = array( $r = array(
'tag' => $tag, 'tag' => $tag,
'name' => $name, 'name' => $name,
'type' => $type, 'type' => $type,
'label' => $p->label, 'label' => $p->label,
'value' => $p->value, 'value' => $p->value,
'default' => $p->default, 'default' => $p->default,
'options' => & $options, 'options' => & $options,
'multiple' => $multiple, 'multiple' => $multiple,
); );
if (isset($p->rules)) { if (isset($p->rules)) {
@ -347,30 +373,36 @@ class Forms implements iFilter
$r['message'] = Validator::$exceptions[$p->name]->getMessage(); $r['message'] = Validator::$exceptions[$p->name]->getMessage();
} }
if (true === $p->required) if (true === $p->required) {
$r['required'] = 'required'; $r['required'] = 'required';
if (isset($p->rules['autofocus'])) }
if (isset($p->rules['autofocus'])) {
$r['autofocus'] = 'autofocus'; $r['autofocus'] = 'autofocus';
}
/* /*
echo "<pre>"; echo "<pre>";
print_r($r); print_r($r);
echo "</pre>"; echo "</pre>";
*/ */
if ($dataOnly) if ($dataOnly) {
return $r; return $r;
if (isset($p->rules['form'])) }
if (isset($p->rules['form'])) {
return Emmet::make($p->rules['form'], $r); return Emmet::make($p->rules['form'], $r);
}
$m = static::$info->metadata; $m = static::$info->metadata;
$t = Emmet::make(static::style($type, $m, $p->type) ? : static::style($tag, $m, $p->type), $r); $t = Emmet::make(static::style($type, $m, $p->type) ?: static::style($tag, $m, $p->type), $r);
return $t; return $t;
} }
protected static function guessFieldType(ValidationInfo $p, $type = 'type') protected static function guessFieldType(ValidationInfo $p, $type = 'type')
{ {
if (in_array($p->$type, static::$inputTypes)) if (in_array($p->$type, static::$inputTypes)) {
return $p->$type; return $p->$type;
if ($p->choice) }
if ($p->choice) {
return $p->type == 'array' ? 'checkbox' : 'select'; return $p->type == 'array' ? 'checkbox' : 'select';
}
switch ($p->$type) { switch ($p->$type) {
case 'boolean': case 'boolean':
return 'radio'; return 'radio';
@ -381,8 +413,9 @@ class Forms implements iFilter
case 'array': case 'array':
return static::guessFieldType($p, 'contentType'); return static::guessFieldType($p, 'contentType');
} }
if ($p->name == 'password') if ($p->name == 'password') {
return 'password'; return 'password';
}
return 'text'; return 'text';
} }
@ -397,11 +430,13 @@ class Forms implements iFilter
*/ */
public static function key($method = 'POST', $action = null) public static function key($method = 'POST', $action = null)
{ {
if (is_null($action)) if (is_null($action)) {
$action = Scope::get('Restler')->url; $action = Scope::get('Restler')->url;
}
$target = "$method $action"; $target = "$method $action";
if (empty(static::$key[$target])) if (empty(static::$key[$target])) {
static::$key[$target] = md5($target . User::getIpAddress() . uniqid(mt_rand())); static::$key[$target] = md5($target . User::getIpAddress() . uniqid(mt_rand()));
}
$_SESSION[static::FORM_KEY] = static::$key; $_SESSION[static::FORM_KEY] = static::$key;
return static::$key[$target]; return static::$key[$target];
} }
@ -425,8 +460,9 @@ class Forms implements iFilter
$url = $restler->url; $url = $restler->url;
foreach (static::$excludedPaths as $exclude) { foreach (static::$excludedPaths as $exclude) {
if (empty($exclude)) { if (empty($exclude)) {
if ($url == $exclude) if ($url == $exclude) {
return true; return true;
}
} elseif (Text::beginsWith($url, $exclude)) { } elseif (Text::beginsWith($url, $exclude)) {
return true; return true;
} }
@ -447,4 +483,4 @@ class Forms implements iFilter
} }
return true; return true;
} }
} }

View File

@ -19,7 +19,7 @@ use Luracast\Restler\Util;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Nav class Nav
{ {
@ -130,7 +130,8 @@ class Nav
} else { } else {
return $tree; return $tree;
} }
return null; $value = null;
return $value;
} }
public static function addUrls(array $urls) public static function addUrls(array $urls)

View File

@ -14,7 +14,7 @@ use Luracast\Restler\Util;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
* *
* ============================ magic properties ============================== * ============================ magic properties ==============================
* @property Tags parent parent tag * @property Tags parent parent tag

View File

@ -11,7 +11,7 @@ namespace Luracast\Restler;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class User implements iIdentifyUser class User implements iIdentifyUser
{ {

View File

@ -1,5 +1,7 @@
<?php <?php
namespace Luracast\Restler; namespace Luracast\Restler;
/** /**
* Describe the purpose of this class/interface/trait * Describe the purpose of this class/interface/trait
* *
@ -9,7 +11,7 @@ namespace Luracast\Restler;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
class Util class Util
{ {
@ -50,8 +52,8 @@ class Util
* When the deeply nested property is found its value is returned, otherwise * When the deeply nested property is found its value is returned, otherwise
* false is returned. * false is returned.
* *
* @param array $from array to extract the value from * @param array|object $from array to extract the value from
* @param string|array $key ... pass more to go deeply inside the array * @param string|array $key ... pass more to go deeply inside the array
* alternatively you can pass a single array * alternatively you can pass a single array
* *
* @return null|mixed null when not found, value otherwise * @return null|mixed null when not found, value otherwise
@ -77,24 +79,29 @@ class Util
return $from; return $from;
} }
public static function getResourcePath($className, public static function getResourcePath(
$resourcePath = null, $className,
$prefix = '') $resourcePath = null,
{ $prefix = ''
) {
if (is_null($resourcePath)) { if (is_null($resourcePath)) {
if (Defaults::$autoRoutingEnabled) { if (Defaults::$autoRoutingEnabled) {
$resourcePath = strtolower($className); $resourcePath = strtolower($className);
if (false !== ($index = strrpos($className, '\\'))) if (false !== ($index = strrpos($className, '\\'))) {
$resourcePath = substr($resourcePath, $index + 1); $resourcePath = substr($resourcePath, $index + 1);
if (false !== ($index = strrpos($resourcePath, '_'))) }
if (false !== ($index = strrpos($resourcePath, '_'))) {
$resourcePath = substr($resourcePath, $index + 1); $resourcePath = substr($resourcePath, $index + 1);
}
} else { } else {
$resourcePath = ''; $resourcePath = '';
} }
} else } else {
$resourcePath = trim($resourcePath, '/'); $resourcePath = trim($resourcePath, '/');
if (strlen($resourcePath) > 0) }
if (strlen($resourcePath) > 0) {
$resourcePath .= '/'; $resourcePath .= '/';
}
return $prefix . $resourcePath; return $prefix . $resourcePath;
} }
@ -114,8 +121,9 @@ class Util
*/ */
public static function removeCommonPath($fromPath, $usingPath, $char = '/') public static function removeCommonPath($fromPath, $usingPath, $char = '/')
{ {
if (empty($fromPath)) if (empty($fromPath)) {
return ''; return '';
}
$fromPath = explode($char, $fromPath); $fromPath = explode($char, $fromPath);
$usingPath = explode($char, $usingPath); $usingPath = explode($char, $usingPath);
while (count($usingPath)) { while (count($usingPath)) {
@ -145,8 +153,9 @@ class Util
*/ */
public static function splitCommonPath($fromPath, $usingPath, $char = '/') public static function splitCommonPath($fromPath, $usingPath, $char = '/')
{ {
if (empty($fromPath)) if (empty($fromPath)) {
return array('', ''); return array('', '');
}
$fromPath = explode($char, $fromPath); $fromPath = explode($char, $fromPath);
$usingPath = explode($char, $usingPath); $usingPath = explode($char, $usingPath);
$commonPath = array(); $commonPath = array();
@ -213,11 +222,18 @@ class Util
$accepts = array($accepts); $accepts = array($accepts);
} }
foreach ($accepts as $pos => $accept) { foreach ($accepts as $pos => $accept) {
$parts = explode(';q=', trim($accept)); $parts = explode(';', $accept);
$type = array_shift($parts); $type = trim(array_shift($parts));
$quality = count($parts) ? $parameters = [];
floatval(array_shift($parts)) : foreach ($parts as $part) {
(1000 - $pos) / 1000; $part = explode('=', $part);
if (2 !== count($part)) {
continue;
}
$key = strtolower(trim($part[0]));
$parameters[$key] = trim($part[1], ' "');
}
$quality = isset($parameters['q']) ? (float)$parameters['q'] : (1000 - $pos) / 1000;
$acceptList[$type] = $quality; $acceptList[$type] = $quality;
} }
arsort($acceptList); arsort($acceptList);
@ -227,8 +243,7 @@ class Util
public static function getShortName($className) public static function getShortName($className)
{ {
// @CHANGE LDR // @CHANGE LDR
if (! is_string($className)) return ''; if (!is_string($className)) return;
//var_dump($className);
$className = explode('\\', $className); $className = explode('\\', $className);
return end($className); return end($className);

View File

@ -11,7 +11,7 @@ namespace Luracast\Restler;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
interface iAuthenticate extends iFilter interface iAuthenticate extends iFilter
{ {

View File

@ -9,7 +9,7 @@ namespace Luracast\Restler;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
interface iCache interface iCache
{ {

View File

@ -13,7 +13,7 @@ use Exception;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
interface iCompose { interface iCompose {
/** /**

View File

@ -12,7 +12,7 @@ namespace Luracast\Restler;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
interface iFilter interface iFilter
{ {

View File

@ -12,7 +12,7 @@ namespace Luracast\Restler;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
interface iIdentifyUser interface iIdentifyUser
{ {

View File

@ -5,7 +5,7 @@ namespace Luracast\Restler;
* Interface iProvideMultiVersionApi * Interface iProvideMultiVersionApi
* @package Luracast\Restler * @package Luracast\Restler
* *
* @version 3.0.0rc6 *
*/ */
interface iProvideMultiVersionApi interface iProvideMultiVersionApi
{ {

View File

@ -11,7 +11,7 @@ namespace Luracast\Restler;
* @copyright 2010 Luracast * @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/ * @link http://luracast.com/products/restler/
* @version 3.0.0rc6 *
*/ */
interface iUseAuthentication interface iUseAuthentication
{ {