diff --git a/phpBB/phpbb/storage/adapter/adapter_interface.php b/phpBB/phpbb/storage/adapter/adapter_interface.php index c92a429ec8..207f001fad 100644 --- a/phpBB/phpbb/storage/adapter/adapter_interface.php +++ b/phpBB/phpbb/storage/adapter/adapter_interface.php @@ -84,13 +84,4 @@ interface adapter_interface * When the file cannot be copied */ public function copy($path_orig, $path_dest); - - /** - * Creates a directory recursively. - * - * @param string $path The directory path - * - * @throws \phpbb\storage\exception\exception On any directory creation failure - */ - public function create_dir($path); } diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index 0d05676a78..319308527d 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -16,6 +16,7 @@ namespace phpbb\storage\adapter; use phpbb\storage\exception\exception; use phpbb\filesystem\exception\filesystem_exception; use phpbb\filesystem\filesystem; +use phpbb\filesystem\helper as filesystem_helper; /** * @internal Experimental @@ -66,6 +67,8 @@ class local implements adapter_interface */ public function put_contents($path, $content) { + $this->ensure_directory_exists($path); + if ($this->exists($path)) { throw new exception('STORAGE_FILE_EXISTS', $path); @@ -129,6 +132,8 @@ class local implements adapter_interface */ public function rename($path_orig, $path_dest) { + $this->ensure_directory_exists($path_dest); + try { $this->filesystem->rename($this->root_path . $path_orig, $this->root_path . $path_dest, false); @@ -144,6 +149,8 @@ class local implements adapter_interface */ public function copy($path_orig, $path_dest) { + $this->ensure_directory_exists($path_dest); + try { $this->filesystem->copy($this->root_path . $path_orig, $this->root_path . $path_dest, false); @@ -155,9 +162,13 @@ class local implements adapter_interface } /** - * {@inheritdoc} + * Creates a directory recursively. + * + * @param string $path The directory path + * + * @throws \phpbb\storage\exception\exception On any directory creation failure */ - public function create_dir($path) + protected function create_dir($path) { try { @@ -169,4 +180,19 @@ class local implements adapter_interface } } + /** + * Ensures that the directory of a file exists. + * + * @param string $path The file path + */ + protected function ensure_directory_exists($path) + { + $path = dirname($this->root_path . $path); + $path = filesystem_helper::make_path_relative($directory, $this->root_path); + + if (!$this->exists($path)) + { + $this->create_dir($path); + } + } } diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 7468a0d782..65a4f6eb72 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -140,16 +140,4 @@ class storage { $this->get_adapter()->copy($path_orig, $path_dest); } - - /** - * Creates a directory recursively. - * - * @param string $path The directory path - * - * @throws \phpbb\storage\exception\exception On any directory creation failure - */ - public function create_dir($path) - { - $this->get_adapter()->create_dir($path); - } }