Completed
Push — master ( 4e3077...d2ec78 )
by Aimeos
15:45
created

Cache::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package Slim
7
 * @subpackage Command
8
 */
9
10
namespace Aimeos\Slim\Command;
11
12
13
/**
14
 * Aimeos cache command class
15
 *
16
 * @package Slim
17
 * @subpackage Command
18
 */
19
class Cache extends Base implements Iface
20
{
21
	/**
22
	 * Returns the command usage and options
23
	 *
24
	 * @return string Command usage and options
25
	 */
26
	public static function usage()
27
	{
28
		return "Usage: php cache.php [--extdir=<path>]* [--config=<path>|<file>]* [\"sitecode1 [sitecode2]*\"]\n";
29
	}
30
31
32
	/**
33
	 * Executes the command
34
	 *
35
	 * @param array $argv Associative array from $_SERVER['argv']
36
	 */
37
	public static function run( array $argv )
38
	{
39
		array_shift( $argv );
40
		$options = self::getOptions( $argv );
41
		$sites = array_shift( $argv );
42
43
		$extdirs = ( isset( $options['extdir'] ) ? (array) $options['extdir'] : array() );
44
		$aimeos = new \Aimeos\Bootstrap( $extdirs );
45
46
		$ctx = self::getContext( $aimeos->getConfigPaths(), $options );
47
		$siteItems = self::getSiteItems( $ctx, $sites );
48
49
		self::clear( $ctx, $siteItems );
50
	}
51
52
53
	/**
54
	 * Returns a new context object
55
	 *
56
	 * @param array $confPaths List of configuration paths from the bootstrap object
57
	 * @param array $options Associative list of configuration options as key/value pairs
58
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
59
	 */
60
	protected static function getContext( array $confPaths, array $options )
61
	{
62
		$config = array();
63
		$ctx = new \Aimeos\MShop\Context\Item\Standard();
64
65 View Code Duplication
		if( isset( $options['config'] ) )
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
		{
67
			foreach( (array) $options['config'] as $path )
68
			{
69
				if( is_file( $path ) ) {
70
					$config = array_replace_recursive( $config, require $path );
71
				} else {
72
					$confPaths[] = $path;
73
				}
74
			}
75
		}
76
77
		$conf = new \Aimeos\MW\Config\PHPArray( $config, $confPaths );
78
		$conf = new \Aimeos\MW\Config\Decorator\Memory( $conf );
79
		$ctx->setConfig( $conf );
80
81
		$dbm = new \Aimeos\MW\DB\Manager\PDO( $conf );
82
		$ctx->setDatabaseManager( $dbm );
83
84
		$logger = new \Aimeos\MW\Logger\Errorlog( \Aimeos\MW\Logger\Base::INFO );
85
		$ctx->setLogger( $logger );
86
87
		return $ctx;
88
	}
89
90
91
	/**
92
	 * Removes the cached data for the given sites
93
	 *
94
	 * @param \Aimeos\MShop\Context\Item\Iface $ctx Context object
95
	 * @param array $siteItems List of site items implementing \Aimeos\MShop\Locale\Site\Iface
96
	 */
97
	protected static function clear( \Aimeos\MShop\Context\Item\Iface $ctx, array $siteItems )
98
	{
99
		$localeManager = \Aimeos\MShop\Factory::createManager( $ctx, 'locale' );
100
101
		foreach( $siteItems as $siteItem )
102
		{
103
			$localeItem = $localeManager->bootstrap( $siteItem->getCode(), '', '', false );
104
105
			$lcontext = clone $ctx;
106
			$lcontext->setLocale( $localeItem );
107
108
			$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $lcontext );
109
			$lcontext->setCache( $cache );
110
111
			printf( "Clearing the Aimeos cache for site \"%1\$s\"\n", $siteItem->getCode() );
112
113
			\Aimeos\MAdmin\Cache\Manager\Factory::createManager( $lcontext )->getCache()->flush();
114
		}
115
	}
116
}