2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Abstraction of general file archives.
21 * @copyright 2020 Mark Nelson <mdjnelson@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 use core_files\local\archive_writer\file_writer_interface
as file_writer_interface
;
28 use core_files\local\archive_writer\stream_writer_interface
as stream_writer_interface
;
31 * Each file archive type must extend this class.
34 * @copyright 2020 Mark Nelson <mdjnelson@gmail.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 abstract class archive_writer
{
40 * The zip writer class.
42 public const ZIP_WRITER
= 'zip_writer';
45 * Returns the stream writer.
47 * @param string $filename
49 * @return stream_writer_interface
51 public static function get_stream_writer(string $filename, string $type): stream_writer_interface
{
52 $classname = self
::get_classname_for_type($type);
54 if (!is_a($classname, stream_writer_interface
::class, true)) {
55 throw new \
InvalidArgumentException("{$type} does not support streaming");
58 return $classname::stream_instance($filename);
62 * Returns the file writer.
64 * @param string $filepath
66 * @return file_writer_interface
68 public static function get_file_writer(string $filepath, string $type): file_writer_interface
{
69 $classname = self
::get_classname_for_type($type);
71 if (!is_a($classname, file_writer_interface
::class, true)) {
72 throw new \
InvalidArgumentException("{$type} does not support writing to files");
75 return $classname::file_instance($filepath);
79 * Sanitise the file path, removing any unsuitable characters.
81 * @param string $filepath
84 public function sanitise_filepath(string $filepath): string {
85 return clean_param($filepath, PARAM_PATH
);
89 * Returns the class name for the type that was provided in get_file_writer().
94 protected static function get_classname_for_type(string $type): string {
95 return "core_files\local\archive_writer\\" . $type;
99 * The archive_writer Constructor.
101 protected function __construct() {
106 * Adds a file from a file path.
108 * @param string $name The path of file in archive (including directory).
109 * @param string $path The path to file on disk (note: paths should be encoded using
110 * UNIX-style forward slashes -- e.g '/path/to/some/file').
112 abstract public function add_file_from_filepath(string $name, string $path): void
;
115 * Adds a file from a string.
117 * @param string $name The path of file in archive (including directory).
118 * @param string $data The contents of file
120 abstract public function add_file_from_string(string $name, string $data): void
;
123 * Adds a file from a stream.
125 * @param string $name The path of file in archive (including directory).
126 * @param resource $stream The contents of file as a stream resource
128 abstract public function add_file_from_stream(string $name, $stream): void
;
131 * Adds a stored_file to archive.
133 * @param string $name The path of file in archive (including directory).
134 * @param \stored_file $file
136 abstract public function add_file_from_stored_file(string $name, \stored_file
$file): void
;
139 * Finish writing the zip footer.
141 abstract public function finish(): void
;