Completed
Push — master ( 21f2d4...ef12f9 )
by Aimeos
02:43
created

SetupCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 7
Bugs 1 Features 1
Metric Value
wmc 14
c 7
b 1
f 1
lcom 1
cbo 4
dl 0
loc 145
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B autoload() 0 19 5
B fire() 0 30 2
A getArguments() 0 7 1
A getOptions() 0 6 1
A getDbConfig() 0 13 3
A setOptions() 0 8 2
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
		$manager->run( 'mysql' );
100
	}
101
102
103
	/**
104
	 * Get the console command arguments.
105
	 *
106
	 * @return array
107
	 */
108
	protected function getArguments()
109
	{
110
		return array(
111
			array( 'site', InputArgument::OPTIONAL, 'Site for updating database entries', 'default' ),
112
			array( 'tplsite', InputArgument::OPTIONAL, 'Site used as template for creating the new one', 'default' ),
113
		);
114
	}
115
116
117
	/**
118
	 * Get the console command options.
119
	 *
120
	 * @return array
121
	 */
122
	protected function getOptions()
123
	{
124
		return array(
125
			array( 'option', null, InputOption::VALUE_REQUIRED, 'Optional setup configuration, name and value are separated by ":" like "setup/default/demo:1"', array() ),
126
		);
127
	}
128
129
130
	/**
131
	 * Returns the database configuration from the config object.
132
	 *
133
	 * @param \Aimeos\MW\Config\Iface $conf Config object
134
	 * @return array Multi-dimensional associative list of database configuration parameters
135
	 */
136
	protected function getDbConfig( \Aimeos\MW\Config\Iface $conf )
137
	{
138
		$dbconfig = $conf->get( 'resource', array() );
139
140
		foreach( $dbconfig as $rname => $dbconf )
141
		{
142
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
143
				unset( $dbconfig[$rname] );
144
			}
145
		}
146
147
		return $dbconfig;
148
	}
149
150
151
	/**
152
	 * Extracts the configuration options from the input object and updates the configuration values in the config object.
153
	 *
154
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
155
	 * @param array Associative list of database configurations
156
	 * @throws \RuntimeException If the format of the options is invalid
157
	 */
158
	protected function setOptions( \Aimeos\MW\Config\Iface $conf )
159
	{
160
		foreach( (array) $this->option( 'option' ) as $option )
161
		{
162
			list( $name, $value ) = explode( ':', $option );
163
			$conf->set( $name, $value );
164
		}
165
	}
166
}
167