File indexing completed on 2025-03-02 05:29:09
0001 <?php 0002 0003 /** 0004 * ocs-apiserver 0005 * 0006 * Copyright 2016 by pling GmbH. 0007 * 0008 * This file is part of ocs-apiserver. 0009 * 0010 * This program is free software: you can redistribute it and/or modify 0011 * it under the terms of the GNU Affero General Public License as 0012 * published by the Free Software Foundation, either version 3 of the 0013 * License, or (at your option) any later version. 0014 * 0015 * This program is distributed in the hope that it will be useful, 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0018 * GNU Affero General Public License for more details. 0019 * 0020 * You should have received a copy of the GNU Affero General Public License 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0022 **/ 0023 class Local_Application extends Zend_Application 0024 { 0025 const CACHE_APP_INI = 'cache_api_ini'; 0026 0027 /** 0028 * 0029 * @var Zend_Cache_Core|null 0030 */ 0031 protected $_configCache; 0032 0033 /** 0034 * Constructor 0035 * 0036 * Initialize application. Potentially initializes include_paths, PHP 0037 * settings, and bootstrap class. 0038 * 0039 * @param string $environment 0040 * @param string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of 0041 * configuration options 0042 * 0043 * @throws Zend_Application_Exception 0044 * @throws Zend_Cache_Exception 0045 */ 0046 public function __construct($environment, $options = null) 0047 { 0048 $this->_configCache = $this->_initCache(); 0049 parent::__construct($environment, $options); 0050 } 0051 0052 /** 0053 * @throws Zend_Cache_Exception 0054 */ 0055 protected function _initCache() 0056 { 0057 $frontendOptions = array('lifetime' => null, 0058 'automatic_serialization' => true); 0059 0060 $backendOptions = array('cache_dir' => APPLICATION_CACHE, 0061 'file_locking' => true, 0062 'read_control' => true, 0063 'read_control_type' => 'crc32', 0064 'hashed_directory_level' => 0, 0065 'hashed_directory_perm' => 0700, 0066 'file_name_prefix' => 'ocs', 0067 'cache_file_perm' => 0700); 0068 0069 return Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions); 0070 } 0071 0072 /** 0073 * @throws Zend_Cache_Exception 0074 */ 0075 public function getApplicationConfig() 0076 { 0077 $cacheName = APPLICATION_ENV . '_' . self::CACHE_APP_INI; 0078 if (false === ($config = $this->_configCache->load($cacheName))) { 0079 $config = new Zend_Config($this->getOptions(), true); 0080 $this->_configCache->save($config, $cacheName, array(), 300); 0081 } 0082 0083 return $config; 0084 } 0085 0086 /** 0087 * Load configuration file of options 0088 * 0089 * @param string $file 0090 * 0091 * @return array 0092 * @throws Zend_Application_Exception When invalid configuration file is provided 0093 * @throws Zend_Cache_Exception 0094 */ 0095 protected function _loadConfig($file) 0096 { 0097 $suffix = strtolower(pathinfo($file, PATHINFO_EXTENSION)); 0098 0099 if ($this->_configCache === null or $suffix == 'php' or $suffix == 'inc') { //No need for caching those 0100 return parent::_loadConfig($file); 0101 } 0102 0103 $cacheId = $this->_cacheId($file); 0104 0105 if (false === ($config = $this->_configCache->load($cacheId))) { 0106 $config = parent::_loadConfig($file); 0107 $this->_configCache->save($config, $cacheId, array(), null); 0108 } 0109 0110 return $config; 0111 } 0112 0113 protected function _cacheId($file) 0114 { 0115 return 'config_' . $this->getEnvironment() . '_' . md5_file($file); 0116 } 0117 0118 /** 0119 * @param string $file 0120 * @param $cacheId 0121 * 0122 * @return bool 0123 */ 0124 protected function _testCache($file, $cacheId) 0125 { 0126 $configMTime = filemtime($file); 0127 0128 $cacheLastMTime = $this->_configCache->test($cacheId); 0129 0130 if ($cacheLastMTime !== false and $configMTime < $cacheLastMTime) { //Valid cache? 0131 return true; 0132 } 0133 0134 return false; 0135 } 0136 0137 }