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

cache.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 */
7
8
9
if( php_sapi_name() != 'cli' ) {
10
	exit( 'Setup can only be started via command line for security reasons' );
11
}
12
13
ini_set( 'display_errors', 1 );
14
date_default_timezone_set( 'UTC' );
15
16
17
18
/**
19
 * Returns the command options given by the user
20
 *
21
 * @param array &$params List of parameters
22
 * @return array Associative list of option name and value(s)
23
 */
24 View Code Duplication
function getOptions( array &$params )
0 ignored issues
show
This function seems to be duplicated in 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...
25
{
26
	$options = array();
27
28
	foreach( $params as $key => $option )
29
	{
30
		if( $option === '--help' ) {
31
			usage();
32
		}
33
34
		if( strncmp( $option, '--', 2 ) === 0 && ( $pos = strpos( $option, '=', 2 ) ) !== false )
35
		{
36
			if( ( $name = substr( $option, 2, $pos - 2 ) ) !== false )
37
			{
38
				if( isset( $options[$name] ) )
39
				{
40
					$options[$name] = (array) $options[$name];
41
					$options[$name][] = substr( $option, $pos + 1 );
42
				}
43
				else
44
				{
45
					$options[$name] = substr( $option, $pos + 1 );
46
				}
47
48
				unset( $params[$key] );
49
			}
50
			else
51
			{
52
				printf( "Invalid option \"%1\$s\"\n", $option );
53
				usage();
54
			}
55
		}
56
	}
57
58
	return $options;
59
}
60
61
62
/**
63
 * Returns a new context object
64
 *
65
 * @param array $confPaths List of configuration paths from the bootstrap object
66
 * @param array $options Associative list of configuration options as key/value pairs
67
 * @return \Aimeos\MShop\Context\Item\Iface Context object
68
 */
69
function getContext( array $confPaths, array $options )
70
{
71
	$config = array();
72
	$ctx = new \Aimeos\MShop\Context\Item\Standard();
73
74 View Code Duplication
	if( isset( $options['config'] ) )
1 ignored issue
show
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...
75
	{
76
		foreach( (array) $options['config'] as $path )
77
		{
78
			if( is_file( $path ) ) {
79
				$config = array_replace_recursive( $config, require $path );
80
			} else {
81
				$confPaths[] = $path;
82
			}
83
		}
84
	}
85
86
	$conf = new \Aimeos\MW\Config\PHPArray( $config, $confPaths );
87
	$conf = new \Aimeos\MW\Config\Decorator\Memory( $conf );
88
	$ctx->setConfig( $conf );
89
90
	$dbm = new \Aimeos\MW\DB\Manager\PDO( $conf );
91
	$ctx->setDatabaseManager( $dbm );
92
93
	$logger = new \Aimeos\MW\Logger\Errorlog( \Aimeos\MW\Logger\Base::INFO );
94
	$ctx->setLogger( $logger );
95
96
	return $ctx;
97
}
98
99
100
/**
101
 * Returns the locale site items for the given site code string
102
 *
103
 * @param \Aimeos\MShop\Context\Item\Iface $ctx Context object
104
 * @param string|null $sites List of site codes separated by a space
105
 */
106 View Code Duplication
function getSiteItems( \Aimeos\MShop\Context\Item\Iface $ctx, $sites )
0 ignored issues
show
This function seems to be duplicated in 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...
107
{
108
	$manager = \Aimeos\MShop\Factory::createManager( $ctx, 'locale/site' );
109
	$search = $manager->createSearch();
110
111
	if( is_scalar( $sites ) && $sites != '' ) {
112
		$sites = explode( ' ', $sites );
113
	}
114
115
	if( !empty( $sites ) ) {
116
		$search->setConditions( $search->compare( '==', 'locale.site.code', $sites ) );
117
	}
118
119
	return $manager->searchItems( $search );
120
}
121
122
123
/**
124
 * Removes the cached data for the given sites
125
 *
126
 * @param \Aimeos\MShop\Context\Item\Iface $ctx Context object
127
 * @param array $siteItems List of site items implementing \Aimeos\MShop\Locale\Site\Iface
128
 */
129
function clear( \Aimeos\MShop\Context\Item\Iface $ctx, array $siteItems )
130
{
131
	$localeManager = \Aimeos\MShop\Factory::createManager( $ctx, 'locale' );
132
133
	foreach( $siteItems as $siteItem )
134
	{
135
		$localeItem = $localeManager->bootstrap( $siteItem->getCode(), '', '', false );
136
137
		$lcontext = clone $ctx;
138
		$lcontext->setLocale( $localeItem );
139
140
		$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $lcontext );
141
		$lcontext->setCache( $cache );
142
143
		printf( "Clearing the Aimeos cache for site \"%1\$s\"\n", $siteItem->getCode() );
144
145
		\Aimeos\MAdmin\Cache\Manager\Factory::createManager( $lcontext )->getCache()->flush();
146
	}
147
}
148
149
150
/**
151
 * Prints the command usage and options, exits the program after printing
152
 */
153
function usage()
154
{
155
	printf( "Usage: php cache.php [--extdir=<path>]* [--config=<path>|<file>]* [\"sitecode1 [sitecode2]*\"]\n" );
156
	exit( 1 );
0 ignored issues
show
Coding Style Compatibility introduced by
The function usage() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
157
}
158
159
160
try
161
{
162
	$params = $_SERVER['argv'];
163
	array_shift( $params );
164
165
	$options = getOptions( $params );
166
	$sites = array_shift( $params );
167
168
	require 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
169
170
	$aimeos = new \Aimeos\Bootstrap( ( isset( $options['extdir'] ) ? (array) $options['extdir'] : array() ) );
171
	$ctx = getContext( $aimeos->getConfigPaths(), $options );
172
	$siteItems = getSiteItems( $ctx, $sites );
173
174
	clear( $ctx, $siteItems );
175
}
176
catch( \Throwable $t )
177
{
178
	echo "\n\nCaught PHP error while processing setup";
179
	echo "\n\nMessage:\n";
180
	echo $t->getMessage();
181
	echo "\n\nStack trace:\n";
182
	echo $t->getTraceAsString();
183
	echo "\n\n";
184
	exit( 1 );
185
}
186
catch( \Exception $e )
187
{
188
	echo "\n\nCaught exception while processing setup";
189
	echo "\n\nMessage:\n";
190
	echo $e->getMessage();
191
	echo "\n\nStack trace:\n";
192
	echo $e->getTraceAsString();
193
	echo "\n\n";
194
	exit( 1 );
195
}
196