Issues (251)

Setup.php (3 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2024
6
 */
7
8
9
namespace Aimeos;
10
11
12
/**
13
 * Setup Aimeos data structures and storeages
14
 */
15
class Setup
16
{
17
	private $bootstrap;
18
	private $context;
19
	private $config;
20
	private $verbose;
21
22
23
	/**
24
	 * Initializes the Aimeos setup object
25
	 *
26
	 * @param Bootstrap $bootstrap Aimeos bootstrap object
27
	 * @param array $config Associative list of config keys and values
28
	 */
29
	public function __construct( Bootstrap $bootstrap, array $config = [] )
30
	{
31
		$this->bootstrap = $bootstrap;
32
		$this->config = $config;
33
34
		$this->macros();
35
	}
36
37
38
	/**
39
	 * Creates a new initialized setup object
40
	 *
41
	 * @param Bootstrap $bootstrap Aimeos bootstrap object
42
	 * @param array $config Associative list of config keys and values
43
	 * @return self Aimeos setup object
44
	 */
45
	public static function use( Bootstrap $bootstrap, array $config = [] ) : self
46
	{
47
		return new static( $bootstrap, $config );
48
	}
49
50
51
	/**
52
	 * Sets a custom context object
53
	 *
54
	 * @param \Aimeos\MShop\ContextIface $context Context object
55
	 * @return self Same object for fluid method calls
56
	 */
57
	public function context( \Aimeos\MShop\ContextIface $context ) : self
58
	{
59
		$this->context = $context;
60
		return $this;
61
	}
62
63
64
	/**
65
	 * Performs the migrations
66
	 *
67
	 * @param string $site Site code to execute the migrations for
68
	 * @param string $template Name of the migration template
69
	 */
70
	public function up( string $site = 'default', string $template = 'default' )
71
	{
72
		$ctx = ( $this->context ?? $this->createContext() )->setEditor( 'setup' );
73
74
		\Aimeos\Upscheme\Task\Base::macro( 'context', function() use ( $ctx ) {
75
			return $ctx;
76
		} );
77
78
79
		$config = $ctx->config();
80
		$config->set( 'setup/site', $site );
81
		$dbconf = $this->getDBConfig( $config );
82
		$taskPaths = $this->bootstrap->getSetupPaths( $template );
83
84
		\Aimeos\Upscheme\Up::use( $dbconf, $taskPaths )->verbose( $this->verbose )->up();
85
	}
86
87
88
	/**
89
	 * Sets the verbosity level
90
	 *
91
	 * @param mixed $level Verbosity level (empty: none, v: notice, vv: info, vvv: debug)
92
	 * @return self Same object for fluid method calls
93
	 */
94
	public function verbose( $level = 'v' ) : self
95
	{
96
		$this->verbose = $level;
97
		return $this;
98
	}
99
100
101
	/**
102
	 * Returns a new context object
103
	 *
104
	 * @return \Aimeos\MShop\ContextIface New context object
105
	 */
106
	protected function createContext() : \Aimeos\MShop\ContextIface
107
	{
108
		$ctx = new \Aimeos\MShop\Context();
109
110
		$conf = new \Aimeos\Base\Config\PHPArray( [], $this->bootstrap->getConfigPaths() );
111
112
		foreach( $this->config as $key => $value ) {
113
			$conf->set( $key, $value );
114
		}
115
116
		$conf = new \Aimeos\Base\Config\Decorator\Memory( $conf );
117
		$ctx->setConfig( $conf );
118
119
		$dbm = new \Aimeos\Base\DB\Manager\Standard( $conf->get( 'resource', [] ), 'DBAL' );
120
		$ctx->setDatabaseManager( $dbm );
121
122
		$fsm = new \Aimeos\Base\Filesystem\Manager\Standard( $conf->get( 'resource', [] ) );
123
		$ctx->setFilesystemManager( $fsm );
124
125
		$logger = new \Aimeos\Base\Logger\Errorlog( \Aimeos\Base\Logger\Iface::INFO );
126
		$ctx->setLogger( $logger );
127
128
		$password = new \Aimeos\Base\Password\Standard();
129
		$ctx->setPassword( $password );
130
131
		$session = new \Aimeos\Base\Session\None();
132
		$ctx->setSession( $session );
133
134
		$cache = new \Aimeos\Base\Cache\None();
135
		$ctx->setCache( $cache );
136
137
		$process = new \Aimeos\Base\Process\Pcntl( $conf->get( 'pcntl_max', 4 ), $conf->get( 'pcntl_priority', 19 ) );
138
		$process = new \Aimeos\Base\Process\Decorator\Check( $process );
139
		$ctx->setProcess( $process );
140
141
		return $ctx;
142
	}
143
144
145
	/**
146
	 * Returns the database configuration
147
	 *
148
	 * @param \Aimeos\Base\Config\Iface $conf Configuration object
149
	 * @return array Database configuration
150
	 */
151
	protected function getDBConfig( \Aimeos\Base\Config\Iface $conf ) : array
152
	{
153
		$dbconfig = $conf->get( 'resource', [] );
154
155
		foreach( $dbconfig as $rname => $entry )
156
		{
157
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
158
				unset( $dbconfig[$rname] );
159
			}
160
		}
161
162
		return $dbconfig;
163
	}
164
165
166
	/**
167
	 * Adds the required marcos to the Upscheme objects
168
	 */
169
	protected function macros()
170
	{
171
		\Aimeos\Upscheme\Up::macro( 'connect', function( array $cfg ) {
172
173
			switch( $cfg['adapter'] )
174
			{
175
				case 'mysql': $cfg['driver'] = 'pdo_mysql'; break;
176
				case 'oracle': $cfg['driver'] = 'pdo_oci'; break;
177
				case 'pgsql': $cfg['driver'] = 'pdo_pgsql'; break;
178
				case 'sqlsrv': $cfg['driver'] = 'pdo_sqlsrv'; break;
179
				default: $cfg['driver'] = $cfg['adapter'];
180
			}
181
182
			if( isset( $cfg['database'] ) ) {
183
				$cfg['dbname'] = $cfg['database'];
184
			}
185
186
			if( isset( $cfg['username'] ) ) {
187
				$cfg['user'] = $cfg['username'];
188
			}
189
190
			unset( $cfg['adapter'], $cfg['database'], $cfg['username'] );
191
192
			return \Doctrine\DBAL\DriverManager::getConnection( $cfg );
193
		} );
194
195
196
		$codelen = $this->config['codelength'] ?? 64;
197
198
		\Aimeos\Upscheme\Schema\Table::macro( 'startend', function() {
199
			$this->datetime( 'start' )->null( true );
0 ignored issues
show
The method datetime() does not exist on Aimeos\Setup. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

199
			$this->/** @scrutinizer ignore-call */ 
200
          datetime( 'start' )->null( true );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
200
			return $this->datetime( 'end' )->null( true );
201
		} );
202
203
		\Aimeos\Upscheme\Schema\Table::macro( 'code', function( string $name = 'code' ) use ( $codelen ) {
204
			return $this->string( $name, $codelen )
0 ignored issues
show
The method string() does not exist on Aimeos\Setup. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

204
			return $this->/** @scrutinizer ignore-call */ string( $name, $codelen )

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
205
				->opt( 'charset', 'utf8mb4', 'mysql' )
206
				->opt( 'collation', 'utf8mb4_bin', 'mysql' )
207
				->default( '' );
208
		} );
209
210
		\Aimeos\Upscheme\Schema\Table::macro( 'config', function( string $name = 'config' ) {
211
			return $this->text( $name )
0 ignored issues
show
The method text() does not exist on Aimeos\Setup. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
			return $this->/** @scrutinizer ignore-call */ text( $name )

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
212
				->opt( 'charset', 'utf8mb4', 'mysql' )
213
				->opt( 'collation', 'utf8mb4_general_ci', 'mysql' )
214
				->null( true );
215
		} );
216
217
		\Aimeos\Upscheme\Schema\Table::macro( 'type', function( string $name = 'type' ) use ( $codelen ) {
218
			return $this->string( $name, $codelen )
219
				->opt( 'charset', 'utf8mb4', 'mysql' )
220
				->opt( 'collation', 'utf8mb4_bin', 'mysql' )
221
				->default( '' );
222
		} );
223
224
		\Aimeos\Upscheme\Schema\Table::macro( 'refid', function( string $name = 'refid' ) {
225
			return $this->string( $name, 36 )
226
				->opt( 'charset', 'utf8mb4', 'mysql' )
227
				->opt( 'collation', 'utf8mb4_bin', 'mysql' )
228
				->default( '' );
229
		} );
230
231
		\Aimeos\Upscheme\Schema\Table::macro( 'i18n', function( string $name = 'i18n' ) {
232
			return $this->text( $name )
233
				->opt( 'charset', 'utf8mb4', 'mysql' )
234
				->opt( 'collation', 'utf8mb4_bin', 'mysql' )
235
				->null( true );
236
		} );
237
238
		\Aimeos\Upscheme\Schema\Table::macro( 'meta', function() {
239
			$this->datetime( 'mtime' );
240
			$this->datetime( 'ctime' );
241
			return $this->string( 'editor' );
242
		} );
243
	}
244
}
245