Completed
Push — master ( b6ebb2...06767e )
by Aimeos
01:46
created

SetupCommand::getDbConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package laravel
7
 * @subpackage Command
8
 */
9
10
11
namespace Aimeos\Shop\Command;
12
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Input\InputArgument;
15
16
17
/**
18
 * Command for initializing or updating the Aimeos database tables
19
 * @package laravel
20
 * @subpackage Command
21
 */
22
class SetupCommand extends AbstractCommand
23
{
24
	/**
25
	 * The name and signature of the console command.
26
	 *
27
	 * @var string
28
	 */
29
	protected $signature = 'aimeos:setup
30
		{site=default : Site for updating database entries}
31
		{tplsite=default : Site used as template for creating the new one}
32
		{--task : Name of the setup task that should be executed}
33
		{--action=migrate : Action name that should be executed, i.e. "migrate", "rollback", "clean"}
34
		{--option= : Setup configuration, name and value are separated by ":" like "setup/default/demo:1"}
35
	';
36
37
	/**
38
	 * The console command description.
39
	 *
40
	 * @var string
41
	 */
42
	protected $description = 'Initialize or update the Aimeos database tables';
43
44
45
	/**
46
	 * Execute the console command.
47
	 *
48
	 * @return mixed
49
	 */
50
	public function handle()
51
	{
52
		$ctx = $this->getLaravel()->make( '\Aimeos\Shop\Base\Context' )->get( false, 'command' );
53
		$ctx->setEditor( 'aimeos:setup' );
54
55
		$config = $ctx->getConfig();
56
		$site = $this->argument( 'site' );
57
		$template = $this->argument( 'tplsite' );
58
59
		$config->set( 'setup/site', $site );
60
		$dbconfig = $this->getDbConfig( $config );
61
		$this->setOptions( $config );
62
63
		$taskPaths = $this->getLaravel()->make( '\Aimeos\Shop\Base\Aimeos' )->get()->getSetupPaths( $template );
64
		$manager = new \Aimeos\MW\Setup\Manager\Multiple( $ctx->getDatabaseManager(), $dbconfig, $taskPaths, $ctx );
65
66
		$this->info( sprintf( 'Initializing or updating the Aimeos database tables for site "%1$s"', $site ) );
67
68
		if( ( $task = $this->option( 'task' ) ) && is_array( $task ) ) {
69
			$task = reset( $task );
70
		}
71
72
		switch( $this->option( 'action' ) )
73
		{
74
			case 'migrate':
75
				$manager->migrate( $task );
76
				break;
77
			case 'rollback':
78
				$manager->rollback( $task );
79
				break;
80
			case 'clean':
81
				$manager->clean( $task );
82
				break;
83
			default:
84
				throw new \Exception( sprintf( 'Invalid setup action "%1$s"', $this->option( 'action' ) ) );
85
		}
86
	}
87
88
89
	/**
90
	 * Returns the database configuration from the config object.
91
	 *
92
	 * @param \Aimeos\MW\Config\Iface $conf Config object
93
	 * @return array Multi-dimensional associative list of database configuration parameters
94
	 */
95
	protected function getDbConfig( \Aimeos\MW\Config\Iface $conf )
96
	{
97
		$dbconfig = $conf->get( 'resource', array() );
98
99
		foreach( $dbconfig as $rname => $dbconf )
100
		{
101
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
102
				unset( $dbconfig[$rname] );
103
			}
104
		}
105
106
		return $dbconfig;
107
	}
108
109
110
	/**
111
	 * Extracts the configuration options from the input object and updates the configuration values in the config object.
112
	 *
113
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
114
	 * @param array Associative list of database configurations
115
	 * @throws \RuntimeException If the format of the options is invalid
116
	 */
117
	protected function setOptions( \Aimeos\MW\Config\Iface $conf )
118
	{
119
		foreach( (array) $this->option( 'option' ) as $option )
120
		{
121
			list( $name, $value ) = explode( ':', $option );
122
			$conf->set( str_replace( '\\', '/', $name ), $value );
123
		}
124
	}
125
}
126