Completed
Push — master ( 456d5e...a86294 )
by Aimeos
02:08
created

SetupCommand::autoload()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 9

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 8.8571
cc 5
eloc 9
nc 3
nop 1
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 console command name.
26
	 *
27
	 * @var string
28
	 */
29
	protected $name = 'aimeos:setup';
30
31
	/**
32
	 * The console command description.
33
	 *
34
	 * @var string
35
	 */
36
	protected $description = 'Initialize or update the Aimeos database tables';
37
38
39
	/**
40
	 * Execute the console command.
41
	 *
42
	 * @return mixed
43
	 */
44
	public function fire()
45
	{
46
		$ctx = $this->getLaravel()->make( '\Aimeos\Shop\Base\Context' )->get( false, 'command' );
47
		$ctx->setEditor( 'aimeos:setup' );
48
49
		$config = $ctx->getConfig();
50
		$site = $this->argument( 'site' );
51
		$template = $this->argument( 'tplsite' );
52
53
		$config->set( 'setup/site', $site );
54
		$dbconfig = $this->getDbConfig( $config );
55
		$this->setOptions( $config );
56
57
		$taskPaths = $this->getLaravel()->make( '\Aimeos\Shop\Base\Aimeos' )->get()->getSetupPaths( $template );
58
		$manager = new \Aimeos\MW\Setup\Manager\Multiple( $ctx->getDatabaseManager(), $dbconfig, $taskPaths, $ctx );
59
60
		$this->info( sprintf( 'Initializing or updating the Aimeos database tables for site "%1$s"', $site ) );
61
62
		if( ( $task = $this->option( 'task' ) ) && is_array( $task ) ) {
63
			$task = reset( $task );
64
		}
65
66
		switch( $this->option( 'action' ) )
67
		{
68
			case 'migrate':
69
				$manager->migrate( $task );
70
				break;
71
			case 'rollback':
72
				$manager->rollback( $task );
73
				break;
74
			case 'clean':
75
				$manager->clean( $task );
76
				break;
77
			default:
78
				throw new \Exception( sprintf( 'Invalid setup action "%1$s"', $this->option( 'action' ) ) );
79
		}
80
	}
81
82
83
	/**
84
	 * Get the console command arguments.
85
	 *
86
	 * @return array
87
	 */
88
	protected function getArguments()
89
	{
90
		return array(
91
			array( 'site', InputArgument::OPTIONAL, 'Site for updating database entries', 'default' ),
92
			array( 'tplsite', InputArgument::OPTIONAL, 'Site used as template for creating the new one', 'default' ),
93
		);
94
	}
95
96
97
	/**
98
	 * Get the console command options.
99
	 *
100
	 * @return array
101
	 */
102
	protected function getOptions()
103
	{
104
		return array(
105
			array( 'option', null, InputOption::VALUE_REQUIRED, 'Setup configuration, name and value are separated by ":" like "setup/default/demo:1"', array() ),
106
			array( 'action', null, InputOption::VALUE_REQUIRED, 'Action name that should be executed, i.e. "migrate", "rollback", "clean"', 'migrate' ),
107
			array( 'task', null, InputOption::VALUE_REQUIRED, 'Name of the setup task that should be executed', null ),
108
		);
109
	}
110
111
112
	/**
113
	 * Returns the database configuration from the config object.
114
	 *
115
	 * @param \Aimeos\MW\Config\Iface $conf Config object
116
	 * @return array Multi-dimensional associative list of database configuration parameters
117
	 */
118
	protected function getDbConfig( \Aimeos\MW\Config\Iface $conf )
119
	{
120
		$dbconfig = $conf->get( 'resource', array() );
121
122
		foreach( $dbconfig as $rname => $dbconf )
123
		{
124
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
125
				unset( $dbconfig[$rname] );
126
			}
127
		}
128
129
		return $dbconfig;
130
	}
131
132
133
	/**
134
	 * Extracts the configuration options from the input object and updates the configuration values in the config object.
135
	 *
136
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
137
	 * @param array Associative list of database configurations
138
	 * @throws \RuntimeException If the format of the options is invalid
139
	 */
140
	protected function setOptions( \Aimeos\MW\Config\Iface $conf )
141
	{
142
		foreach( (array) $this->option( 'option' ) as $option )
143
		{
144
			list( $name, $value ) = explode( ':', $option );
145
			$conf->set( str_replace( '\\', '/', $name ), $value );
146
		}
147
	}
148
}
149