예제 #1
0
 /**
  * Retrieves the encoding
  *
  * @return string
  * @throws Glitch_Application_Resource_Exception
  */
 public function getEncoding()
 {
     if (null === $this->_encoding) {
         // Test for UTF-8 support. The regex will wrongly match 4 chars,
         // rather than 2, if PCRE was compiled without UTF-8 support. If the chars
         // in preg_match appear garbled, change the encoding of this file to UTF-8!
         if (!extension_loaded('iconv') || !preg_match('~^..$~u', '±')) {
             throw new Glitch_Application_Resource_Exception('No multibyte strings support');
         }
         $options = $this->getOptions();
         // Force these options to be set - don't rely on the defaults!
         if (!isset($options['encoding'])) {
             throw new Glitch_Application_Resource_Exception('Undefined encoding option: "encoding"');
         }
         $this->_encoding = (string) $options['encoding'];
         // Override the default charset; also sends the appropriate HTTP header
         ini_set('default_charset', $this->_encoding);
         // ZF uses iconv for e.g. form validation and Zend_Locale
         iconv_set_encoding('internal_encoding', $this->_encoding);
         // MB extension is not required by ZF, so don't throw exceptions
         if (function_exists('mb_internal_encoding')) {
             // ZF uses this for e.g. Zend_Filter and Zend_Feed
             mb_internal_encoding($this->_encoding);
         }
         // Allow application-wide access; e.g. Zend_Mail uses this
         Glitch_Registry::setEncoding($this->_encoding);
     }
     return $this->_encoding;
 }
예제 #2
0
 /**
  * Retrieves the logger object
  *
  * @return Zend_Log
  * @throws Glitch_Application_Resource_Exception
  */
 public function getLog()
 {
     if (null === $this->_log) {
         $options = $this->getOptions();
         // Force these options to be set - don't rely on the defaults!
         if (!isset($options['level'])) {
             throw new Glitch_Application_Resource_Exception('Undefined log option: "level"');
         }
         // Validate the log level
         $level = constant('Zend_Log::' . $options['level']);
         if (null === $level) {
             throw new Glitch_Application_Resource_Exception('Unknown log level: "' . $options['level'] . '"');
         }
         // Ensure the request is initialized
         $this->_bootstrap->bootstrap('Request');
         $request = $this->_bootstrap->getResource('Request');
         $isHttpRequest = $request instanceof Zend_Controller_Request_Http;
         // Use localhost as name if not running in HTTP mode
         $host = $isHttpRequest ? $request->getHttpHost() : 'localhost';
         if (strncasecmp($host, 'www.', 4) == 0) {
             $host = substr($host, 4);
             // Remove "www." prefix for readability
         }
         $this->_log = new Zend_Log();
         // Build filename, e.g. "20090601_localhost.log"
         $file = Zend_Date::now()->toString('yyyyMMdd') . '_' . $host . '.log';
         $file = GLITCH_LOGS_PATH . DIRECTORY_SEPARATOR . $file;
         $writer = new Zend_Log_Writer_Stream($file);
         // Use custom logging format, e.g.
         // [2010-08-07T17:03:18+02:00] ERR (/account/login): Method "_getParams" does not exist
         $format = '[%timestamp%] %priorityName%';
         if ($isHttpRequest) {
             $format .= ' (%requestUri%)';
             $this->_log->setEventItem('requestUri', $request->getRequestUri());
         }
         $format .= ': %message%';
         $formatter = new Zend_Log_Formatter_Simple($format . PHP_EOL);
         $writer->setFormatter($formatter);
         $this->_log->addWriter($writer);
         // Also send log output to browser console?
         if ($isHttpRequest && (isset($options['toFirebug']) && $options['toFirebug'])) {
             $this->_log->addWriter(new Zend_Log_Writer_Firebug());
         }
         $filter = new Zend_Log_Filter_Priority($level);
         $this->_log->addFilter($filter);
         // Allow application-wide access
         Glitch_Registry::setLog($this->_log);
     }
     return $this->_log;
 }
예제 #3
0
 /**
  * Retrieves the locale object
  *
  * @return Zend_Locale
  * @throws Glitch_Application_Resource_Exception
  */
 public function getLocale()
 {
     if (null === $this->_locale) {
         $options = $this->getOptions();
         // Force these options to be set - don't rely on the defaults!
         if (!isset($options['default'])) {
             throw new Glitch_Application_Resource_Exception('Locale option "default" not set');
         }
         // First init cache, then create the locale
         $this->_setCache();
         $this->_locale = new Zend_Locale($options['default']);
         Zend_Locale::setDefault($this->_locale);
         // Allow application-wide access; e.g. Zend_Date uses this
         Glitch_Registry::setLocale($this->_locale);
         // Force formatter to use the above registered default locale
         Zend_Locale_Format::setOptions(array('locale' => null));
     }
     return $this->_locale;
 }
예제 #4
0
 /**
  * Retrieves the database adapter
  *
  * @return Zend_Db_Adapter_Abstract
  */
 public function getDb()
 {
     if (null === $this->_db) {
         // Use parent for basic initialization
         parent::init();
         // Has profiler? Attach it to the database adapter
         if ($this->_db->getProfiler()->getEnabled()) {
             // Check whether this is a HTTP request; if not, don't use Firebug
             $this->_bootstrap->bootstrap('Request');
             $request = $this->_bootstrap->getResource('Request');
             $profiler = $request instanceof Zend_Controller_Request_Http ? new Zend_Db_Profiler_Firebug('Database queries') : new Zend_Db_Profiler();
             $profiler->setEnabled(true);
             $this->_db->setProfiler($profiler);
         }
         $this->_setCache();
         // Allow application-wide access
         Glitch_Registry::setDb($this->_db);
     }
     return $this->_db;
 }
