Bootstrap::setup()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
c 4
b 0
f 0
nc 1
nop 1
dl 0
loc 48
rs 9.552
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package Slim
7
 */
8
9
10
namespace Aimeos\Slim;
11
12
13
/**
14
 * Bootstrap class for Aimeos Slim integration
15
 *
16
 * @package Slim
17
 */
18
class Bootstrap
19
{
20
	private $app;
21
	private $settings;
22
23
24
	/**
25
	 * Initializes the object
26
	 *
27
	 * @param \Slim\App $app Slim application
28
	 * @param array $settings Multi-dimensional array of configuration settings
29
	 */
30
	public function __construct( \Slim\App $app, array $settings )
31
	{
32
		$this->app = $app;
33
		$this->settings = $settings;
34
	}
35
36
37
	/**
38
	 * Registers the Aimeos routes
39
	 *
40
	 * @param string $path Absolute or relative path to the Aimeos route file
41
	 * @return \Aimeos\Slim\Bootstrap Self instance
42
	 */
43
	public function routes( $path ) : self
44
	{
45
		$app = $this->app;
46
		$settings = $this->settings;
47
48
		$config = function( $key, $default ) use ( $settings )
49
		{
50
			foreach( explode( '/', trim( $key, '/' ) ) as $part )
51
			{
52
				if( isset( $settings[$part] ) ) {
53
					$settings = $settings[$part];
54
				} else {
55
					return $default;
56
				}
57
			}
58
59
			return $settings;
60
		};
61
62
		require $path;
63
64
		return $this;
65
	}
66
67
68
	/**
69
	 * Sets up the Aimeos environemnt
70
	 *
71
	 * @param string $extdir Absolute or relative path to the Aimeos extension directory
72
	 * @return \Aimeos\Slim\Bootstrap Self instance
73
	 */
74
	public function setup( $extdir = '../ext' ) : self
75
	{
76
		$container = $this->app->getContainer();
77
78
		$container['router'] = function( $c ) {
79
			return new \Aimeos\Slim\Router();
80
		};
81
82
		$container['mailer'] = function( $c ) {
83
			return new \Swift_Mailer( new \Swift_SendmailTransport() );
84
		};
85
86
87
		$default = require __DIR__ . DIRECTORY_SEPARATOR . 'aimeos-default.php';
88
		$settings = array_replace_recursive( $default, $this->settings );
89
90
		$container['aimeos'] = function( $c ) use ( $extdir ) {
91
			return new \Aimeos\Bootstrap( (array) $extdir, false );
92
		};
93
94
		$container['aimeos.config'] = function( $c ) use ( $settings ) {
95
			return new \Aimeos\Slim\Base\Config( $c, $settings );
96
		};
97
98
		$container['aimeos.context'] = function( $c ) {
99
			return new \Aimeos\Slim\Base\Context( $c );
100
		};
101
102
		$container['aimeos.i18n'] = function( $c ) {
103
			return new \Aimeos\Slim\Base\I18n( $c );
104
		};
105
106
		$container['aimeos.locale'] = function( $c ) {
107
			return new \Aimeos\Slim\Base\Locale( $c );
108
		};
109
110
		$container['aimeos.view'] = function( $c ) {
111
			return new \Aimeos\Slim\Base\View( $c );
112
		};
113
114
		$container['shop'] = function( $c ) {
115
			return new \Aimeos\Slim\Base\Shop( $c );
116
		};
117
118
		// add client IP address to requests
119
		$this->app->add( new \RKA\Middleware\IpAddress( true, [] ) );
120
121
		return $this;
122
	}
123
124
125
	/**
126
	 * Returns the version of the Aimeos package
127
	 *
128
	 * @return string Version string
129
	 */
130
	public static function getVersion() : string
131
	{
132
		$basedir = dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) . DIRECTORY_SEPARATOR;
133
134
		if( ( $content = @file_get_contents( $basedir . 'composer.lock' ) ) !== false
135
			&& ( $content = json_decode( $content, true ) ) !== null && isset( $content['packages'] )
136
		) {
137
			foreach( (array) $content['packages'] as $item )
138
			{
139
				if( $item['name'] === 'aimeos/aimeos-slim' ) {
140
					return $item['version'];
141
				}
142
			}
143
		}
144
145
		return '';
146
	}
147
}
148