Completed
Push — master ( 336742...ba7147 )
by Aimeos
03:24
created

SetupCommand::fire()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 1
Metric Value
c 7
b 1
f 1
dl 0
loc 47
rs 6.7272
cc 7
eloc 31
nc 9
nop 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 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
	 * Loads the requested setup task class
41
	 *
42
	 * @param string $classname Name of the setup task class
43
	 * @return boolean True if class is found, false if not
44
	 */
45
	public static function autoload( $classname )
46
	{
47
		if( strncmp( $classname, 'Aimeos\\MW\\Setup\\Task\\', 21 ) === 0 )
48
		{
49
			$fileName = substr( $classname, 21 ) . '.php';
50
			$paths = explode( PATH_SEPARATOR, get_include_path() );
51
52
			foreach( $paths as $path )
53
			{
54
				$file = $path . DIRECTORY_SEPARATOR . $fileName;
55
56
				if( file_exists( $file ) === true && ( include_once $file ) !== false ) {
57
					return true;
58
				}
59
			}
60
		}
61
62
		return false;
63
	}
64
65
66
	/**
67
	 * Execute the console command.
68
	 *
69
	 * @return mixed
70
	 */
71
	public function fire()
72
	{
73
		$ctx = $this->getLaravel()->make( '\Aimeos\Shop\Base\Context' )->get( false );
74
		$ctx->setEditor( 'aimeos:setup' );
75
76
		$config = $ctx->getConfig();
77
		$site = $this->argument( 'site' );
78
		$template = $this->argument( 'tplsite' );
79
80
		$config->set( 'setup/site', $site );
81
		$dbconfig = $this->getDbConfig( $config );
82
		$this->setOptions( $config );
83
84
		$taskPaths = $this->getLaravel()->make( '\Aimeos\Shop\Base\Aimeos' )->get()->getSetupPaths( $template );
85
86
		$includePaths = $taskPaths;
87
		$includePaths[] = get_include_path();
88
89
		if( set_include_path( implode( PATH_SEPARATOR, $includePaths ) ) === false ) {
90
			throw new \Exception( 'Unable to extend include path' );
91
		}
92
93
		spl_autoload_register( '\Aimeos\Shop\Command\SetupCommand::autoload', true );
94
95
		$manager = new \Aimeos\MW\Setup\Manager\Multiple( $ctx->getDatabaseManager(), $dbconfig, $taskPaths, $ctx );
96
97
		$this->info( sprintf( 'Initializing or updating the Aimeos database tables for site "%1$s"', $site ) );
98
99
		if( ( $task = $this->option( 'task' ) ) && is_array( $task ) ) {
100
			$task = reset( $task );
101
		}
102
103
		switch( $this->option( 'action' ) )
104
		{
105
			case 'migrate':
106
				$manager->migrate( $task );
107
				break;
108
			case 'rollback':
109
				$manager->rollback( $task );
110
				break;
111
			case 'clean':
112
				$manager->clean( $task );
113
				break;
114
			default:
115
				throw new \Exception( sprintf( 'Invalid setup action "%1$s"', $this->option( 'action' ) ) );
116
		}
117
	}
118
119
120
	/**
121
	 * Get the console command arguments.
122
	 *
123
	 * @return array
124
	 */
125
	protected function getArguments()
126
	{
127
		return array(
128
			array( 'site', InputArgument::OPTIONAL, 'Site for updating database entries', 'default' ),
129
			array( 'tplsite', InputArgument::OPTIONAL, 'Site used as template for creating the new one', 'default' ),
130
		);
131
	}
132
133
134
	/**
135
	 * Get the console command options.
136
	 *
137
	 * @return array
138
	 */
139
	protected function getOptions()
140
	{
141
		return array(
142
			array( 'option', null, InputOption::VALUE_REQUIRED, 'Setup configuration, name and value are separated by ":" like "setup/default/demo:1"', array() ),
143
			array( 'action', null, InputOption::VALUE_REQUIRED, 'Action name that should be executed, i.e. "migrate", "rollback", "clean"', 'migrate' ),
144
			array( 'task', null, InputOption::VALUE_REQUIRED, 'Name of the setup task that should be executed', null ),
145
		);
146
	}
147
148
149
	/**
150
	 * Returns the database configuration from the config object.
151
	 *
152
	 * @param \Aimeos\MW\Config\Iface $conf Config object
153
	 * @return array Multi-dimensional associative list of database configuration parameters
154
	 */
155
	protected function getDbConfig( \Aimeos\MW\Config\Iface $conf )
156
	{
157
		$dbconfig = $conf->get( 'resource', array() );
158
159
		foreach( $dbconfig as $rname => $dbconf )
160
		{
161
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
162
				unset( $dbconfig[$rname] );
163
			}
164
		}
165
166
		return $dbconfig;
167
	}
168
169
170
	/**
171
	 * Extracts the configuration options from the input object and updates the configuration values in the config object.
172
	 *
173
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
174
	 * @param array Associative list of database configurations
175
	 * @throws \RuntimeException If the format of the options is invalid
176
	 */
177
	protected function setOptions( \Aimeos\MW\Config\Iface $conf )
178
	{
179
		foreach( (array) $this->option( 'option' ) as $option )
180
		{
181
			list( $name, $value ) = explode( ':', $option );
182
			$conf->set( $name, $value );
183
		}
184
	}
185
}
186