function testDetectCicularReferenceInObject() { $context =& Piece_Unity_Context::singleton(); $foo =& new stdClass(); $bar =& new stdClass(); $baz =& new stdClass(); $foo->bar =& $bar; $bar->baz =& $baz; $baz->foo =& $foo; $viewElement =& $context->getViewElement(); $viewElement->setElementByRef('foo', $foo); $config =& new Piece_Unity_Config(); $config->setConfiguration('Renderer_JSON', 'contentType', 'text/javascript'); $config->setConfiguration('Renderer_JSON', 'include', array()); $config->setConfiguration('Renderer_JSON', 'exclude', array()); $config->setConfiguration('Renderer_JSON', 'useJSONP', true); $config->setConfiguration('Renderer_JSON', 'callbackKey', 'callback'); $context =& Piece_Unity_Context::singleton(); $context->setConfiguration($config); $renderer =& Piece_Unity_Plugin_Factory::factory('Renderer_JSON'); Piece_Unity_Error::disableCallback(); $renderer->invoke(); Piece_Unity_Error::enableCallback(); $this->assertTrue(Piece_Unity_Error::hasErrors()); $error = Piece_Unity_Error::pop(); $this->assertEquals(PIECE_UNITY_ERROR_UNEXPECTED_VALUE, $error['code']); $this->assertEquals(strtolower('_visitObject'), strtolower($error['context']['function'])); }
/** * Invokes the plugin specific code. * * @return string * @throws PIECE_UNITY_ERROR_NOT_FOUND * @throws PIECE_UNITY_ERROR_CANNOT_READ */ function invoke() { $eventName = $this->_context->getEventName(); if ($this->_getConfiguration('useDefaultEvent')) { if (is_null($eventName) || !strlen($eventName)) { $eventName = $this->_getConfiguration('defaultEventName'); $this->_context->setEventName($eventName); } } $class = str_replace('.', '', "{$eventName}Action"); $actionDirectory = $this->_getConfiguration('actionDirectory'); if (is_null($actionDirectory)) { return $eventName; } if (!Piece_Unity_ClassLoader::loaded($class)) { Piece_Unity_Error::disableCallback(); Piece_Unity_ClassLoader::load($class, $actionDirectory); Piece_Unity_Error::enableCallback(); if (Piece_Unity_Error::hasErrors()) { $error = Piece_Unity_Error::pop(); if ($error['code'] == PIECE_UNITY_ERROR_NOT_FOUND) { return $eventName; } Piece_Unity_Error::push(PIECE_UNITY_ERROR_CANNOT_READ, "Failed to read the action class [ {$class} ] for any reasons.", 'exception', array(), $error); return; } if (!Piece_Unity_ClassLoader::loaded($class)) { Piece_Unity_Error::push(PIECE_UNITY_ERROR_NOT_FOUND, "The class [ {$class} ] is not found in the loaded file."); return; } } $action =& new $class(); if (!method_exists($action, 'invoke')) { Piece_Unity_Error::push(PIECE_UNITY_ERROR_NOT_FOUND, "The method invoke() is not found in the class [ {$class} ]."); return; } $viewString = $action->invoke($this->_context); if (is_null($viewString)) { return $eventName; } else { return $viewString; } }
/** * Creates a plugin object from the plugin directories. * * @param string $pluginName * @return mixed * @throws PIECE_UNITY_ERROR_NOT_FOUND * @throws PIECE_UNITY_ERROR_INVALID_PLUGIN * @throws PIECE_UNITY_ERROR_CANNOT_READ */ function &factory($pluginName) { if (!array_key_exists($pluginName, $GLOBALS['PIECE_UNITY_Plugin_Instances'])) { $found = false; foreach ($GLOBALS['PIECE_UNITY_Plugin_Prefixes'] as $prefixAlias) { $pluginClass = Piece_Unity_Plugin_Factory::_getPluginClass($pluginName, $prefixAlias); if (Piece_Unity_ClassLoader::loaded($pluginClass)) { $found = true; break; } } if (!$found) { foreach ($GLOBALS['PIECE_UNITY_Plugin_Directories'] as $pluginDirectory) { foreach ($GLOBALS['PIECE_UNITY_Plugin_Prefixes'] as $prefixAlias) { $pluginClass = Piece_Unity_Plugin_Factory::_getPluginClass($pluginName, $prefixAlias); Piece_Unity_Error::disableCallback(); Piece_Unity_ClassLoader::load($pluginClass, $pluginDirectory); Piece_Unity_Error::enableCallback(); if (Piece_Unity_Error::hasErrors()) { $error = Piece_Unity_Error::pop(); if ($error['code'] == PIECE_UNITY_ERROR_NOT_FOUND) { continue; } Piece_Unity_Error::push(PIECE_UNITY_ERROR_CANNOT_READ, "Failed to read the plugin [ {$pluginName} ] for any reasons.", 'exception', array(), $error); $return = null; return $return; } if (Piece_Unity_ClassLoader::loaded($pluginClass)) { $found = true; break 2; } } } if (!$found) { Piece_Unity_Error::push(PIECE_UNITY_ERROR_NOT_FOUND, "The plugin [ {$pluginName} ] is not found in the following directories:\n" . implode("\n", $GLOBALS['PIECE_UNITY_Plugin_Directories'])); $return = null; return $return; } } $plugin =& new $pluginClass($prefixAlias); if (Piece_Unity_Error::hasErrors()) { $return = null; return $return; } if (!is_subclass_of($plugin, 'Piece_Unity_Plugin_Common')) { Piece_Unity_Error::push(PIECE_UNITY_ERROR_INVALID_PLUGIN, "The plugin [ {$pluginName} ] is invalid."); $return = null; return $return; } $GLOBALS['PIECE_UNITY_Plugin_Instances'][$pluginName] =& $plugin; } return $GLOBALS['PIECE_UNITY_Plugin_Instances'][$pluginName]; }
function testShouldAuthenticatePermissions() { $_SERVER['REQUEST_URI'] = '/wiki/delete.php'; $config =& new Piece_Unity_Config(); $config->setConfiguration('Authorization', 'permissions', array('^/wiki/.*' => 'WIKI_ADMIN')); $context =& Piece_Unity_Context::singleton(); $context->setConfiguration($config); $authorization =& Piece_Unity_Plugin_Factory::factory('Authorization'); $GLOBALS['PIECE_UNITY_Plugin_Interceptor_AuthorizationTestCase_hasPermission'] = true; $this->assertTrue($authorization->invoke()); $GLOBALS['PIECE_UNITY_Plugin_Interceptor_AuthorizationTestCase_hasPermission'] = false; Piece_Unity_Error::disableCallback(); $authorization->invoke(); Piece_Unity_Error::enableCallback(); $this->assertTrue(Piece_Unity_Error::hasErrors()); $error = Piece_Unity_Error::pop(); $this->assertEquals(PIECE_UNITY_ERROR_INVOCATION_FAILED, $error['code']); }
function testExceptionShouldBeRaisedIfControllerClassIsNotSpecified() { $context =& Piece_Unity_Context::singleton(); $context->setView("{$this->_target}ControllerShouldBeUsedIfUseControllerIsTrue"); $viewElement =& $context->getViewElement(); $viewElement->setElement('foo', 'BAR'); $config =& $this->_getConfig(); $config->setConfiguration('Renderer_Flexy', 'useController', true); $config->setConfiguration('Renderer_Flexy', 'controllerClass', 'Piece_Unity_Plugin_Renderer_FlexyTestCase_Controller'); $context->setConfiguration($config); Piece_Unity_Error::disableCallback(); $this->_render(); Piece_Unity_Error::enableCallback(); $this->assertTrue(Piece_Unity_Error::hasErrors()); $error = Piece_Unity_Error::pop(); $this->assertEquals(PIECE_UNITY_ERROR_INVOCATION_FAILED, $error['code']); }
/** * Renders a HTML. * If an error occured while rendering with a specified view and * useFallback is true, a fallback view will be rendered. * * @param boolean $isLayout * @throws PIECE_UNITY_ERROR_INVOCATION_FAILED */ function _render($isLayout) { Piece_Unity_Error::disableCallback(); $this->_doRender($isLayout); Piece_Unity_Error::enableCallback(); if (!Piece_Unity_Error::hasErrors()) { return; } $error = Piece_Unity_Error::pop(); if ($error['code'] == 'PIECE_UNITY_PLUGIN_RENDERER_HTML_ERROR_NOT_FOUND') { trigger_error("Failed to render a HTML template with the plugin [ {$this->_name} ].", E_USER_WARNING); var_dump($this->_context->getView()); if ($this->_getConfiguration('useFallback')) { $this->_context->setView($this->_getConfiguration('fallbackView')); $this->_prepareFallback(); $this->_doRender($isLayout); return; } } else { Piece_Unity_Error::push(PIECE_UNITY_ERROR_INVOCATION_FAILED, "Failed to render a HTML template with the plugin [ {$this->_name} ].", 'exception', array(), $error); } }
/** * Renders a HTML or HTML fragment. * * @param string $file * @param Piece_Unity_ViewElement &$viewElement * @throws PIECE_UNITY_ERROR_NOT_FOUND * @throws PIECE_UNITY_ERROR_INVOCATION_FAILED */ function render($file, &$viewElement) { $this->_loadSmarty(); $smarty =& new Smarty(); foreach ($this->_smartyClassVariables as $key => $value) { if ($key == 'plugins_dir' && is_array($value)) { $oldPluginDirectories = $smarty->plugins_dir; $smarty->plugins_dir = array_merge($value, $oldPluginDirectories); continue; } if (!is_null($value)) { $smarty->{$key} = $this->_adjustEndingSlash($value); } else { $smarty->{$key} = null; } } $viewElements = $viewElement->getElements(); foreach (array_keys($viewElements) as $elementName) { $smarty->assign_by_ref($elementName, $viewElements[$elementName]); } set_error_handler(array(__CLASS__, 'pushPHPError')); Piece_Unity_Error::disableCallback(); $smarty->display($file); Piece_Unity_Error::enableCallback(); restore_error_handler(); if (Piece_Unity_Error::hasErrors()) { $error = Piece_Unity_Error::pop(); if ($error['code'] == PIECE_UNITY_ERROR_PHP_ERROR && array_key_exists('repackage', $error) && preg_match('/^Smarty error: unable to read resource:/', $error['repackage']['message'])) { Piece_Unity_Error::push(PIECE_UNITY_ERROR_NOT_FOUND, "The HTML template file [ {$file} ] is not found or not readable.", 'exception', array(), $error); } else { Piece_Unity_Error::push(PIECE_UNITY_ERROR_INVOCATION_FAILED, 'Failed to invoke Smarty::display() for any reasons.', $error['level'], array(), $error); } } }