Completed
Push — master ( ece81b...0332d8 )
by Aimeos
01:48
created

AimeosCommandController::autoload()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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