Passed
Push — release-3.2.0 ( e3b9c5...a662cc )
by Daniel
02:49
created

ext.php (2 issues)

1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker;
11
12
class ext extends \phpbb\extension\base
13
{
14
	/**
15
	 * Check whether or not the extension can be enabled.
16
	 *
17
	 * @return bool
18
	 */
19
	public function is_enableable()
20
	{
21
		$user = $this->container->get('user');
22
		$user->add_lang_ext('blitze/sitemaker', 'ext');
23
24
		$lang = $user->lang;
25
		$metadata = $this->get_metadata('blitze/sitemaker');
26
27
		$req_phpbb_version = $metadata['extra']['soft-require']['phpbb/phpbb'];
28
		$phpbb_version = $this->container->get('config')['version'];
29
		$is_enableable = $this->version_is_ok($phpbb_version, $req_phpbb_version);
30
31
		if (!$is_enableable)
32
		{
33
			$lang['EXTENSION_NOT_ENABLEABLE'] .= '<br>' . $user->lang('PHPBB_VERSION_UNMET', $req_phpbb_version);
34
		}
35
36
		$user->lang = $lang;
37
		return $is_enableable;
38
	}
39
40
	/**
41
	 * @param string $current_version
42
	 * @param string $required_version
43
	 * @return bool
44
	 */
45
	protected function version_is_ok($current_version, &$required_version)
46
	{
47
		$required_version = html_entity_decode($required_version);
48
		list($min_req_version, $max_req_version) = explode(',', $required_version);
49
50
		$contraint = $this->get_version_constraint($min_req_version);
51
		if (!phpbb_version_compare($current_version, $contraint['version'], $contraint['operator']))
52
		{
53
			return false;
54
		}
55
56
		$contraint = $this->get_version_constraint($max_req_version);
57
		if ($constraint['version'] && !phpbb_version_compare($current_version, $contraint['version'], $contraint['operator']))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $constraint does not exist. Did you maybe mean $contraint?
Loading history...
58
		{
59
			return false;
60
		}
61
62
		return true;
63
	}
64
65
	/**
66
	 * @param string $version
67
	 * @return array
68
	 */
69
	protected function get_version_constraint($version)
70
	{
71
		if (preg_match('/^(\D+)(.+)/i', trim($version), $matches))
72
		{
73
			list(, $operator, $version) = $matches;
74
		}
75
76
		return [
77
			'version' => str_replace('@', '', $version),
78
			'operator' => $operator ?: '>=',
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $operator does not seem to be defined for all execution paths leading up to this point.
Loading history...
79
		];
80
	}
81
82
	/**
83
	 * Get composer metadata information
84
	 *
85
	 * @param string $name
86
	 * @param string $element
87
	 * @return array
88
	 */
89
	protected function get_metadata($name, $element = 'all')
90
	{
91
		$ext_manager = $this->container->get('ext.manager');
92
		$metadata_manager = $ext_manager->create_extension_metadata_manager($name);
93
		return $metadata_manager->get_metadata($element);
94
	}
95
}
96