Completed
Push — master ( 2ced0e...c16d2b )
by Aimeos
03:27
created

Bootstrap::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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 )
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];
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $settings, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
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' )
75
	{
76
		$container = $this->app->getContainer();
77
78
		$container['router'] = function( $c ) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
			return new \Aimeos\Slim\Router();
80
		};
81
82
		$container['aimeos'] = function( $c ) use ( $extdir ) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
			return new \Aimeos\Bootstrap( (array) $extdir, false );
84
		};
85
86
		$container['aimeos_context'] = function( $c ) {
87
			return new \Aimeos\Slim\Base\Context( $c );
88
		};
89
90
		$container['aimeos_i18n'] = function( $c ) {
91
			return new \Aimeos\Slim\Base\I18n( $c );
92
		};
93
94
		$container['aimeos_page'] = function( $c ) {
95
			return new \Aimeos\Slim\Base\Page( $c );
96
		};
97
98
		$container['aimeos_view'] = function( $c ) {
99
			return new \Aimeos\Slim\Base\View( $c );
100
		};
101
102
103
		$default = require __DIR__ . DIRECTORY_SEPARATOR . 'aimeos-default.php';
104
		$settings = array_replace_recursive( $default, $this->settings );
105
106
		$container['aimeos_config'] = function( $c ) use ( $settings ) {
107
108
			$config = new \Aimeos\MW\Config\PHPArray( $settings, $c['aimeos']->getConfigPaths() );
109
110 View Code Duplication
			if( function_exists( 'apc_store' ) === true && $config->get( 'apc_enabled', false ) == true ) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
				$config = new \Aimeos\MW\Config\Decorator\APC( $config, $config->get( 'apc_prefix', 'slim:' ) );
112
			}
113
114
			return $config;
115
		};
116
117
118
		$container['mailer'] = function( $c ) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
			return \Swift_Mailer::newInstance( \Swift_SendmailTransport::newInstance() );
0 ignored issues
show
Bug introduced by
The method newInstance() does not seem to exist on object<Swift_SendmailTransport>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method newInstance() does not seem to exist on object<Swift_Mailer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
		};
121
122
		return $this;
123
	}
124
125
126
	/**
127
	 * Returns the version of the Aimeos package
128
	 *
129
	 * @return string Version string
130
	 */
131
	public static function getVersion()
132
	{
133
		$basedir = dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) . DIRECTORY_SEPARATOR;
134
135
		if( ( $content = @file_get_contents( $basedir . 'composer.lock' ) ) !== false
136
			&& ( $content = json_decode( $content, true ) ) !== null && isset( $content['packages'] )
137
		) {
138
			foreach( (array) $content['packages'] as $item )
139
			{
140
				if( $item['name'] === 'aimeos/aimeos-slim' ) {
141
					return $item['version'];
142
				}
143
			}
144
		}
145
146
		return '';
147
	}
148
}