APC::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2026
6
 * @package Base
7
 * @subpackage Translation
8
 */
9
10
11
namespace Aimeos\Base\Translation\Decorator;
12
13
14
/**
15
 * APC caching decorator for translation classes.
16
 *
17
 * @package Base
18
 * @subpackage Translation
19
 */
20
class APC
21
	extends \Aimeos\Base\Translation\Decorator\Base
22
	implements \Aimeos\Base\Translation\Decorator\Iface
23
{
24
	private bool $enable;
25
	private string $prefix;
26
27
28
	/**
29
	 * Initializes the decorator.
30
	 *
31
	 * @param \Aimeos\Base\Translation\Iface $object Translation object or decorator
32
	 * @param string $prefix Prefix for keys to distinguish several instances
33
	 */
34
	public function __construct( \Aimeos\Base\Translation\Iface $object, string $prefix = '' )
35
	{
36
		parent::__construct( $object );
37
38
		$this->enable = function_exists( 'apcu_store' );
39
		$this->prefix = $prefix;
40
	}
41
42
43
	/**
44
	 * Returns the translated string.
45
	 *
46
	 * @param string $domain Translation domain
47
	 * @param string $string String to be translated
48
	 * @return string The translated string
49
	 */
50
	public function dt( string $domain, string $string ) : string
51
	{
52
		if( !$this->enable ) {
53
			return $this->object()->dt( $domain, $string );
54
		}
55
56
		$key = $this->prefix . $domain . '|' . $this->getLocale() . '|' . $string;
57
58
		// regular cache
59
		$success = false;
60
		$value = apcu_fetch( $key, $success );
61
62
		if( $success === true ) {
63
			return $value;
64
		}
65
66
		// not cached
67
		$value = $this->object()->dt( $domain, $string );
68
69
		apcu_store( $key, $value );
70
71
		return $value;
72
	}
73
74
75
	/**
76
	 * Returns the translated string by the given plural and quantity.
77
	 *
78
	 * @param string $domain Translation domain
79
	 * @param string $singular String in singular form
80
	 * @param string $plural String in plural form
81
	 * @param int $number Quantity to chose the correct plural form for languages with plural forms
82
	 * @return string Returns the translated singular or plural form of the string depending on the given number.
83
	 */
84
	public function dn( string $domain, string $singular, string $plural, int $number ) : string
85
	{
86
		if( !$this->enable ) {
87
			return $this->object()->dn( $domain, $singular, $plural, $number );
88
		}
89
90
		$locale = $this->getLocale();
91
		$index = $this->getPluralIndex( $number, $locale );
92
		$key = $this->prefix . $domain . '|' . $locale . '|' . $singular . '|' . $index;
93
94
		// regular cache
95
		$success = false;
96
		$value = apcu_fetch( $key, $success );
97
98
		if( $success === true ) {
99
			return $value;
100
		}
101
102
		// not cached
103
		$value = $this->object()->dn( $domain, $singular, $plural, $number );
104
105
		apcu_store( $key, $value );
106
107
		return $value;
108
	}
109
}
110