Completed
Push — master ( da2dea...5ebe79 )
by Aimeos
22:25
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 1 Features 0
Metric Value
c 1
b 1
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
6
 */
7
8
9
namespace Aimeos\Shop\Command;
10
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Input\InputArgument;
13
14
15
class SetupCommand extends AbstractCommand
16
{
17
	/**
18
	 * The console command name.
19
	 *
20
	 * @var string
21
	 */
22
	protected $name = 'aimeos:setup';
23
24
	/**
25
	 * The console command description.
26
	 *
27
	 * @var string
28
	 */
29
	protected $description = 'Initialize or update the Aimeos database tables';
30
31
32
	/**
33
	 * Execute the console command.
34
	 *
35
	 * @return mixed
36
	 */
37
	public function fire()
38
	{
39
		$ctx = $this->getLaravel()->make( '\Aimeos\Shop\Base\Context' )->get( false );
40
		$ctx->setEditor( 'aimeos:setup' );
41
42
		$config = $ctx->getConfig();
43
		$site = $this->argument( 'site' );
44
		$template = $this->argument( 'tplsite' );
45
46
		$config->set( 'setup/site', $site );
47
		$dbconfig = $this->getDbConfig( $config );
48
		$this->setOptions( $config );
49
50
		$taskPaths = $this->getLaravel()->make( '\Aimeos\Shop\Base\Aimeos' )->get()->getSetupPaths( $template );
51
52
		$includePaths = $taskPaths;
53
		$includePaths[] = get_include_path();
54
55
		if( set_include_path( implode( PATH_SEPARATOR, $includePaths ) ) === false ) {
56
			throw new Exception( 'Unable to extend include path' );
57
		}
58
59
		$manager = new \Aimeos\MW\Setup\Manager\Multiple( $ctx->getDatabaseManager(), $dbconfig, $taskPaths, $ctx );
60
61
		$this->info( sprintf( 'Initializing or updating the Aimeos database tables for site "%1$s"', $site ) );
62
63
		$manager->run( 'mysql' );
64
	}
65
66
67
	/**
68
	 * Get the console command arguments.
69
	 *
70
	 * @return array
71
	 */
72
	protected function getArguments()
73
	{
74
		return array(
75
			array( 'site', InputArgument::OPTIONAL, 'Site for updating database entries', 'default' ),
76
			array( 'tplsite', InputArgument::OPTIONAL, 'Site used as template for creating the new one', 'default' ),
77
		);
78
	}
79
80
81
	/**
82
	 * Get the console command options.
83
	 *
84
	 * @return array
85
	 */
86
	protected function getOptions()
87
	{
88
		return array(
89
			array( 'option', null, InputOption::VALUE_REQUIRED, 'Optional setup configuration, name and value are separated by ":" like "setup/default/demo:1"', array() ),
90
		);
91
	}
92
93
94
	/**
95
	 * Returns the database configuration from the config object.
96
	 *
97
	 * @param \Aimeos\MW\Config\Iface $conf Config object
98
	 * @return array Multi-dimensional associative list of database configuration parameters
99
	 */
100
	protected function getDbConfig( \Aimeos\MW\Config\Iface $conf )
101
	{
102
		$dbconfig = $conf->get( 'resource', array() );
103
104
		foreach( $dbconfig as $rname => $dbconf )
105
		{
106
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
107
				unset( $dbconfig[$rname] );
108
			}
109
		}
110
111
		return $dbconfig;
112
	}
113
114
115
	/**
116
	 * Extracts the configuration options from the input object and updates the configuration values in the config object.
117
	 *
118
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
119
	 * @param array Associative list of database configurations
120
	 * @throws \RuntimeException If the format of the options is invalid
121
	 */
122
	protected function setOptions( \Aimeos\MW\Config\Iface $conf )
123
	{
124
		foreach( (array) $this->option( 'option' ) as $option )
125
		{
126
			list( $name, $value ) = explode( ':', $option );
127
			$conf->set( $name, $value );
128
		}
129
	}
130
}
131