public function __construct($controller, $action, $format, array $args = null) { parent::__construct($format); //Sanitize names for consistent use $this->_controller = implode('.', array_map('Tale\\Util\\StringUtil::canonicalize', explode('.', $controller))); $this->_action = StringUtil::canonicalize($action); $this->_args = $args ? $args : []; }
public function setCanonicalNameFromName() { $name = null; $i = 0; do { $addOn = ++$i >= 2 ? "-{$i}" : ''; $name = StringUtil::canonicalize($this->name) . $addOn; } while ($this->getTable()->where(['canonicalName' => $name])->count() > 0); $this->canonicalName = $name; return $name; }
public static function dispatch(array $request = null) { $request = array_replace(['module' => null, 'controller' => self::DEFAULT_CONTROLLER, 'action' => self::DEFAULT_ACTION, 'id' => null, 'args' => [], 'format' => self::DEFAULT_FORMAT], $request ? $request : []); //Sanitize (e.g. you can pass [sS]ome[-_][cC]ontroller, we want some-controller) $request['controller'] = StringUtil::canonicalize($request['controller']); $request['action'] = StringUtil::canonicalize($request['action']); $request['format'] = strtolower($request['format']); if ($request['id']) { array_unshift($request['args'], $request['id']); } $className = self::getClassName($request['controller'], $request['module']); $methodName = self::getMethodName($request['action']); var_dump("DISPATCH {$className}->{$methodName}", $request); try { if (!class_exists($className) || !is_subclass_of($className, __CLASS__)) { throw new \Exception("Failed to dispatch controller: {$className} doesnt exist or is not an instance of " . __CLASS__); } if (!method_exists($className, $methodName)) { throw new \Exception("Failed to dispatch action: {$className} has no method {$methodName}"); } } catch (\Exception $e) { if ($request['controller'] === self::ERROR_CONTROLLER) { throw $e; } self::dispatchError('not-found', array_merge(['exception' => $e], $request)); return; } $controller = new $className($request); //Get controllers init* methods $initMethods = call_user_func([$className, 'getInitMethodNames']); //Call all init*-Methods $result = null; foreach ($initMethods as $method) { $result = call_user_func([$controller, $method]); if ($result) { break; } } //Call the action if (!$result) { $result = call_user_func_array([$controller, $methodName], $request['args']); } //Let the dev be able to change the format in the action $format = $controller->format; switch ($format) { default: case 'html': header('Content-Type: text/html; encoding=utf-8'); $view = $controller->controller . '/' . $controller->action; if (!empty($controller->module)) { $view = $controller->module . '/' . $view; } $path = $view . '.phtml'; break; case 'json': header('Content-Type: application/json; encoding=utf-8'); echo json_encode($result); break; case 'txt': header('Content-Type: text/plain; encoding=utf-8'); echo serialize($result); break; case 'xml': header('Content-Type: text/xml; encoding=utf-8'); $doc = new \DOMDocument('1.0', 'UTF-8'); $doc->formatOutput = true; $addNode = function (\DOMNode $node, $name, $value, $self) use($doc) { $type = gettype($value); $el = $doc->createElement($name); $typeAttr = $doc->createAttribute('type'); $typeAttr->value = $type; $el->appendChild($typeAttr); switch (strtolower($type)) { case 'null': $el->textContent = 'null'; break; case 'string': case 'int': case 'double': $el->textContent = (string) $value; break; case 'boolean': $el->textContent = $value ? 'true' : 'false'; break; case 'object': if ($value instanceof XmlSerializable) { $value = $value->xmlSerialize(); } else { $value = (array) $value; } case 'array': foreach ($value as $key => $val) { $self($el, $key, $val, $self); } break; case 'resource': $el->textContent = get_resource_type($value); break; } }; $addNode($doc, 'value', $result, $addNode); echo $doc->saveXML(); break; } }
public function renderView($path, array $args = null, $cacheHtml = false) { $renderView = function () use($path, $args) { $phtml = $this->fetchCached('views.' . StringUtil::canonicalize($path), function () use($path) { return $this->getConvertedContent(self::TYPE_VIEW, $path); }, $this->getOption('lifeTime')); if (!$phtml) { throw new \Exception("Failed to convert theme: Neither {$path} nor" . " any convertible file could be found"); } $dataUri = "{$this->_wrapperName}://data;{$phtml}"; $render = function ($__dataUri, $__args) { ob_start(); extract($__args); include $__dataUri; return ob_get_clean(); }; return $render($dataUri, $args ? $args : []); }; if (!$cacheHtml) { return $renderView(); } return $this->fetchCached('views.html.' . StringUtil::canonicalize($path), $renderView, $this->getOption('lifeTime')); }