|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * phpBB Browser Push Notifications. An extension for the phpBB Forum Software package. |
| 5 | + * |
| 6 | + * @copyright (c) 2024, phpBB Limited <https://www.phpbb.com> |
| 7 | + * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +namespace phpbb\webpushnotifications\migrations; |
| 12 | + |
| 13 | +use phpbb\db\migration\container_aware_migration; |
| 14 | +use phpbb\filesystem\exception\filesystem_exception; |
| 15 | +use phpbb\filesystem\filesystem; |
| 16 | + |
| 17 | +class move_site_icons extends container_aware_migration |
| 18 | +{ |
| 19 | + private const NEW_ICON_DIR = 'images/site_icons'; |
| 20 | + private const OLD_ICON_DIR = 'images/icons'; |
| 21 | + |
| 22 | + /* @var filesystem $filesystem */ |
| 23 | + private $filesystem; |
| 24 | + |
| 25 | + public function effectively_installed() |
| 26 | + { |
| 27 | + return $this->get_filesystem()->exists($this->container->getParameter('core.root_path') . self::NEW_ICON_DIR); |
| 28 | + } |
| 29 | + |
| 30 | + public static function depends_on() |
| 31 | + { |
| 32 | + return ['\phpbb\webpushnotifications\migrations\add_acp_pwa_configs']; |
| 33 | + } |
| 34 | + |
| 35 | + public function update_data(): array |
| 36 | + { |
| 37 | + return [ |
| 38 | + ['custom', [[$this, 'configure_site_icons']]], |
| 39 | + ]; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Create a site_icons directory in the images directory (and copy existing PWA the icons there) |
| 44 | + * |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + public function configure_site_icons() |
| 48 | + { |
| 49 | + $filesystem = $this->get_filesystem(); |
| 50 | + $root_path = $this->container->getParameter('core.root_path'); |
| 51 | + |
| 52 | + $user = $this->container->get('user'); |
| 53 | + |
| 54 | + $small_icon = $this->config->offsetGet('pwa_icon_small'); |
| 55 | + $large_icon = $this->config->offsetGet('pwa_icon_large'); |
| 56 | + |
| 57 | + try |
| 58 | + { |
| 59 | + // Create the new site_icons directory |
| 60 | + if (!$filesystem->exists($root_path . self::NEW_ICON_DIR)) |
| 61 | + { |
| 62 | + $filesystem->mkdir($root_path . self::NEW_ICON_DIR); |
| 63 | + } |
| 64 | + |
| 65 | + // Copy existing PWA icons to the new site_icons directory |
| 66 | + foreach ([$small_icon, $large_icon] as $icon) |
| 67 | + { |
| 68 | + if (!$filesystem->exists($root_path . self::OLD_ICON_DIR . '/' . $icon)) |
| 69 | + { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + $filesystem->copy( |
| 74 | + $root_path . self::OLD_ICON_DIR . '/' . $icon, |
| 75 | + $root_path . self::NEW_ICON_DIR . '/' . $icon |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + $this->container->get('log')->add('admin', $user->data['user_id'], $user->ip, 'LOG_WEBPUSH_ICON_DIR_SUCCESS', false, [self::NEW_ICON_DIR]); |
| 80 | + } |
| 81 | + catch (filesystem_exception $e) |
| 82 | + { |
| 83 | + $this->container->get('log')->add('critical', $user->data['user_id'], $user->ip, 'LOG_WEBPUSH_ICON_DIR_FAIL', false, [$e->get_filename()]); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get the filesystem text object |
| 89 | + * |
| 90 | + * @return filesystem |
| 91 | + */ |
| 92 | + protected function get_filesystem() |
| 93 | + { |
| 94 | + if ($this->filesystem === null) |
| 95 | + { |
| 96 | + $this->filesystem = $this->container->get('filesystem'); |
| 97 | + } |
| 98 | + |
| 99 | + return $this->filesystem; |
| 100 | + } |
| 101 | +} |
0 commit comments