Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
54.17% |
13 / 24 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
| base | |
54.17% |
13 / 24 |
|
33.33% |
2 / 6 |
16.80 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| is_enableable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| enable_step | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| disable_step | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| purge_step | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| get_migration_file_list | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
2.01 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\extension; |
| 15 | |
| 16 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 17 | |
| 18 | /** |
| 19 | * A base class for extensions without custom enable/disable/purge code. |
| 20 | */ |
| 21 | class base implements \phpbb\extension\extension_interface |
| 22 | { |
| 23 | /** @var ContainerInterface */ |
| 24 | protected $container; |
| 25 | |
| 26 | /** @var \phpbb\finder\finder */ |
| 27 | protected $extension_finder; |
| 28 | |
| 29 | /** @var \phpbb\db\migrator */ |
| 30 | protected $migrator; |
| 31 | |
| 32 | /** @var string */ |
| 33 | protected $extension_name; |
| 34 | |
| 35 | /** @var string */ |
| 36 | protected $extension_path; |
| 37 | |
| 38 | /** @var string[]|false */ |
| 39 | private $migrations = false; |
| 40 | |
| 41 | /** |
| 42 | * Constructor |
| 43 | * |
| 44 | * @param ContainerInterface $container Container object |
| 45 | * @param \phpbb\finder\finder $extension_finder |
| 46 | * @param \phpbb\db\migrator $migrator |
| 47 | * @param string $extension_name Name of this extension (from ext.manager) |
| 48 | * @param string $extension_path Relative path to this extension |
| 49 | */ |
| 50 | public function __construct(ContainerInterface $container, \phpbb\finder\finder $extension_finder, \phpbb\db\migrator $migrator, $extension_name, $extension_path) |
| 51 | { |
| 52 | $this->container = $container; |
| 53 | $this->extension_finder = $extension_finder; |
| 54 | $this->migrator = $migrator; |
| 55 | |
| 56 | $this->extension_name = $extension_name; |
| 57 | $this->extension_path = $extension_path; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * {@inheritdoc} |
| 62 | */ |
| 63 | public function is_enableable() |
| 64 | { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Single enable step that installs any included migrations |
| 70 | * |
| 71 | * @param mixed $old_state State returned by previous call of this method |
| 72 | * @return bool True if further steps are necessary, otherwise false |
| 73 | */ |
| 74 | public function enable_step($old_state) |
| 75 | { |
| 76 | $this->get_migration_file_list(); |
| 77 | |
| 78 | $this->migrator->update(); |
| 79 | |
| 80 | return !$this->migrator->finished(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Single disable step that does nothing |
| 85 | * |
| 86 | * @param mixed $old_state State returned by previous call of this method |
| 87 | * @return false Indicates no further steps are required |
| 88 | */ |
| 89 | public function disable_step($old_state) |
| 90 | { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Single purge step that reverts any included and installed migrations |
| 96 | * |
| 97 | * @param mixed $old_state State returned by previous call of this method |
| 98 | * @return bool True if further steps are necessary, otherwise false |
| 99 | */ |
| 100 | public function purge_step($old_state) |
| 101 | { |
| 102 | $migrations = $this->get_migration_file_list(); |
| 103 | |
| 104 | foreach ($migrations as $migration) |
| 105 | { |
| 106 | while ($this->migrator->migration_state($migration) !== false) |
| 107 | { |
| 108 | $this->migrator->revert($migration); |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get the list of migration files from this extension |
| 119 | * |
| 120 | * @return array |
| 121 | */ |
| 122 | protected function get_migration_file_list() |
| 123 | { |
| 124 | if ($this->migrations !== false) |
| 125 | { |
| 126 | return $this->migrations; |
| 127 | } |
| 128 | |
| 129 | // Only have the finder search in this extension path directory |
| 130 | $migrations = $this->extension_finder |
| 131 | ->extension_directory('/migrations') |
| 132 | ->find_from_extension($this->extension_name, $this->extension_path); |
| 133 | |
| 134 | $migrations = $this->extension_finder->get_classes_from_files($migrations); |
| 135 | |
| 136 | $this->migrator->set_migrations($migrations); |
| 137 | |
| 138 | return $this->migrator->get_migrations(); |
| 139 | } |
| 140 | } |