Completed
Push — master ( f5d287...8699f2 )
by Aimeos
08:40
created

Context::get()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 3 Features 1
Metric Value
c 7
b 3
f 1
dl 0
loc 48
rs 9.125
cc 3
eloc 28
nc 4
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://www.gnu.org/copyleft/lgpl.html
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package flow
7
 * @subpackage Base
8
 */
9
10
11
namespace Aimeos\Shop\Base;
12
13
use TYPO3\Flow\Annotations as Flow;
14
15
16
/**
17
 * Class providing the context object
18
 *
19
 * @package flow
20
 * @subpackage Base
21
 * @Flow\Scope("singleton")
22
 */
23
class Context
24
{
25
	/**
26
	 * @var \Aimeos\MShop\Context\Item\Iface
27
	 */
28
	private static $context;
29
30
	/**
31
	 * @var \Aimeos\MShop\Locale\Item\Iface
32
	 */
33
	private $locale;
34
35
	/**
36
	 * @var array
37
	 */
38
	private $settings;
39
40
	/**
41
	 * @var \TYPO3\Flow\Cache\Frontend\StringFrontend
42
	 */
43
	private $cache;
44
45
	/**
46
	 * @var array
47
	 * @Flow\Inject(setting="persistence.backendOptions", package="TYPO3.Flow")
48
	 */
49
	protected $resource;
50
51
	/**
52
	 * @var \Aimeos\Shop\Base\Aimeos
53
	 * @Flow\Inject
54
	 */
55
	protected $aimeos;
56
57
	/**
58
	 * @var \Aimeos\Shop\Base\I18n
59
	 * @Flow\Inject
60
	 */
61
	protected $i18n;
62
63
	/**
64
	 * @var \TYPO3\SwiftMailer\MailerInterface
65
	 * @Flow\Inject
66
	 */
67
	protected $mailer;
68
69
	/**
70
	 * @var \TYPO3\Flow\Session\SessionInterface
71
	 * @Flow\Inject(lazy = FALSE)
72
	 */
73
	protected $session;
74
75
76
	/**
77
	 * Returns the current context.
78
	 *
79
	 * @param \TYPO3\Flow\Mvc\RequestInterface $request Request object
80
	 * @return \Aimeos\MShop\Context\Item\Iface
81
	 */
82
	public function get( \TYPO3\Flow\Mvc\RequestInterface $request = null )
83
	{
84
		if( self::$context === null )
85
		{
86
			$context = new \Aimeos\MShop\Context\Item\Standard();
87
88
			$config = $this->getConfig();
89
			$context->setConfig( $config );
90
91
			$dbm = new \Aimeos\MW\DB\Manager\PDO( $config );
92
			$context->setDatabaseManager( $dbm );
93
94
			$fsm = new \Aimeos\MW\Filesystem\Manager\Standard( $config );
95
			$context->setFilesystemManager( $fsm );
96
97
			$mq = new \Aimeos\MW\MQueue\Manager\Standard( $config );
98
			$context->setMessageQueueManager( $mq );
99
100
			$mail = new \Aimeos\MW\Mail\Swift( $this->mailer );
101
			$context->setMail( $mail );
102
103
			$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context );
104
			$context->setLogger( $logger );
105
106
			$cache = $this->getCache( $context );
107
			$context->setCache( $cache );
108
109
			self::$context = $context;
110
		}
111
112
		$context = self::$context;
113
114
		if( $request !== null )
115
		{
116
			$localeItem = $this->getLocale( $context, $request );
117
			$context->setLocale( $localeItem );
118
119
			$i18n = $this->i18n->get( array( $localeItem->getLanguageId() ) );
120
			$context->setI18n( $i18n );
121
		}
122
123
		$session = new \Aimeos\MW\Session\Flow( $this->session );
124
		$context->setSession( $session );
125
126
		$this->addUser( $context );
127
128
		return $context;
129
	}
130
131
132
	/**
133
	 * Adds the user ID and name if available
134
	 *
135
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
136
	 */
137
	protected function addUser( \Aimeos\MShop\Context\Item\Iface $context )
138
	{
139
		$username = '';
140
141
		$context->setEditor( $username );
142
	}
143
144
145
	/**
146
	 * Returns the cache object for the context
147
	 *
148
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
149
	 * @return \Aimeos\MW\Cache\Iface Cache object
150
	 */
151
	protected function getCache( \Aimeos\MShop\Context\Item\Iface $context )
152
	{
153
		$config = $context->getConfig();
154
155
		switch( $config->get( 'flow/cache/name', 'Flow' ) )
156
		{
157
			case 'None':
158
				$config->set( 'client/html/basket/cache/enable', false );
159
				return \Aimeos\MW\Cache\Factory::createManager( 'None', array(), null );
160
161
			case 'Flow':
162
				return new \Aimeos\MAdmin\Cache\Proxy\Flow( $context, $this->cache );
163
164
			default:
165
				return new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
166
		}
167
	}
168
169
170
	/**
171
	 * Creates a new configuration object.
172
	 *
173
	 * @return \Aimeos\MW\Config\Iface Configuration object
174
	 */
175
	protected function getConfig()
176
	{
177
		$this->settings['resource']['db']['host'] = $this->resource['host'];
178
		$this->settings['resource']['db']['database'] = $this->resource['dbname'];
179
		$this->settings['resource']['db']['username'] = $this->resource['user'];
180
		$this->settings['resource']['db']['password'] = $this->resource['password'];
181
182
		$configPaths = $this->aimeos->get()->getConfigPaths();
183
		$config = new \Aimeos\MW\Config\PHPArray( $this->settings, $configPaths );
184
185
		$apc = (bool) ( isset( $this->settings['flow']['apc']['enable'] ) ? $this->settings['flow']['apc']['enable'] : false );
186
		$prefix = (string) ( isset( $this->settings['flow']['apc']['prefix'] ) ? $this->settings['flow']['apc']['prefix'] : 'flow:' );
187
188
		if( function_exists( 'apc_store' ) === true && $apc === true ) {
189
			$config = new \Aimeos\MW\Config\Decorator\APC( $config, $prefix );
190
		}
191
192
		return $config;
193
	}
194
195
196
	/**
197
	 * Returns the locale item for the current request
198
	 *
199
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
200
	 * @param \TYPO3\Flow\Mvc\RequestInterface $request Request object
201
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item object
202
	 */
203
	protected function getLocale( \Aimeos\MShop\Context\Item\Iface $context, \TYPO3\Flow\Mvc\RequestInterface $request )
204
	{
205
		if( $this->locale === null )
206
		{
207
			$params = $request->getArguments();
208
209
			$site = ( isset( $params['site'] ) ? $params['site'] : 'default' );
210
			$lang = ( isset( $params['locale'] ) ? $params['locale'] : '' );
211
			$currency = ( isset( $params['currency'] ) ? $params['currency'] : '' );
212
213
			$disableSites = (bool) ( isset( $this->settings['flow']['disableSites'] ) ? $this->settings['flow']['disableSites'] : false );
214
215
			$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
216
			$this->locale = $localeManager->bootstrap( $site, $lang, $currency, $disableSites );
217
		}
218
219
		return $this->locale;
220
	}
221
222
223
	/**
224
	 * Inject the settings
225
	 *
226
	 * @param array $settings
227
	 * @return void
228
	 */
229
	public function injectSettings( array $settings )
230
	{
231
		$this->settings = $settings;
232
	}
233
234
235
	/**
236
	 * Sets the Aimeos shop cache
237
	 *
238
	 * @param \TYPO3\Flow\Cache\Frontend\StringFrontend $cache Cache for shop data
239
	 * @return void
240
	 */
241
	public function setCache( \TYPO3\Flow\Cache\Frontend\StringFrontend $cache )
242
	{
243
		$this->cache = $cache;
244
	}
245
}
246