|
| 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 setup_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 | + // Cache frequently used services and values |
| 50 | + $filesystem = $this->get_filesystem(); |
| 51 | + $root_path = $this->container->getParameter('core.root_path'); |
| 52 | + $user = $this->container->get('user'); |
| 53 | + $log = $this->container->get('log'); |
| 54 | + |
| 55 | + // Prepare paths once |
| 56 | + $new_icon_path = $root_path . self::NEW_ICON_DIR; |
| 57 | + $old_icon_path = $root_path . self::OLD_ICON_DIR; |
| 58 | + |
| 59 | + // Batch get config values |
| 60 | + $icons = [ |
| 61 | + 'small' => $this->config->offsetGet('pwa_icon_small'), |
| 62 | + 'large' => $this->config->offsetGet('pwa_icon_large') |
| 63 | + ]; |
| 64 | + |
| 65 | + try |
| 66 | + { |
| 67 | + // Create directory only if needed (give an empty index.htm too) |
| 68 | + if (!$filesystem->exists($new_icon_path)) |
| 69 | + { |
| 70 | + $filesystem->mkdir($new_icon_path, 0755); |
| 71 | + $filesystem->touch($new_icon_path . '/index.htm'); |
| 72 | + } |
| 73 | + |
| 74 | + // Process icons |
| 75 | + $copied = false; |
| 76 | + foreach ($icons as $icon) |
| 77 | + { |
| 78 | + $old_file = $old_icon_path . '/' . $icon; |
| 79 | + $new_file = $new_icon_path . '/' . $icon; |
| 80 | + |
| 81 | + if (!empty($icon) && $filesystem->exists($old_file)) |
| 82 | + { |
| 83 | + $filesystem->copy($old_file, $new_file); |
| 84 | + $copied = true; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Set up a log message result |
| 89 | + $result = [ |
| 90 | + 'lang_key' => $copied ? 'LOG_WEBPUSH_ICON_COPY_SUCCESS' : 'LOG_WEBPUSH_ICON_DIR_SUCCESS', |
| 91 | + 'params' => [self::NEW_ICON_DIR], |
| 92 | + ]; |
| 93 | + } |
| 94 | + catch (filesystem_exception $e) |
| 95 | + { |
| 96 | + $result = [ |
| 97 | + 'lang_key' => 'LOG_WEBPUSH_ICON_DIR_FAIL', |
| 98 | + 'params' => [$e->get_filename(), $e->getMessage()] |
| 99 | + ]; |
| 100 | + } |
| 101 | + |
| 102 | + // Log result |
| 103 | + $log->add('admin', $user->data['user_id'], $user->ip, $result['lang_key'], false, $result['params']); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Get the filesystem object |
| 108 | + * |
| 109 | + * @return filesystem |
| 110 | + */ |
| 111 | + protected function get_filesystem() |
| 112 | + { |
| 113 | + if ($this->filesystem === null) |
| 114 | + { |
| 115 | + $this->filesystem = $this->container->get('filesystem'); |
| 116 | + } |
| 117 | + |
| 118 | + return $this->filesystem; |
| 119 | + } |
| 120 | +} |
0 commit comments