예제 #5
0
 /**
  * Retrieves the translate object
  *
  * @return Zend_Translate
  */
 public function getTranslate()
 {
     if (null === $this->_translate) {
         $options = $this->getOptions();
         // First init cache, then create the translator
         $this->_setCache();
         // Ensure locale is set: required by translator. There's no need to
         // pass this locale to translate, though: will be done automatically.
         $this->_bootstrap->bootstrap('Locale');
         $locale = $this->_bootstrap->getResource('Locale');
         // Load the file with shared, module-independant translations.
         // Performance: use absolute path, so that ZF doesn't need to resolve it
         // Filepath format: {locale}/{locale}.ini, e.g. "nl_NL/nl_NL.ini".
         $filename = sprintf('%s%s%s%s%s.%s', GLITCH_LANGUAGES_PATH, DIRECTORY_SEPARATOR, $locale->toString(), DIRECTORY_SEPARATOR, $locale->toString(), $this->_adapterFileExtension);
         // Config may contain additional translate options
         $params = isset($options['options']) ? $options['options'] : array();
         // Auto-set the logger to which notices and messages are written to
         $this->_bootstrap->bootstrap('Log');
         $params['log'] = $this->_bootstrap->getResource('Log');
         $this->_translate = new Zend_Translate($this->_adapterName, $filename, null, $params);
         // Allow application-wide access; e.g. Zend_Form uses this.
         // Use the registry to change the locale at some point in your
         // application, i.e. after the user has switched language:
         // Glitch_Registry::getTranslate()->setLocale('en_GB')
         Glitch_Registry::setTranslate($this->_translate);
     }
     return $this->_translate;
 }
예제 #6
0
 /**
  * Gets the configuration
  *
  * This is the preferred method for loading the config. Don't use loadConfig() directly!
  * Be aware: this method is used for bootstrapping the config, e.g. in public/index.php.
  * Once loaded, users ought to call Glitch_Registry::getConfig() or
  * Glitch_Registry::getSettings().
  *
  * @return Zend_Config
  */
 public static function getConfig()
 {
     // Don't call the cached method more than once
     if (null === self::$_config) {
         self::$_config = self::getInstance()->_cache->loadConfig(GLITCH_APP_ENV);
         // Allow application-wide access
         Glitch_Registry::setConfig(self::$_config);
         // Store the application settings, if any
         $settings = self::$_config->get('settings');
         if (null !== $settings) {
             Glitch_Registry::setSettings($settings);
         }
     }
     return self::$_config;
 }
예제 #7
0
 /**
  * This action will decide what controllers will be called and in which order
  *
  * Note: this can change the expected dispatch behaviour!
  *
  * @param $request
  * @return void
  */
 public function routeShutdown(Zend_Controller_Request_Abstract $request)
 {
     //Check if the module is registered for HMVC
     if (!$this->isActiveModule($request->getModuleName())) {
         return;
     }
     //For some reason, unknown to me at this time, Zend_Controller_Request has lost all relevant info.
     //Rebuild it from scratch.
     $params = explode('/', trim($request->getPathInfo(), '/'));
     if ($params[0] === $request->getModuleName()) {
         //Unset the module if the same, not needed further
         unset($params[0]);
     }
     $action = $request->getActionName();
     $module = $request->getModuleName();
     //Clear all params as new ones are on the way
     $request->clearParams();
     //Filter strings only
     $names = array_filter($params, 'ctype_alpha');
     //Filter digits only
     $ids = array_filter($params, 'ctype_digit');
     if (0 === count($names) && 0 === count($ids)) {
         //We have nothing here to work with.. Assume default actions take over
         return;
     }
     $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
     //Set some default params
     $request->setParam('collection', false)->setParam('resource', true)->setParam('passthrough', true);
     if (count($names) !== count($ids)) {
         $ids = array_pad($ids, count($names), null);
         $request->setParam('collection', true)->setParam('resource', false);
         //Possible fix for crappy Zend_Rest_Route as the action is set to index
         //Example: /rest/car should point to Hmvc/Controller/Car/"METHOD" to get a collection
         if (!in_array($action, array('get', 'put', 'post', 'delete'))) {
             $action = strtolower($request->getMethod());
         }
     }
     //Combine all data
     $params = array_combine($names, $ids);
     $params['module'] = $module;
     $params['action'] = $action;
     $params['controller'] = $dispatcher->formatModuleName(current($names));
     //Register with the request so the controllers can work with them
     $request->setModuleName($module)->setActionName($action)->setParams($params);
     $delimiter = $dispatcher->getPathDelimiter();
     if (!Glitch_Registry::getConfig()->resources->hmvc->redispatch) {
         $controller = $dispatcher->formatModuleName(implode($delimiter, $names));
         $request->setControllerName($controller)->setParam('controller', $controller)->setDispatched(false)->setParam('passthrough', false);
         return;
     }
     $nextController = '';
     foreach ($names as $controller) {
         //Collect the new controller string
         $nextController .= $delimiter . $controller;
         //Modify so it fits our structure and append to the controllers we want to handle
         //We use formatModuleName as we already figured out our own structure
         $this->_controllers[] = $dispatcher->formatModuleName(trim($nextController, $delimiter));
     }
 }