1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
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.
|
|
|
8 |
//
|
|
|
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.
|
|
|
13 |
//
|
|
|
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/>.
|
|
|
16 |
|
|
|
17 |
namespace core_files\hook;
|
|
|
18 |
|
|
|
19 |
use core\exception\coding_exception;
|
|
|
20 |
use core\attribute;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* A hook which is fired before a file is created in the file storage API.
|
|
|
24 |
*
|
|
|
25 |
* @package core_files
|
|
|
26 |
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
#[attribute\label('Allows subscribers to modify file content before it is stored in the file pool')]
|
|
|
30 |
#[attribute\tags('file')]
|
|
|
31 |
#[attribute\hook\replaces_callbacks('before_file_created')]
|
|
|
32 |
final class before_file_created {
|
|
|
33 |
use \core\hook\stoppable_trait;
|
|
|
34 |
|
|
|
35 |
/** @var bool Whether the content has been updated at all */
|
|
|
36 |
public bool $contentupdated = false;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Constructor.
|
|
|
40 |
*
|
|
|
41 |
* @param \stdClass|null $filerecord
|
|
|
42 |
* @param string|null $filepath The path to the file on disk
|
|
|
43 |
* @param string|null $filecontent The content of the file
|
|
|
44 |
*/
|
|
|
45 |
public function __construct(
|
|
|
46 |
/** @var \stdClass The file record */
|
|
|
47 |
protected ?\stdClass $filerecord = null,
|
|
|
48 |
/** @var string|null $filepath The source file on disk */
|
|
|
49 |
protected ?string $filepath = null,
|
|
|
50 |
/** @var string|null $filecontent The content of the file if it is not stored on disk */
|
|
|
51 |
protected ?string $filecontent = null,
|
|
|
52 |
) {
|
|
|
53 |
if ($filepath === null && $filecontent === null) {
|
|
|
54 |
throw new \InvalidArgumentException('Either $filepath or $filecontent must be set');
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
if ($filepath !== null && $filecontent !== null) {
|
|
|
58 |
throw new \InvalidArgumentException('Only one of $filepath or $filecontent can be set');
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Whether the file path was specified.
|
|
|
64 |
*
|
|
|
65 |
* @return bool
|
|
|
66 |
*/
|
|
|
67 |
public function has_filepath(): bool {
|
|
|
68 |
return $this->filepath !== null;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Whether the file content was specified.
|
|
|
73 |
*
|
|
|
74 |
* @return bool
|
|
|
75 |
*/
|
|
|
76 |
public function has_filecontent(): bool {
|
|
|
77 |
return $this->filecontent !== null;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Get the file path to the file that will be stored.
|
|
|
82 |
*
|
|
|
83 |
* @return string
|
|
|
84 |
*/
|
|
|
85 |
public function get_filepath(): ?string {
|
|
|
86 |
return $this->filepath;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Get the file content that will be stored.
|
|
|
91 |
*
|
|
|
92 |
* @return string
|
|
|
93 |
*/
|
|
|
94 |
public function get_filecontent(): ?string {
|
|
|
95 |
return $this->filecontent;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Get the file record.
|
|
|
100 |
*
|
|
|
101 |
* @return \stdClass|null
|
|
|
102 |
*/
|
|
|
103 |
public function get_filerecord(): ?\stdClass {
|
|
|
104 |
return $this->filerecord;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Update the file path to a new value.
|
|
|
109 |
*
|
|
|
110 |
* @param string $filepath
|
|
|
111 |
*/
|
|
|
112 |
public function update_filepath(string $filepath): void {
|
|
|
113 |
if ($this->filepath === null) {
|
|
|
114 |
throw new coding_exception('Cannot update file path when the file path is not set');
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
if ($filepath !== $this->filepath) {
|
|
|
118 |
$this->contentupdated = true;
|
|
|
119 |
$this->filepath = $filepath;
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Update the file content to a new value.
|
|
|
125 |
*
|
|
|
126 |
* @param string $filecontent
|
|
|
127 |
*/
|
|
|
128 |
public function update_filecontent(string $filecontent): void {
|
|
|
129 |
if ($this->filecontent === null) {
|
|
|
130 |
throw new coding_exception('Cannot update file content when the file content is not set');
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
if ($filecontent !== $this->filecontent) {
|
|
|
134 |
$this->contentupdated = true;
|
|
|
135 |
$this->filecontent = $filecontent;
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Whether the file path or file content has been changed.
|
|
|
141 |
*
|
|
|
142 |
* @return bool
|
|
|
143 |
*/
|
|
|
144 |
public function has_changed(): bool {
|
|
|
145 |
return $this->contentupdated;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Process legacy callbacks.
|
|
|
150 |
*/
|
|
|
151 |
public function process_legacy_callbacks(): void {
|
|
|
152 |
if ($pluginsfunction = get_plugins_with_function(function: 'before_file_created', migratedtohook: true)) {
|
|
|
153 |
foreach ($pluginsfunction as $plugintype => $plugins) {
|
|
|
154 |
foreach ($plugins as $pluginfunction) {
|
|
|
155 |
$pluginfunction($this->filerecord);
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
}
|