aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/extension/base.php
blob: a7531350f7cf61c1f3eecbe9b7155f6075544ba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\extension;

use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* A base class for extensions without custom enable/disable/purge code.
*/
class base implements \phpbb\extension\extension_interface
{
	/** @var ContainerInterface */
	protected $container;

	/** @var \phpbb\finder */
	protected $extension_finder;

	/** @var \phpbb\db\migrator */
	protected $migrator;

	/** @var string */
	protected $extension_name;

	/** @var string */
	protected $extension_path;

	/** @var string[] */
	private $migrations = false;

	/**
	* Constructor
	*
	* @param ContainerInterface $container Container object
	* @param \phpbb\finder $extension_finder
	* @param \phpbb\db\migrator $migrator
	* @param string $extension_name Name of this extension (from ext.manager)
	* @param string $extension_path Relative path to this extension
	*/
	public function __construct(ContainerInterface $container, \phpbb\finder $extension_finder, \phpbb\db\migrator $migrator, $extension_name, $extension_path)
	{
		$this->container = $container;
		$this->extension_finder = $extension_finder;
		$this->migrator = $migrator;

		$this->extension_name = $extension_name;
		$this->extension_path = $extension_path;
	}

	/**
	* {@inheritdoc}
	*/
	public function is_enableable()
	{
		return true;
	}

	/**
	* Single enable step that installs any included migrations
	*
	* @param mixed $old_state State returned by previous call of this method
	* @return false Indicates no further steps are required
	*/
	public function enable_step($old_state)
	{
		$migrations = $this->get_migration_file_list();

		$this->migrator->set_migrations($migrations);

		$this->migrator->update();

		return !$this->migrator->finished();
	}

	/**
	* Single disable step that does nothing
	*
	* @param mixed $old_state State returned by previous call of this method
	* @return false Indicates no further steps are required
	*/
	public function disable_step($old_state)
	{
		return false;
	}

	/**
	* Single purge step that reverts any included and installed migrations
	*
	* @param mixed $old_state State returned by previous call of this method
	* @return false Indicates no further steps are required
	*/
	public function purge_step($old_state)
	{
		$migrations = $this->get_migration_file_list();

		$this->migrator->set_migrations($migrations);

		foreach ($migrations as $migration)
		{
			while ($this->migrator->migration_state($migration) !== false)
			{
				$this->migrator->revert($migration);

				return true;
			}
		}

		return false;
	}

	/**
	* Get the list of migration files from this extension
	*
	* @return array
	*/
	protected function get_migration_file_list()
	{
		if ($this->migrations !== false)
		{
			return $this->migrations;
		}

		// Only have the finder search in this extension path directory
		$migrations = $this->extension_finder
			->extension_directory('/migrations')
			->find_from_extension($this->extension_name, $this->extension_path);

		$migrations = $this->extension_finder->get_classes_from_files($migrations);

		// Unset classes that are not a valid migration
		foreach ($migrations as $key => $migration)
		{
			if (\phpbb\db\migrator::is_migration($migration) === true)
			{
				continue;
			}

			unset($migrations[$key]);
		}

		return $migrations;
	}
}