Code coverage 95 %

95 %
Translator.php
1: <?php
2:
3:
namespace DK\NetteTranslator;
4:
5: use 
DK\Translator\Translator as DKTranslator;
6: use 
Nette\Localization\ITranslator;
7: use 
Nette\Caching\IStorage;
8: use 
Nette\Caching\Cache;
9:
10:
/**
11:  *
12:  * @author David Kudera
13:  */
14: class Translator extends DKTranslator implements ITranslator
15: {
16:
17:
18:     
/** @var \Nette\Caching\Cache */
19:     
private $cache;
20:
21:
22:     
/**
23:      * @param \Nette\Caching\IStorage $cacheStorage
24:      * @return \DK\NetteTranslator\Translator
25:      */
26:     
public function setCacheStorage(IStorage $cacheStorage)
27:     {
28:         $this->cache = new Cache($cacheStorage'translator');
29:         return 
$this;
30:     }
31:
32:
33:     
/**
34:      * @return \Nette\Caching\Cache
35:      */
36:     
public function getCache()
37:     {
38:         return $this->cache;
39:     }
40:
41:
42:     
/**
43:      * @param \Nette\Caching\Cache $cache
44:      * @return \DK\NetteTranslator\Translator
45:      */
46:     public function setCache(Cache $cache null)
47:     {
48:         $this->cache $cache;
49:         return 
$this;
50:     }
51:
52:
53:     
/**
54:      * @param string $path
55:      * @param string $name
56:      * @param string $language
57:      * @return string
58:      */
59:     
public function _getCachedCategoryName($path$name$language)
60:     {
61:         return 
$language':'$path'/'$name;
62:     }
63:
64:
65:     
/**
66:      * @param string $path
67:      * @param string $name
68:      * @param null|string $language
69:      * @return array
70:      */
71:     
public function _loadCategory($path$name$language null)
72:     {
73:         if (
$language === null) {
74:             
$language $this->getLanguage();
75:         }
76:
77:         if (
$this->cache === null) {
78:             return 
parent::_loadCategory($path$name$language);
79:         }
80:
81:         $cachedName $this->_getCachedCategoryName($path$name$language);
82:         
$data $this->cache->load($cachedName);
83:
84:         if (
$data === null) {
85:             
$data parent::_loadCategory($path$name$language);
86:             
$this->cache->save($cachedName$data, array(
87:                 
Cache::FILES => array($this->getLoader()->getFileSystemPath($path$name$language))
88:             ));
89:         }
90:
91:         return 
$data;
92:     }
93: