Completed
Push — master ( e0d55d...a2d63d )
by Aimeos
01:42
created

AimeosCommandController::getSiteItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
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 Command
8
 */
9
10
11
namespace Aimeos\Shop\Command;
12
13
use TYPO3\Flow\Annotations as Flow;
14
15
16
/**
17
 * Aimeos CLI controller for cronjobs
18
 *
19
 * @package flow
20
 * @subpackage Command
21
 * @Flow\Scope("singleton")
22
 */
23
class AimeosCommandController extends \TYPO3\Flow\Cli\CommandController
24
{
25
	/**
26
	 * @var string
27
	 * @Flow\Inject(setting="http.baseUri", package="TYPO3.Flow")
28
	 */
29
	protected $baseUri;
30
31
	/**
32
	 * @var \TYPO3\Flow\Cache\Frontend\StringFrontend
33
	 */
34
	protected $cache;
35
36
	/**
37
	 * @var \TYPO3\Flow\Object\ObjectManagerInterface
38
	 */
39
	protected $objectManager;
40
41
42
	/**
43
	 * Clears the content cache
44
	 *
45
	 * @param string $sites List of sites separated by a space character the jobs should be executed for, e.g. "default unittest"
46
	 * @return void
47
	 */
48
	public function cacheCommand( $sites = '' )
49
	{
50
		$context = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\Context' )->get( null, 'command' );
51
		$context->setEditor( 'aimeos:cache' );
52
53
		$config = $context->getConfig();
54
		$name = $config->get( 'flow/cache/name', 'Flow' );
55
56
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
57
58
		foreach( $this->getSiteItems( $context, $sites ) as $siteItem )
59
		{
60
			$localeItem = $localeManager->bootstrap( $siteItem->getCode(), '', '', false );
61
62
			$lcontext = clone $context;
63
			$lcontext->setLocale( $localeItem );
64
65
			switch( $name )
66
			{
67
				case 'None':
68
					$config->set( 'client/html/basket/cache/enable', false );
69
					$cache = \Aimeos\MW\Cache\Factory::createManager( 'None', array(), null );
70
					break;
71
				case 'Flow':
72
					$cache = new \Aimeos\MAdmin\Cache\Proxy\Flow( $lcontext, $this->cache );
73
					break;
74
				default:
75
					$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $lcontext );
76
					break;
77
			}
78
79
			$this->outputFormatted( 'Clearing the Aimeos cache for site <b>%s</b>', array( $siteItem->getCode() ) );
80
81
			$cache->flush();
82
		}
83
	}
84
85
86
	/**
87
	 * Executes the Aimeos maintenance jobs
88
	 *
89
	 * The Aimeos shop system needs some maintenance tasks that must be
90
	 * regularly executed. These include
91
	 *
92
	 * - admin/cache (remove expired cache entries once a day)
93
	 * - admin/job (process import/export jobs created in the admin interface every five minutes)
94
	 * - admin/log (archivate and delete old log entries once a day)
95
	 * - customer/email/watch (send customers e-mails if their watched products have changed)
96
	 * - index/rebuild (rebuild the catalog index once a day after midnight)
97
	 * - index/optimize (optimize the catalog index once a day one hour after the rebuild)
98
	 * - order/cleanup/unfinished (remove unfinised orders once a day)
99
	 * - order/cleanup/unfinised (remove unpaid orders once a day)
100
	 * - order/email/delivery (send delivery status update e-mails to the customers every few hours)
101
	 * - order/email/payment (send payment status update e-mails to the customers every few hours)
102
	 * - order/service/async (import batch delivery or payment status updates if necessary)
103
	 * - order/service/delivery (sends paid orders to the ERP system or logistic partner)
104
	 * - order/service/payment (captures authorized payments after the configured amount of time automatically)
105
	 * - product/bought (updates the suggested products based on what other customers bought once a day)
106
	 * - product/export (export products)
107
	 * - product/export/sitemap (generate product sitemaps for search engines)
108
	 * - product/import/csv (import products from CSV files)
109
	 *
110
	 * Each of these maintenance tasks must be executed for all shop instances
111
	 * if you have more than one site in your installation. The sites parameter
112
	 * should contain a list of site codes in this case. If you only have one
113
	 * site named "default" then you don't need to specify the site.
114
	 *
115
	 * @param string $jobs List of job names separated by a space character like "admin/job catalog/index/rebuild"
116
	 * @param string $sites List of sites separated by a space character the jobs should be executed for, e.g. "default unittest"
117
	 * @return void
118
	 */
119
	public function jobsCommand( $jobs, $sites = 'default' )
120
	{
121
		$aimeos = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\Aimeos' )->get();
122
		$context = $this->getContext();
123
124
		$jobs = explode( ' ', $jobs );
125
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
126
127
		foreach( $this->getSiteItems( $context, $sites ) as $siteItem )
128
		{
129
			$localeItem = $localeManager->bootstrap( $siteItem->getCode(), '', '', false );
130
			$localeItem->setLanguageId( null );
131
			$localeItem->setCurrencyId( null );
132
133
			$context->setLocale( $localeItem );
134
135
			$this->outputFormatted( 'Executing jobs for site <b>%s</b>', array( $siteItem->getCode() ) );
136
137
			foreach( $jobs as $jobname )
138
			{
139
				$this->outputFormatted( '  <b>%s</b>', array( $jobname ) );
140
				\Aimeos\Controller\Jobs\Factory::createController( $context, $aimeos, $jobname )->run();
141
			}
142
		}
143
	}
144
145
146
	/**
147
	 * Initialize or update the Aimeos database tables
148
	 *
149
	 * After installing and updating the Aimeos package, the database structure
150
	 * must be created or upgraded to the current version. Depending on the size
151
	 * of the database, this may take a while.
152
	 *
153
	 * @param string $site Site for updating database entries
154
	 * @param string $tplsite Template site for creating or updating database entries
155
	 * @param array $option Optional setup configuration, name and value are separated by ":" like "setup/default/demo:1".
156
	 * @param string|null $task Setup task name to execute
157
	 * @param string $action Setup task action, e.g. "migrate", "rollback" or "clean"
158
	 * @return void
159
	 */
160
	public function setupCommand( $site = 'default', $tplsite = 'default', array $option = array(), $task = null, $action = 'migrate' )
161
	{
162
		$context = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\Context' )->get( null, 'command' );
163
		$context->setEditor( 'aimeos:setup' );
164
165
		$config = $context->getConfig();
166
		$config->set( 'setup/site', $site );
167
		$dbconfig = $this->getDbConfig( $config );
168
		$this->setOptions( $config, $option );
169
170
		$taskPaths = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\Aimeos' )->get()->getSetupPaths( $tplsite );
171
		$manager = new \Aimeos\MW\Setup\Manager\Multiple( $context->getDatabaseManager(), $dbconfig, $taskPaths, $context );
172
173
		$this->outputFormatted( 'Initializing or updating the Aimeos database tables for site <b>%s</b>', array( $site ) );
174
175
		switch( $action )
176
		{
177
			case 'migrate':
178
				$manager->migrate( $task );
179
				break;
180
			case 'rollback':
181
				$manager->rollback( $task );
182
				break;
183
			case 'clean':
184
				$manager->clean( $task );
185
				break;
186
			default:
187
				throw new \Exception( sprintf( 'Invalid setup action "%1$s"', $action ) );
188
		}
189
	}
190
191
192
	/**
193
	 * Returns a context object for the jobs command
194
	 *
195
	 * @return \Aimeos\MShop\Context\Item\Standard Context object
196
	 */
197
	protected function getContext()
198
	{
199
		$aimeos = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\Aimeos' )->get();
200
		$context = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\Context' )->get( null, 'command' );
201
		$uriBuilder = $this->objectManager->get( '\\TYPO3\\Flow\\Mvc\\Routing\\UriBuilder' );
202
203
		$request = \TYPO3\Flow\Http\Request::createFromEnvironment();
204
		$request->setBaseUri( new \TYPO3\Flow\Http\Uri( $this->baseUri ) );
205
		$uriBuilder->setRequest( new \TYPO3\Flow\Mvc\ActionRequest( $request ) );
206
207
		$tmplPaths = $aimeos->getCustomPaths( 'controller/jobs/templates' );
208
209
		$langManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context )->getSubManager( 'language' );
210
		$langids = array_keys( $langManager->searchItems( $langManager->createSearch( true ) ) );
211
212
		$i18n = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\I18n' )->get( $langids );
213
		$view = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\View' )->create( $context, $uriBuilder, $tmplPaths );
214
215
		$context->setEditor( 'aimeos:jobs' );
216
		$context->setView( $view );
217
		$context->setI18n( $i18n );
218
219
		return $context;
220
	}
221
222
223
	/**
224
	 * Returns the database configuration from the config object.
225
	 *
226
	 * @param \Aimeos\MW\Config\Iface $conf Config object
227
	 * @return array Multi-dimensional associative list of database configuration parameters
228
	 */
229
	protected function getDbConfig( \Aimeos\MW\Config\Iface $conf )
230
	{
231
		$dbconfig = $conf->get( 'resource', array() );
232
233
		foreach( $dbconfig as $rname => $dbconf )
234
		{
235
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
236
				unset( $dbconfig[$rname] );
237
			}
238
		}
239
240
		return $dbconfig;
241
	}
242
243
244
	/**
245
	 * Returns the enabled site items which may be limited by the input arguments.
246
	 *
247
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
248
	 * @param string $sites Unique site codes
249
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface[] List of site items
250
	 */
251
	protected function getSiteItems( \Aimeos\MShop\Context\Item\Iface $context, $sites )
252
	{
253
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'locale/site' );
254
		$search = $manager->createSearch();
255
256
		if( $sites !== '' ) {
257
			$search->setConditions( $search->compare( '==', 'locale.site.code', explode( ' ', $sites ) ) );
258
		}
259
260
		return $manager->searchItems( $search );
261
	}
262
263
264
	/**
265
	 * @param \TYPO3\Flow\Cache\Frontend\StringFrontend $cache
266
	 * @return void
267
	 */
268
	public function injectCache( \TYPO3\Flow\Cache\Frontend\StringFrontend $cache )
269
	{
270
		$this->cache = $cache;
271
	}
272
273
274
	/**
275
	 * @param \TYPO3\Flow\Object\ObjectManagerInterface $objectManager
276
	 * @return void
277
	 */
278
	public function injectObjectManager( \TYPO3\Flow\Object\ObjectManagerInterface $objectManager )
279
	{
280
		$this->objectManager = $objectManager;
281
	}
282
283
284
	/**
285
	 * Extracts the configuration options from the input object and updates the configuration values in the config object.
286
	 *
287
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
288
	 * @param array $options List of option key/value pairs
289
	 * @param array Associative list of database configurations
290
	 */
291
	protected function setOptions( \Aimeos\MW\Config\Iface $conf, array $options )
292
	{
293
		foreach( $options as $option )
294
		{
295
			list( $name, $value ) = explode( ':', $option );
296
			$conf->set( str_replace( '\\', '/', $name ), $value );
297
		}
298
	}
299
}
300