/** * Loads the template from the specified file. * @return ITemplate template parsed from the specified file, null if the file doesn't exist. */ public function getTemplateByFileName($fileName) { if (($fileName = $this->getLocalizedTemplate($fileName)) !== null) { Prado::trace("Loading template {$fileName}", '\\Prado\\Web\\UI\\TTemplateManager'); if (($cache = $this->getApplication()->getCache()) === null) { return new TTemplate(file_get_contents($fileName), dirname($fileName), $fileName); } else { $array = $cache->get(self::TEMPLATE_CACHE_PREFIX . $fileName); if (is_array($array)) { list($template, $timestamps) = $array; if ($this->getApplication()->getMode() === TApplicationMode::Performance) { return $template; } $cacheValid = true; foreach ($timestamps as $tplFile => $timestamp) { if (!is_file($tplFile) || filemtime($tplFile) > $timestamp) { $cacheValid = false; break; } } if ($cacheValid) { return $template; } } $template = new TTemplate(file_get_contents($fileName), dirname($fileName), $fileName); $includedFiles = $template->getIncludedFiles(); $timestamps = array(); $timestamps[$fileName] = filemtime($fileName); foreach ($includedFiles as $includedFile) { $timestamps[$includedFile] = filemtime($includedFile); } $cache->set(self::TEMPLATE_CACHE_PREFIX . $fileName, array($template, $timestamps)); return $template; } } else { return null; } }
/** * Renders the callback response by adding additional callback data and * javascript actions in the header and page state if required. * @param THtmlWriter html content writer. */ protected function renderResponse($writer) { Prado::trace("ActivePage renderResponse()", 'Prado\\Web\\UI\\ActiveControls\\TActivePageAdapter'); //renders all the defered render() calls. foreach ($this->_controlsToRender as $rid => $forRender) { $forRender[0]->render($forRender[1]); } $response = $this->getResponse(); //send response data in header if ($response->getHasAdapter()) { $responseData = $response->getAdapter()->getResponseData(); if ($responseData !== null) { $data = TJavaScript::jsonEncode($responseData); $this->appendContentPart($response, self::CALLBACK_DATA_HEADER, $data); } } //sends page state in header if (($handler = $this->getCallbackEventTarget()) !== null) { if ($handler->getActiveControl()->getClientSide()->getEnablePageStateUpdate()) { $pagestate = $this->getPage()->getClientState(); $this->appendContentPart($response, self::CALLBACK_PAGESTATE_HEADER, $pagestate); } } //safari must receive at least 1 byte of data. $writer->write(" "); //output the end javascript if ($this->getPage()->getClientScript()->hasEndScripts()) { $writer = $response->createHtmlWriter(); $this->getPage()->getClientScript()->renderEndScriptsCallback($writer); $this->getPage()->getCallbackClient()->evaluateScript($writer); } //output the actions $executeJavascript = $this->getCallbackClientHandler()->getClientFunctionsToExecute(); $actions = TJavaScript::jsonEncode($executeJavascript); $this->appendContentPart($response, self::CALLBACK_ACTION_HEADER, $actions); $cs = $this->Page->getClientScript(); // collect all stylesheet file references $stylesheets = $cs->getStyleSheetUrls(); if (count($stylesheets) > 0) { $this->appendContentPart($response, self::CALLBACK_STYLESHEETLIST_HEADER, TJavaScript::jsonEncode($stylesheets)); } // collect all stylesheet snippets references $stylesheets = $cs->getStyleSheetCodes(); if (count($stylesheets) > 0) { $this->appendContentPart($response, self::CALLBACK_STYLESHEET_HEADER, TJavaScript::jsonEncode($stylesheets)); } // collect all script file references $scripts = $cs->getScriptUrls(); if (count($scripts) > 0) { $this->appendContentPart($response, self::CALLBACK_SCRIPTLIST_HEADER, TJavaScript::jsonEncode($scripts)); } // collect all hidden field references $fields = $cs->getHiddenFields(); if (count($fields) > 0) { $this->appendContentPart($response, self::CALLBACK_HIDDENFIELDLIST_HEADER, TJavaScript::jsonEncode($fields)); } }
/** * Saves page state from persistent storage. */ protected function savePageState() { Prado::trace("Saving state", 'Prado\\Web\\UI\\TPage'); $state =& $this->saveStateRecursive($this->getEnableViewState()); $this->getStatePersister()->save($state); }
/** * Copies a file to a directory. * Copying is done only when the destination file does not exist * or has an older file modification time. * @param string source file path * @param string destination directory (if not exists, it will be created) */ protected function copyFile($src, $dst) { if (!is_dir($dst)) { @mkdir($dst); @chmod($dst, PRADO_CHMOD); } $dstFile = $dst . DIRECTORY_SEPARATOR . basename($src); if (@filemtime($dstFile) < @filemtime($src)) { Prado::trace("Publishing file {$src} to {$dstFile}", 'Prado\\Web\\TAssetManager'); @copy($src, $dstFile); } }
/** * Runs the service. * If the service parameter ends with '.wsdl', it will serve a WSDL file for * the specified soap server. * Otherwise, it will handle the soap request using the specified server. */ public function run() { Prado::trace("Running SOAP service", 'Prado\\Web\\Services\\TSoapService'); $server = $this->createServer(); $this->getResponse()->setContentType('text/xml'); $this->getResponse()->setCharset($server->getEncoding()); if ($this->getIsWsdlRequest()) { // server WSDL file Prado::trace("Generating WSDL", 'Prado\\Web\\Services\\TSoapService'); $this->getResponse()->clear(); $this->getResponse()->write($server->getWsdl()); } else { // provide SOAP service Prado::trace("Handling SOAP request", 'Prado\\Web\\Services\\TSoapService'); $server->run(); } }
/** * Applies the theme to a particular control. * The control's class name and SkinID value will be used to * identify which skin to be applied. If the control's SkinID is empty, * the default skin will be applied. * @param TControl the control to be applied with a skin * @return boolean if a skin is successfully applied * @throws TConfigurationException if any error happened during the skin application */ public function applySkin($control) { $type = get_class($control); if (($id = $control->getSkinID()) === '') { $id = 0; } if (isset($this->_skins[$type][$id])) { foreach ($this->_skins[$type][$id] as $name => $value) { Prado::trace("Applying skin {$name} to {$type}", 'Prado\\Web\\UI\\TThemeManager'); if (is_array($value)) { switch ($value[0]) { case TTemplate::CONFIG_EXPRESSION: $value = $this->evaluateExpression($value[1]); break; case TTemplate::CONFIG_ASSET: $value = $this->_themeUrl . '/' . ltrim($value[1], '/'); break; case TTemplate::CONFIG_DATABIND: $control->bindProperty($name, $value[1]); break; case TTemplate::CONFIG_PARAMETER: $control->setSubProperty($name, $this->getApplication()->getParameters()->itemAt($value[1])); break; case TTemplate::CONFIG_TEMPLATE: $control->setSubProperty($name, $value[1]); break; case TTemplate::CONFIG_LOCALIZATION: $control->setSubProperty($name, Prado::localize($value[1])); break; default: throw new TConfigurationException('theme_tag_unexpected', $name, $value[0]); break; } } if (!is_array($value)) { if (strpos($name, '.') === false) { if ($control->hasProperty($name)) { if ($control->canSetProperty($name)) { $setter = 'set' . $name; $control->{$setter}($value); } else { throw new TConfigurationException('theme_property_readonly', $type, $name); } } else { throw new TConfigurationException('theme_property_undefined', $type, $name); } } else { // complex property $control->setSubProperty($name, $value); } } } return true; } else { return false; } }
/** * Loads a specific config file. * @param string config file name * @param string the page path that the config file is associated with. The page path doesn't include the page name. */ public function loadFromFile($fname, $configPagePath) { Prado::trace("Loading page configuration file {$fname}", 'System.Web.Services.TPageService'); if (empty($fname) || !is_file($fname)) { return; } if (Prado::getApplication()->getConfigurationType() == TApplication::CONFIG_TYPE_PHP) { $fcontent = (include $fname); $this->loadFromPhp($fcontent, dirname($fname), $configPagePath); } else { $dom = new TXmlDocument(); if ($dom->loadFromFile($fname)) { $this->loadFromXml($dom, dirname($fname), $configPagePath); } else { throw new TConfigurationException('pageserviceconf_file_invalid', $fname); } } }
/** * Runs the service. * This will create the requested page, initializes it with the property values * specified in the configuration, and executes the page. */ public function run() { Prado::trace("Running page service", 'Prado\\Web\\Services\\TPageService'); $this->_page = $this->createPage($this->getRequestedPagePath()); $this->runPage($this->_page, $this->_properties); }
/** * Loads the template associated with this control class. * @return ITemplate the parsed template structure */ protected function loadTemplate() { Prado::trace("Loading template " . get_class($this), '\\Prado\\Web\\UI\\TTemplateControl'); $template = $this->getService()->getTemplateManager()->getTemplateByClassName(get_class($this)); return $template; }
/** * Raises 'OnDataBinding' event. * This method is invoked when {@link dataBind} is invoked. * @param TEventParameter event parameter to be passed to the event handlers */ public function onDataBinding($param) { Prado::trace("onDataBinding()", 'Prado\\Web\\UI\\TControl'); $this->raiseEvent('OnDataBinding', $this, $param); }
/** * Flush expired values from cache depending on {@link setFlushInterval FlushInterval} * @param boolean override {@link setFlushInterval FlushInterval} and force deletion of expired items * @return void * @since 3.1.5 */ public function flushCacheExpired($force = false) { $interval = $this->getFlushInterval(); if (!$force && $interval === 0) { return; } $key = 'TDbCache:' . $this->_cacheTable . ':flushed'; $now = time(); $next = $interval + (int) $this->getApplication()->getGlobalState($key, 0); if ($force || $next <= $now) { if (!$this->_cacheInitialized) { $this->initializeCache(); } Prado::trace(($force ? 'Force flush of expired items: ' : 'Flush expired items: ') . $this->id . ', ' . $this->_cacheTable, '\\Prado\\Caching\\TDbCache'); $sql = 'DELETE FROM ' . $this->_cacheTable . ' WHERE expire<>0 AND expire<' . $now; $this->getDbConnection()->createCommand($sql)->execute(); $this->getApplication()->setGlobalState($key, $now); } }
/** * Generate a SOAP fault message. * @param string message title * @param mixed message details * @param string message code, defalt is 'SERVER'. * @param string actors * @param string message name */ public function fault($title, $details = '', $code = 'SERVER', $actor = '', $name = '') { Prado::trace('SOAP-Fault ' . $code . ' ' . $title . ' : ' . $details, 'Prado\\Web\\Services\\TSoapService'); $this->_server->fault($code, $title, $actor, $details, $name); }
/** * Initializes this module. * This method is required by the IModule interface. It makes sure that * UniquePrefix has been set, creates a Memcache instance and connects * to the memcache server. * @param TApplication Prado application, can be null * @param TXmlElement configuration for this module, can be null * @throws TConfigurationException if memcache extension is not installed or memcache sever connection fails */ public function init($config) { if (!extension_loaded('memcache') && !$this->_useMemcached) { throw new TConfigurationException('memcache_extension_required'); } if (!extension_loaded('memcached') && $this->_useMemcached) { throw new TConfigurationException('memcached_extension_required'); } $this->_cache = $this->_useMemcached ? new Memcached() : new Memcache(); $this->loadConfig($config); if (count($this->_servers)) { foreach ($this->_servers as $server) { Prado::trace('Adding server ' . $server['Host'] . ' from serverlist', '\\Prado\\Caching\\TMemCache'); if ($this->_cache->addServer($server['Host'], $server['Port'], $server['Persistent'], $server['Weight'], $server['Timeout'], $server['RetryInterval']) === false) { throw new TConfigurationException('memcache_connection_failed', $server['Host'], $server['Port']); } } } else { Prado::trace('Adding server ' . $this->_host, '\\Prado\\Caching\\TMemCache'); if ($this->_cache->addServer($this->_host, $this->_port) === false) { throw new TConfigurationException('memcache_connection_failed', $this->_host, $this->_port); } } if ($this->_threshold !== 0) { $this->_cache->setCompressThreshold($this->_threshold, $this->_minSavings); } $this->_initialized = true; parent::init($config); }
/** * Sends a header. * @param string header * @param boolean whether the header should replace a previous similar header, or add a second header of the same type */ public function appendHeader($value, $replace = true) { Prado::trace("Sending header '{$value}'", 'Prado\\Web\\THttpResponse'); header($value, $replace); }
/** * Resolves the requested service. * This method implements a URL-based service resolution. * A URL in the format of /index.php?sp=serviceID.serviceParameter * will be resolved with the serviceID and the serviceParameter. * You may override this method to provide your own way of service resolution. * @param array list of valid service IDs * @return string the currently requested service ID, null if no service ID is found * @see constructUrl */ public function resolveRequest($serviceIDs) { Prado::trace("Resolving request from " . $_SERVER['REMOTE_ADDR'], 'Prado\\Web\\THttpRequest'); $getParams = $this->parseUrl(); foreach ($getParams as $name => $value) { $_GET[$name] = $value; } $this->_items = array_merge($_GET, $_POST); $this->_requestResolved = true; foreach ($serviceIDs as $serviceID) { if ($this->contains($serviceID)) { $this->setServiceID($serviceID); $this->setServiceParameter($this->itemAt($serviceID)); return $serviceID; } } return null; }
private function _buildSmartToString($tableInfo) { $code = "\tpublic function __toString() {"; $property = "throw new THttpException(500, 'Not implemented yet.');"; try { foreach ($tableInfo->getColumns() as $column) { if (isset($column->IsPrimaryKey) && $column->IsPrimaryKey) { $property = str_replace($this->uqChars, "", $column->ColumnName); } elseif ($column->PdoType == PDO::PARAM_STR && $column->DBType != "date") { $property = str_replace($this->uqChars, "", $column->ColumnName); break; } } } catch (Exception $ex) { Prado::trace($ex->getMessage()); } $code .= "\n\t\treturn \$this->{$property};"; $code .= "\n\t}"; return $code; }