diff --git a/docs/input/en/classes.translate.adapter.ini.txt b/docs/input/en/classes.translate.adapter.ini.txt new file mode 100644 index 0000000..e348236 --- /dev/null +++ b/docs/input/en/classes.translate.adapter.ini.txt @@ -0,0 +1,50 @@ +Title: Opc_Translate_Adapter_Ini + +---- + +Adapter which uses Ini files as source. + +Available options +================= + +Option | Required? | Default value | Description +------------------|-----------|---------------|------------- +`directory` | Yes | null | Directory, where adapter is searching for language files. +`fileExistsCheck` | No | false | File existence checking before loading file state. + +How to set options to adapter? +============================== + +We have 2 ways of setting options to adapter. + +First +----- + +We could use array use it while constructing new object. + +~~~~ +[php] +$options = array('directory' => './langs/', 'fileExistsCheck' => false); // 1 +$adapter = new Opc_Translate_Adapter_Ini($options); // 2 +~~~~ + +> [steps] +> 1. Our options array. +> 2. Using it while constructing new object. + +Second +------ + +We could use setters for every option. + +~~~~ +[php] +$adapter = new Opc_Translate_Adapter_Ini(); +$adapter->setDirectory('./langs/'); +$adapter->setFileExistsCheck(false); +~~~~ + +As you can see, all setters have exactly the same name as configuration option. +Remember just to set at least all required options. + +It is possible to get states of features just by replacing `set` by `get` in these methods. \ No newline at end of file diff --git a/docs/input/en/classes.translate.adapter.txt b/docs/input/en/classes.translate.adapter.txt new file mode 100644 index 0000000..6947500 --- /dev/null +++ b/docs/input/en/classes.translate.adapter.txt @@ -0,0 +1,5 @@ +Title: Opc_Translate_Adapter + +---- + +This class is only an interface, which you have to implement when you want to make you own adapter. \ No newline at end of file diff --git a/docs/input/en/classes.translate.adapter.xml.txt b/docs/input/en/classes.translate.adapter.xml.txt new file mode 100644 index 0000000..9cbd660 --- /dev/null +++ b/docs/input/en/classes.translate.adapter.xml.txt @@ -0,0 +1,51 @@ +Title: Opc_Translate_Adapter_Xml + +---- + + +Adapter which uses XML files as source. + +Available options +================= + +Option | Required? | Default value | Description +------------------|-----------|---------------|------------- +`directory` | Yes | null | Directory, where adapter is searching for language files. +`fileExistsCheck` | No | false | File existence checking before loading file state. + +How to set options to adapter? +============================== + +We have 2 ways of setting options to adapter. + +First +----- + +We could use array use it while constructing new object. + +~~~~ +[php] +$options = array('directory' => './langs/', 'fileExistsCheck' => false); // 1 +$adapter = new Opc_Translate_Adapter_Xml($options); // 2 +~~~~ + +> [steps] +> 1. Our options array. +> 2. Using it while constructing new object. + +Second +------ + +We could use setters for every option. + +~~~~ +[php] +$adapter = new Opc_Translate_Adapter_Xml(); +$adapter->setDirectory('./langs/'); +$adapter->setFileExistsCheck(false); +~~~~ + +As you can see, all setters have exactly the same name as configuration option. +Remember just to set at least all required options. + +It is possible to get states of features just by replacing `set` by `get` in these methods. \ No newline at end of file diff --git a/docs/input/en/classes.translate.adapter.yaml.txt b/docs/input/en/classes.translate.adapter.yaml.txt new file mode 100644 index 0000000..e3e39d0 --- /dev/null +++ b/docs/input/en/classes.translate.adapter.yaml.txt @@ -0,0 +1,58 @@ +Title: Opc_Translate_Adapter_Yaml + +---- + +Adapter which uses Yaml files as source. It uses sfYaml component, which is mantained +and created by Symfony and Sensio Labs. + +Available options +================= + +Option | Required? | Default value | Description +------------------|-----------|---------------|------------- +`directory` | Yes | null | Directory, where adapter is searching for language files. +`fileExistsCheck` | No | false | File existence checking before loading file state. +`compileResult` | No | true | State of compiling results to an php array and saving them to file. + +Compiling results **needs** cache directory which should be placed in language files directory, +e.g. we have language directory `./langs/`, so we have to create directory `./langs/cache/`, +where adapter will store all compiled result files. It is *important* to make PHP available to +write to this directory. + +How to set options to adapter? +============================== + +We have 2 ways of setting options to adapter. + +First +----- + +We could use array use it while constructing new object. + +~~~~ +[php] +$options = array('directory' => './langs/', 'compileResult' => false); // 1 +$adapter = new Opc_Translate_Adapter_Yaml($options); // 2 +~~~~ + +> [steps] +> 1. Our options array. +> 2. Using it while constructing new object. + +Second +------ + +We could use setters for every option. + +~~~~ +[php] +$adapter = new Opc_Translate_Adapter_Yaml(); +$adapter->setDirectory('./langs/'); +$adapter->setFileExistsCheck(false); +$adapter->setCompileResult(true); +~~~~ + +As you can see, all setters have exactly the same name as configuration option. +Remember just to set at least all required options. + +It is possible to get states of features just by replacing `set` by `get` in these methods. \ No newline at end of file diff --git a/docs/input/en/classes.translate.setup.txt b/docs/input/en/classes.translate.setup.txt new file mode 100755 index 0000000..f3ed4ba --- /dev/null +++ b/docs/input/en/classes.translate.setup.txt @@ -0,0 +1,99 @@ +Title: Basic setup +---- + +In this chapter you will read about using translation interface. + +Basic Setup +=========== + +Firstly you should configure Opl_Loader and create an instance of Opc_Class. +Then we can configure our Opc_Translate. + +~~~~ +[php] + './langs/')); // 2 +$t = new Opc_Translate($adapter); // 3 +$tpl->setTranslationInterface($t); // 4 +~~~~ + +> [steps] +> 1. We create Opt_Class object. +> 2. Now we have to create adapter for Opc_Translate, which handles specific file format. +> 3. Here we create Opc_Translate object. It's constructor needs to have default adapter - adapter witch it will use for all operations. +> 4. And at last we registers translation interface to OPT. It is not needed to completly configure translation interface before setting it to template. + +And that's basic configuration. It uses default language - of course English. + +More translation adapters +========================= + +If you need more adapters for your translation - it is possible to set diffrent adapters to diffrent groups of translation. + +As before you need to configure Opl_Loaded by yourself. + +~~~~ +[php] + './langs/')); // 1 +$t = new Opc_Translate($adapter); +$tpl->setTranslationInterface($t); +$t->setGroupAdapter('groupName', new Opc_Translate_Adapter_Yaml(array('directory' => './yaml_langs/')); // 3 +~~~~ + +> [steps] +> 1. Creating default adapter. +> 2. Creating additional adapter for group 'groupName'. + +As you can see, that additional adapter is completly diffrent (uses other file format and path to language files). + +Setting language +================ + +Setting language is the easiest part of using Opc_Translate. + +~~~~ +[php] + './langs/')); +$t = new Opc_Translate($adapter); +$tpl->setTranslationInterface($t); +$t->setDefaultLanguage('pl'); // 1 +$t->setLanguage('pl'); // 2 +$t->setGroupLanguage('groupName', 'pl'); // 3 +~~~~ + +> [steps] +> 1. Setting default language. +> 2. Setting language to load - it's loaded immidiately. +> 3. Setting language for specified group - it's loaded immiadiately. + +When you access to inexistent language Opc_Translate will try to look for default language files. + +---- + +Of course you can get language for every group you want just by using: +~~~~ +[php] +getDefaultLanguage(); // 1 +$t->getLanguage(); // 2 +$t->getGroupLanguage('groupName'); // 3 +~~~~ + +> [steps] +> 1. Getting default language. +> 2. Getting main language. +> 3. Getting specified group language. + +Adapters +-------- + +All adapters are described in details in sections destined to them. All options are described in these sections too. \ No newline at end of file diff --git a/docs/input/en/classes.translate.txt b/docs/input/en/classes.translate.txt index 9721645..5ab0f5f 100644 --- a/docs/input/en/classes.translate.txt +++ b/docs/input/en/classes.translate.txt @@ -1,3 +1,7 @@ Title: Opc_Translate ----- \ No newline at end of file +---- + +This chapter will describe using and all features of Open Power Classes Translate class. + +It is i18n interface for Open Power Template 2, from version 2.0.1 \ No newline at end of file diff --git a/docs/input/en/classes.view-cache.txt b/docs/input/en/classes.view-cache.txt index 050479b..353c01e 100644 --- a/docs/input/en/classes.view-cache.txt +++ b/docs/input/en/classes.view-cache.txt @@ -1,3 +1,34 @@ Title: Opc_View_Cache ----- \ No newline at end of file +---- + +This is simple class to use Open Power Template caching system. Handles `opt:dynamic` instruction. + +Basic setup +=========== + +The most basic setup is just to create an instance of Opc_View_Cache and set it to Opc_Class. + +~~~~ +[php] +$cache = new Opc_View_Cache(array('cacheDir' => './cache/')); // 1 +$tpl = new Opt_Class; // 2 +// ... +$tpl->setCache($cache); // 3 +~~~~ + +> [steps] +> 1. Creating instance of Opc_View_Cache. +> 2. Creating instance of Opt_Class. +> 3. Setting caching system of Opt_Class to use Opc_View_Cache. + +It's important to set **at least** cache directory. + +Available options +================= + +Option | Required? | Default value | Description +-------------|-----------|---------------|------------- +`cacheDir` | Yes | null | Directory, where cache has to store files. +`expiryTime` | No | 1800 | Time of validity of cache files. +`key` | No | null | Additional key to distinguish the same views and other cache files. \ No newline at end of file diff --git a/docs/sort_hints.txt b/docs/sort_hints.txt index c640cc9..ee6c711 100644 --- a/docs/sort_hints.txt +++ b/docs/sort_hints.txt @@ -16,6 +16,11 @@ classes.paginator.decorators.range-slider classes.paginator.decorators.stepping-slider classes.paginator.opt classes.translate +classes.translate.setup +classes.translate.adapter +classes.translate.adapter.ini +classes.translate.adapter.xml +classes.translate.adapter.yaml classes.view-cache classes.visit classes.visit-useragent diff --git a/lib/Class.php b/lib/Class.php index 354cf4f..d8a5366 100644 --- a/lib/Class.php +++ b/lib/Class.php @@ -34,6 +34,13 @@ class Opc_Class extends Opl_Class * @var integer */ public $expiryTime = 3600; + + // Opc_Translate configuration + /** + * Default language. + * @var string + */ + public $defaultLanugage = 'en'; // Opc_Paginator configuration /** diff --git a/lib/DoctrineModels.php b/lib/DoctrineModels.php index 24fd4ba..23705f8 100644 --- a/lib/DoctrineModels.php +++ b/lib/DoctrineModels.php @@ -119,7 +119,7 @@ static public function setGeneratedModelsDirectoryName($dir) } else { - throw new Opc_DoctrineModelsInvalidDirectoryName_Exception(); + throw new Opc_DoctrineModels_InvalidDirectoryName_Exception(); } } // end setGeneratedModelsDirectoryName(); diff --git a/lib/Exception.php b/lib/Exception.php index f02cc52..d82aa7a 100644 --- a/lib/Exception.php +++ b/lib/Exception.php @@ -20,7 +20,7 @@ class Opc_CannotCreateAnotherInstance_Exception extends Opc_Exception protected $_message = 'Cannot create another Opc_Class instance.'; } // end Opc_InvalidArgumentType_Exception; -class Opc_ClassInstanteNotExists_Exception extends Opc_Exception +class Opc_ClassInstanceNotExists_Exception extends Opc_Exception { protected $_message = 'Opc_Class instance not found. It must be created before using Opc_* classes.'; } // end Opc_InvalidArgumentType_Exception; @@ -55,45 +55,55 @@ class Opc_PaginatorWrongSubclassDecorator_Exception extends Opc_Exception protected $_message = 'Given decorator "%s" is not a subclass of "Opc_Paginator_Decorator".'; } // end Opc_PaginatorWrongSubclassDecorator_Exception; -class Opc_DoctrineModelsInvalidDirectoryName_Exception extends Opc_Exception +class Opc_DoctrineModels_InvalidDirectoryName_Exception extends Opc_Exception { protected $_message = 'Given Doctrine generated models folder name is invalid.'; } // end Opc_DoctrineModelsGeneratedNameInvalid_Exception; -class Opc_View_CacheInvalidDynamicContent_Exception extends Opc_Exception +class Opc_View_Cache_InvalidDynamicContent_Exception extends Opc_Exception { protected $_message = 'View %s has dynamic content, but file with it is broken. Cache cannot be generated.'; -} // end Opc_CacheInvalidDynamicContent_Exception; +} // end Opc_Cache_InvalidDynamicContent_Exception; -class Opc_View_CacheCannotSaveFile_Exception extends Opc_Exception +class Opc_View_Cache_CannotSaveFile_Exception extends Opc_Exception { protected $_message = 'Cache file could not be saved. Has PHP permission to write in cache directory "%s"?'; -} // end Opc_CacheCannotSaveFile_Exception; +} // end Opc_View_Cache_CannotSaveFile_Exception; + +class Opc_View_Cache_ReadingError_Exception extends Opc_Exception +{ + protected $_message = 'Caching system encountered some problems while reading cache file.'; +} // end Opc_View_Cache_ReadingError_Exception; class Opc_Translate_NotSetTranslationDirectory_Exception extends Opc_Exception { protected $_message = 'Directory with translation files is not set! You have to set it before using any functions!'; -} // end Opc_TranslateNotSetTranslationDirectory_Exception; +} // end Opc_Translate_NotSetTranslationDirectory_Exception; class Opc_Translate_MessageNotFound_Exception extends Opc_Exception { protected $_message = 'Translation message in group "%s" ID: "%s" is not found! Language used "%s"'; -} // end Opc_TranslateMessageNotFound_Exception; +} // end Opc_Translate_MessageNotFound_Exception; + +class Opc_Translate_NoTranslationLoaded_Exception extends Opc_Exception +{ + protected $_message = 'Translation interface couldn\'t load correct language file!'; +} // end Opc_Translate_NoTranslationLoaded_Exception; class Opc_Translate_Adapter_FileNotFound_Exception extends Opc_Exception { protected $_message = 'Translation file is not found for language "%s"! Type of translation: "%s"'; -} // end Opc_TranslateFileNotFound_Exception; +} // end Opc_Translate_Adapter_FileNotFound_Exception; class Opc_Translate_Adapter_GroupFileNotFound_Exception extends Opc_Exception { protected $_message = 'Translation file for group "%s" is not found for language "%s"!'; -} // end Opc_TranslateGroupFileNotFound_Exception; +} // end Opc_Translate_Adapter_GroupFileNotFound_Exception; class Opc_Translate_Adapter_CannotAssignData_Exception extends Opc_Exception { protected $_message = 'Cannot assign new data to message! Group "%s", ID "%s".'; -} // end Opc_TranslateCannotAssignData_Exception; +} // end Opc_Translate_Adapter_CannotAssignData_Exception; class Opc_Translate_Adapter_Yaml_CompileWriteFile_Exception extends Opc_Exception { @@ -105,11 +115,6 @@ class Opc_Translate_Adapter_YamlParser_Exception extends Opc_Exception protected $_message = 'There is an error in file "%s". Returned error message: "%s"'; } // end Opc_Translate_Adapter_YamlParser_Exception; -class Opc_Translate_Adapter_Xml_NoTranslationLoaded_Exception extends Opc_Exception -{ - protected $_message = 'There is no language loaded yet, so assignment couldn\'t be successful.'; -} // end Opc_Translate_Adapter_Xml_NoTranslationLoaded_Exception; - class Opc_Calendar_NotSetUpCorrectly_Exception extends Opc_Exception { protected $_message = 'You have to set month and year before using Opc_Calendar.'; diff --git a/lib/Paginator/Range.php b/lib/Paginator/Range.php index f1d5908..7d6ace9 100644 --- a/lib/Paginator/Range.php +++ b/lib/Paginator/Range.php @@ -124,7 +124,7 @@ public function __construct($all = null, $limit = null) { if(!Opl_Registry::exists('opc')) { - throw new Opc_ClassInstanteNotExists_Exception; + throw new Opc_ClassInstanceNotExists_Exception; } $opc = Opl_Registry::get('opc'); diff --git a/lib/Translate.php b/lib/Translate.php index 07c51e5..7f809e7 100644 --- a/lib/Translate.php +++ b/lib/Translate.php @@ -48,7 +48,12 @@ class Opc_Translate implements Opl_Translation_Interface * or selected language has not specified required translation. * @var string */ - $_defaultLanguage = 'en'; + $_defaultLanguage = 'en', + /** + * Opc_Class instance. + * @var Opc_Class + */ + $_opc = null; /** * Creates the new translation object. @@ -57,7 +62,13 @@ class Opc_Translate implements Opl_Translation_Interface */ public function __construct(Opc_Translate_Adapter $adapter) { - $this->setAdapter($adapter); + if(!Opl_Registry::exists('opc')) + { + throw new Opc_ClassInstanceNotExists_Exception; + } + $this->_opc = Opl_Registry::get('opc'); + $this->_defaultLanguage = $this->_opc->defaultLanugage; + $this->_defaultAdapter = $adapter; } // end __construct(); /** @@ -151,11 +162,11 @@ public function _($group, $id) return $msg; } // Try to get default message. - if(($msg = $adapter->getMessage($this->_defaultLanguage, $group, $id, 'default')) !== null) + if(($msg = $adapter->getMessage($this->_defaultLanguage, $group, $id)) !== null) { return $msg; } - throw new Opc_TranslateMessageNotFound_Exception($group, $id, $adapter->getLanguage()); + throw new Opc_Translate_MessageNotFound_Exception($group, $id, $adapter->getLanguage()); } // end _(); public function assign($group, $id) @@ -171,7 +182,8 @@ public function assign($group, $id) { $adapter = $this->_defaultAdapter; } - $adapter->assign($group, $id, $data); + $language = null; + $adapter->assign($language, $group, $id, $data); } // end assign(); /** @@ -190,17 +202,18 @@ public function setLanguage($language) $this->_currentLanguage = $language; return true; } - elseif($this->_defaultAdapter->loadLanguage($this->_defaultLanguage, 'default')) + elseif($this->_defaultAdapter->loadLanguage($this->_defaultLanguage)) { $this->_currentLanguage = $this->_defaultLanguage; return false; } - else - { - throw new Opc_TranslateFileNotFound_Exception($language); - } } // end setLanguage(); + public function getLanguage() + { + return $this->_currentLanguage; + } // end getLanguage(); + /** * Sets language to specified group. * @@ -217,14 +230,14 @@ public function setGroupLanguage($group, $language) $this->_groupsLanguage[$group] = $language; return true; } - elseif($this->_groupAdapters[$group]->loadGroupLanguage($group, $this->_defaultLanguage, 'default')) + elseif($this->_groupAdapters[$group]->loadGroupLanguage($group, $this->_defaultLanguage)) { $this->_groupsLanguage[$group] = $this->_defaultLanguage; return false; } else { - throw new Opc_TranslateFileNotFound_Exception($language, 'translation'); + throw new Opc_Translate_NoTranslationLoaded_Exception(); } } else @@ -234,13 +247,22 @@ public function setGroupLanguage($group, $language) $this->_groupsLanguage[$group] = $language; return true; } - elseif($this->_defaultAdapter->loadGroupLanguage($group, $this->_defaultLanguage, 'default')) + elseif($this->_defaultAdapter->loadGroupLanguage($group, $this->_defaultLanguage)) { $this->_groupsLanguage[$group] = $this->_defaultLanguage; + return false; + } + else + { + throw new Opc_Translate_NoTranslationLoaded_Exception(); } - return false; } } // end setGroupLanguage(); + + public function getGroupLanguage($group) + { + return $this->_groupsLanguage[$group]; + } // end getGroupLanguage(); /** * Gives access to control default language. @@ -251,4 +273,9 @@ public function setDefaultLanguage($language) { $this->_defaultLanguage = $language; } // end setDefaultLanguage(); + + public function getDefaultLanguage() + { + return $this->_defaultLanguage; + } // end getDefaultLanguage(); } // end Opc_View_Translation; \ No newline at end of file diff --git a/lib/Translate/Adapter.php b/lib/Translate/Adapter.php index 63bba88..e2456a2 100644 --- a/lib/Translate/Adapter.php +++ b/lib/Translate/Adapter.php @@ -20,7 +20,7 @@ * @author Amadeusz 'megawebmaster' Starzykiewicz * @license http://www.invenzzia.org/license/new-bsd New BSD License */ -abstract class Opc_Translate_Adapter +interface Opc_Translate_Adapter { /** * Returns the message in the specified language. @@ -30,7 +30,7 @@ abstract class Opc_Translate_Adapter * @param string $msg The message identifier * @return string */ - abstract public function getMessage($language, $group, $msg); + public function getMessage($language, $group, $msg); /** * Assings the data to the specified message. The method must return @@ -42,14 +42,14 @@ abstract public function getMessage($language, $group, $msg); * @param array $data The data to assign * @return boolean */ - abstract public function assign($language, $group, $id, array $data); + public function assign($language, $group, $id, array $data); /** * Loads new language. * * @param string $language New language */ - abstract public function loadLanguage($language); + public function loadLanguage($language); /** * Loads new language for specified group. @@ -57,5 +57,5 @@ abstract public function loadLanguage($language); * @param string $language New language * @param string $group Group name */ - abstract public function loadGroupLanguage($language, $group); + public function loadGroupLanguage($language, $group); } // end Opc_Translate_Adapter; \ No newline at end of file diff --git a/lib/Translate/Adapter/Ini.php b/lib/Translate/Adapter/Ini.php index 63ab11d..e95f665 100644 --- a/lib/Translate/Adapter/Ini.php +++ b/lib/Translate/Adapter/Ini.php @@ -20,7 +20,8 @@ * @author Amadeusz 'megawebmaster' Starzykiewicz * @license http://www.invenzzia.org/license/new-bsd New BSD License */ -class Opc_Translate_Adapter_Ini extends Opc_Translate_Adapter +// TODO: Add getting initial configuration from Opc_Class +class Opc_Translate_Adapter_Ini implements Opc_Translate_Adapter { protected /** @@ -42,22 +43,22 @@ class Opc_Translate_Adapter_Ini extends Opc_Translate_Adapter * File existence checking state. * @var boolean */ - $_fileCheck = false; + $_fileExistsCheck = false; /** * Creates the adapter and applies the options. * * @param array $options The adapter options. */ - public function __construct(array $options) + public function __construct(array $options = array()) { - if(!empty($options['directory'])) + if(isset($options['directory'])) { $this->setDirectory($options['directory']); } - if(!empty($options['fileCheck'])) + if(isset($options['fileExistsCheck'])) { - $this->_fileCheck = (boolean)$options['fileCheck']; + $this->_fileExistsCheck = (boolean)$options['fileExistsCheck']; } } // end __construct(); @@ -92,20 +93,20 @@ public function getDirectory() * * @param boolean $state State of feature */ - public function setFileCheck($state) + public function setFileExistsCheck($state) { - $this->_fileCheck = (boolean)$state; - } // end setFileCheck(); + $this->_fileExistsCheck = (boolean)$state; + } // end setFileExistsCheck(); /** * Returns state of feature. * * @return boolean */ - public function getFileCheck() + public function getFileExistsCheck() { - return $this->_fileCheck; - } // end getFileCheck(); + return $this->_fileExistsCheck; + } // end getFileExistsCheck(); /** * Returns translation for specified group and id. @@ -160,7 +161,7 @@ public function loadGroupLanguage($language, $group) $data = @parse_ini_file($this->_directory.$language.DIRECTORY_SEPARATOR.$group.'.ini'); if($data === false) { - if($this->_fileCheck && !file_exists($this->_directory.$language.DIRECTORY_SEPARATOR.$group.'.ini')) + if($this->_fileExistsCheck && !file_exists($this->_directory.$language.DIRECTORY_SEPARATOR.$group.'.ini')) { throw new Opc_Translate_Adapter_GroupFileNotFound_Exception($group, $language); } @@ -184,7 +185,7 @@ public function loadLanguage($language) $data = @parse_ini_file($this->_directory.$language.'.ini',true); if($data === false) { - if($this->_fileCheck) + if($this->_fileExistsCheck) { throw new Opc_Translate_Adapter_FileNotFound_Exception($language); } diff --git a/lib/Translate/Adapter/Xml.php b/lib/Translate/Adapter/Xml.php index a5521bd..043f5fd 100755 --- a/lib/Translate/Adapter/Xml.php +++ b/lib/Translate/Adapter/Xml.php @@ -20,7 +20,10 @@ * @author Amadeusz 'megawebmaster' Starzykiewicz * @license http://www.invenzzia.org/license/new-bsd New BSD License */ -class Opc_Translate_Adapter_Xml extends Opc_Translate_Adapter +// TODO: Add compiling results feature +// TODO: Add getting initial configuration from Opc_Class +// TODO: Test! +class Opc_Translate_Adapter_Xml implements Opc_Translate_Adapter { protected /** @@ -49,13 +52,13 @@ class Opc_Translate_Adapter_Xml extends Opc_Translate_Adapter * * @param array $options The adapter options. */ - public function __construct(array $options) + public function __construct(array $options = array()) { - if(!empty($options['directory'])) + if(isset($options['directory'])) { $this->setDirectory($options['directory']); } - if(!empty($options['fileExistsCheck'])) + if(isset($options['fileExistsCheck'])) { $this->_fileExistsCheck = (boolean)$options['fileExistsCheck']; } @@ -152,7 +155,7 @@ public function assign($language, $group, $id, array $data) } else { - throw new Opc_Translate_Adapter_Xml_NoTranslationLoaded_Exception(); + return false; } } if(isset($this->_messsages->$group->$id)) diff --git a/lib/Translate/Adapter/Yaml.php b/lib/Translate/Adapter/Yaml.php index 8a5228d..ff32101 100755 --- a/lib/Translate/Adapter/Yaml.php +++ b/lib/Translate/Adapter/Yaml.php @@ -19,7 +19,7 @@ * @author Amadeusz 'megawebmaster' Starzykiewicz */ // TODO: Add getting initial configuration from Opc_Class -class Opc_Translate_Adapter_Yaml extends Opc_Translate_Adapter +class Opc_Translate_Adapter_Yaml implements Opc_Translate_Adapter { protected /** @@ -53,6 +53,26 @@ class Opc_Translate_Adapter_Yaml extends Opc_Translate_Adapter */ $_assigned = null; + /** + * Configures adapter with provided options. + * @param array $options Options + */ + public function __construct(array $options = array()) + { + if(isset($options['fileExistsCheck'])) + { + $this->setFileExistsCheck($options['fileExistsCheck']); + } + if(isset($options['directory'])) + { + $this->setDirectory($options['directory']); + } + if(isset($options['compileResult'])) + { + $this->setCompileResult($options['compileResult']); + } + } // end __construct(); + /** * Sets files directory. * @param string $directory Directory @@ -119,26 +139,6 @@ public function getCompileResult() return $this->_compileResult; } // end getFileExistsCheck(); - /** - * Configures adapter with provided options. - * @param array $options Options - */ - public function __construct(array $options) - { - if(isset($options['fileExistsCheck'])) - { - $this->setFileExistsCheck($options['fileExistsCheck']); - } - if(isset($options['directory'])) - { - $this->setDirectory($options['directory']); - } - if(isset($options['compileResult'])) - { - $this->setCompileResult($options['compileResult']); - } - } // end __construct(); - /** * Returns the message in the specified language. * diff --git a/lib/View/Cache.php b/lib/View/Cache.php index e09c2c3..ec1ab05 100644 --- a/lib/View/Cache.php +++ b/lib/View/Cache.php @@ -21,13 +21,6 @@ */ class Opc_View_Cache implements Opt_Caching_Interface { - /** - * The view that the caching system is connected to. - * - * @var Opt_View - */ - private $_view; - /** * The Opc_Class object. * @@ -54,14 +47,7 @@ class Opc_View_Cache implements Opt_Caching_Interface * * @var integer */ - private $_time = null; - - /** - * Cache file name. - * - * @var string - */ - private $_filename = null; + private $_expiryTime = null; /** * Determines if file was read or not. @@ -87,28 +73,48 @@ class Opc_View_Cache implements Opt_Caching_Interface /** * Creates a new caching object and connects to it. * - * @param Opt_View $view The view + * @param array $options Options */ - public function __construct(Opt_View $view) + public function __construct(array $options = array()) { if(!Opl_Registry::exists('opc')) { - throw new Opc_ClassInstanteNotExists_Exception; + throw new Opc_ClassInstanceNotExists_Exception; } $this->_opc = Opl_Registry::get('opc'); - - $this->_view = $view; + if(isset($options['cacheDir'])) + { + $this->setCacheDir($options['cacheDir']); + } + if(isset($options['expiryTime'])) + { + $this->setExpiryTime($options['expiryTime']); + } + if(isset($options['key'])) + { + $this->setKey($options['key']); + } } // end __construct(); /** - * Sets the extra key for the cache. + * Sets the extra key for the cache. Deleting key is setting it to null. * * @param string|null $key The extra key */ public function setKey($key) { - $this->_key = $key; + $this->_key = (string)$key; } // end setKey(); + + /** + * Returns the current extra caching key. + * + * @return string|null + */ + public function getKey() + { + return $this->_key; + } // end getKey(); /** * Sets the expiry time for the specified caching object. @@ -117,9 +123,9 @@ public function setKey($key) */ public function setExpiryTime($time) { - if(is_integer($time)) + if(is_integer($time) || is_numeric($time)) { - $this->_time = $time; + $this->_expiryTime = (int)$time; } else { @@ -127,6 +133,20 @@ public function setExpiryTime($time) } } // end setExpiryTime(); + /** + * Returns the current expiry time for the cache. + * + * @return integer + */ + public function getExpiryTime() + { + if($this->_expiryTime === null) + { + $this->_expiryTime = $this->_opc->expiryTime; + } + return $this->_expiryTime; + } // end getExpiryTime(); + /** * Sets directory for cache files. * @@ -149,30 +169,6 @@ public function setCacheDir($dir) } $this->_cacheDir = $dir; } // end setCacheDir(); - - /** - * Returns the current extra caching key. - * - * @return string|null - */ - public function getKey() - { - return $this->_key; - } // end getKey(); - - /** - * Returns the current expiry time for the cache. - * - * @return integer - */ - public function getExpiryTime() - { - if($this->_time === null) - { - $this->_time = $this->_opc->expiryTime; - } - return $this->_time; - } // end getExpiryTime(); /** * Returns cache directory. @@ -191,28 +187,26 @@ public function getCacheDir() /** * Gets the cache filename with special key. * + * @param Opt_View $view View. * @return string */ - private function _getFilename() + private function _getFilename(Opt_View $view) { - if($this->_filename == null) - { - $this->_filename = $this->_key.'_'.$this->_view->getTemplate().'.cch'; - } - return $this->_filename; + return ($this->_key !== null?$this->_key.'_':'').$view->getTemplate().'.cch'; } // end _getFilename(); /** * Checks if the specified view is cached. * + * @param Opt_View $view View. * @return boolean */ - public function isCached() + public function isCached(Opt_View $view) { if(!$this->_isReadAlready) { $this->_isReadAlready == true; - $this->_fileHandle = @fopen($this->getCacheDir().$this->_getFilename(), 'r'); + $this->_fileHandle = @fopen($this->getCacheDir().$this->_getFilename($view), 'r'); if($this->_fileHandle === false) { return false; @@ -227,18 +221,18 @@ public function isCached() { /* When header is not an array it means it is wrong, so file must be deleted because of that and because it could be bad or unauthorized changed */ - unlink($this->getCacheDir().$this->_getFilename()); + unlink($this->getCacheDir().$this->_getFilename($view)); $this->_fileHandle = null; return false; } if($header['timestamp'] < (time() - $header['expire'])) { - /* When cache file is old it should be deleted ;) */ - unlink($this->getCacheDir().$this->_getFilename()); + /* When cache file is too old it should be deleted ;) */ + unlink($this->getCacheDir().$this->_getFilename($view)); $this->_fileHandle = null; return false; } - $this->_dynamic = $header['dynamic']; + $this->_dynamic = (boolean)$header['dynamic']; return true; } else @@ -256,10 +250,13 @@ public function isCached() /** * Clears the cache for the specified view. + * + * @param Opt_View $view View + * @return boolean */ - public function clear() + public function clear(Opt_View $view) { - unlink($this->getCacheDir().$this->_getFilename()); + return unlink($this->getCacheDir().$this->_getFilename($view)); } // end clear(); /** @@ -270,17 +267,22 @@ public function clear() */ public function templateCacheStart(Opt_View $view) { - if($this->isCached()) + if($this->isCached($view)) { if($this->_dynamic) { - return $this->getCacheDir().$this->_getFilename(); + return $this->getCacheDir().$this->_getFilename($view); } else { while(!feof($this->_fileHandle)) { $buf = fgets($this->_fileHandle, 2048); + if($buf === false) + { + throw new Opc_View_Cache_ReadingError_Exception(); + return false; + } echo $buf; } } @@ -315,7 +317,7 @@ public function templateCacheStop(Opt_View $view) if($view->hasDynamicContent()) { $buffer = $view->getOutputBuffers(); - $dyn = file_get_contents($tpl->compileDir.$this->_view->_convert($this->_view->getTemplate()).'.dyn'); + $dyn = file_get_contents($tpl->compileDir.$view->_convert($view->getTemplate()).'.dyn'); if($dyn !== false) { $dynamic = unserialize($dyn); @@ -323,7 +325,8 @@ public function templateCacheStop(Opt_View $view) } else { - throw new Opc_View_CacheInvalidDynamicContent_Exception($this->_view->getTemplate()); + throw new Opc_View_Cache_InvalidDynamicContent_Exception($view->getTemplate()); + return false; } $content = ''; for($i = 0, $endI = count($buffer); $i<$endI; $i++) @@ -331,16 +334,18 @@ public function templateCacheStop(Opt_View $view) $content .= $buffer[$i]; $content .= $dynamic[$i]; } - if(file_put_contents($this->getCacheDir().$this->_getFilename(), $header.$content.ob_get_flush()) === false) + if(file_put_contents($this->getCacheDir().$this->_getFilename($view), $header.$content.ob_get_flush()) === false) { - throw new Opc_View_CacheCannotSaveFile_Exception($this->getCacheDir()); + throw new Opc_View_Cache_CannotSaveFile_Exception($this->getCacheDir()); + return false; } } else { - if(file_put_contents($this->getCacheDir().$this->_getFileName(), $header.ob_get_contents()) === false) + if(file_put_contents($this->getCacheDir().$this->_getFileName($view), $header.ob_get_contents()) === false) { - throw new Opc_View_CacheCannotSaveFile_Exception($this->getCacheDir()); + throw new Opc_View_Cache_CannotSaveFile_Exception($this->getCacheDir()); + return false; } } } // end templateCacheStop();