Completed
Push — master ( 89068a...af6c70 )
by Aimeos
02:40
created

Bootstrap::setup()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 45
Code Lines 22

Duplication

Lines 3
Ratio 6.67 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 3
loc 45
rs 8.8571
cc 3
eloc 22
nc 1
nop 1
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
		$default = require __DIR__ . DIRECTORY_SEPARATOR . 'aimeos-default.php';
77
		$settings = array_replace_recursive( $default, $this->settings );
78
		$container = $this->app->getContainer();
79
80
		$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...
81
			return new \Aimeos\Bootstrap( (array) $extdir, false );
82
		};
83
84
		$container['aimeos_context'] = function( $c ) {
85
			return new \Aimeos\Slim\Base\Context( $c );
86
		};
87
88
		$container['aimeos_i18n'] = function( $c ) {
89
			return new \Aimeos\Slim\Base\I18n( $c );
90
		};
91
92
		$container['aimeos_page'] = function( $c ) {
93
			return new \Aimeos\Slim\Base\Page( $c );
94
		};
95
96
		$container['aimeos_view'] = function( $c ) {
97
			return new \Aimeos\Slim\Base\View( $c );
98
		};
99
100
101
		$container['aimeos_config'] = function( $c ) use ( $settings ) {
102
103
			$config = new \Aimeos\MW\Config\PHPArray( $settings, $c['aimeos']->getConfigPaths() );
104
105 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...
106
				$config = new \Aimeos\MW\Config\Decorator\APC( $config, $config->get( 'apc_prefix', 'slim:' ) );
107
			}
108
109
			return $config;
110
		};
111
112
113
		$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...
114
			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...
115
		};
116
117
		return $this;
118
	}
119
120
121
	/**
122
	 * Returns the version of the Aimeos package
123
	 *
124
	 * @return string Version string
125
	 */
126
	public static function getVersion()
127
	{
128
		$basedir = dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) . DIRECTORY_SEPARATOR;
129
130
		if( ( $content = @file_get_contents( $basedir . 'composer.lock' ) ) !== false
131
			&& ( $content = json_decode( $content, true ) ) !== null && isset( $content['packages'] )
132
		) {
133
			foreach( (array) $content['packages'] as $item )
134
			{
135
				if( $item['name'] === 'aimeos/aimeos-slim' ) {
136
					return $item['version'];
137
				}
138
			}
139
		}
140
141
		return '';
142
	}
143
}