Skip to content

Commit ea737db

Browse files
authored
Merge pull request #1024 from andrewnicols/MDL-81919
[docs] Document use of legacyclasses.php
2 parents b54758a + 33391fd commit ea737db

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

docs/apis/_files/classes-dir.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import DefaultDescription from './classes-dir.mdx';
2121

2222
export default (initialProps: Props): ComponentFileSummary => (
2323
<ComponentFileSummary
24+
refreshedDuringPurge
2425
filepath="/classes/"
2526
summary="Autoloaded classes"
2627
defaultDescription={DefaultDescription}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- markdownlint-disable first-line-heading -->
2+
Details of legacy classes that have been moved to the classes directory to support autoloading but are not yet named properly.
3+
4+
:::note
5+
6+
Adding classes to `db/legacyclasses.php` is only necessary when the class is part of a _public_ API, or the class name cannot be changed.
7+
8+
:::
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright (c) Moodle Pty Ltd.
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+
import React from 'react';
18+
import { ComponentFileSummary } from '../../_utils';
19+
import type { Props } from '../../_utils';
20+
import DefaultDescription from './db-legacyclasses-php.mdx';
21+
22+
const defaultExample = `
23+
defined('MOODLE_INTERNAL') || die;
24+
25+
$legacyclasses = [
26+
'old_class_name' => 'path/within/classes/directory.php',
27+
28+
// Examples:
29+
\\coding_exception::class => 'exception/coding_exception.php',
30+
\\moodle_exception::class => 'exception/moodle_exception.php',
31+
32+
// Example loading a class from a different subsystem.
33+
// This should typically only be used in core.
34+
\\cache::class => [
35+
'core_cache', // The name of the subsystem to load from.
36+
'cache.php', // The file name within that filesystem's classes directory.
37+
],
38+
];
39+
`;
40+
41+
export default (initialProps: Props): ComponentFileSummary => (
42+
<ComponentFileSummary
43+
refreshedDuringPurge
44+
defaultExample={defaultExample}
45+
defaultDescription={DefaultDescription}
46+
filepath="/db/legacyclasses.php"
47+
summary="Legacy classes"
48+
examplePurpose="Legacy classes"
49+
{...initialProps}
50+
/>
51+
);

docs/apis/_files/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import DbInstallPHP from './db-install-php';
2626
import DbInstallXML from './install-xml';
2727
import DbMessagesPHP from './db-messages-php';
2828
import DbMobilePHP from './db-mobile-php';
29+
import DbLegacyclassesPHP from './db-legacyclasses-php';
2930
import DbRenamedclassesPHP from './db-renamedclasses-php';
3031
import DbServicesPHP from './db-services-php';
3132
import DbTasksPHP from './db-tasks-php';
@@ -57,6 +58,7 @@ export {
5758
DbInstallXML,
5859
DbMessagesPHP,
5960
DbMobilePHP,
61+
DbLegacyclassesPHP,
6062
DbRenamedclassesPHP,
6163
DbServicesPHP,
6264
DbTasksPHP,

docs/apis/commonfiles/index.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
DbInstallPHP,
1818
DbInstallXML,
1919
DbMessagesPHP,
20+
DbLegacyclassesPHP,
2021
DbRenamedclassesPHP,
2122
DbServicesPHP,
2223
DbTasksPHP,
@@ -99,6 +100,10 @@ import extraLangDescription from '../_files/lang-extra.md';
99100

100101
<DbRenamedclassesPHP />
101102

103+
### db/legacyclasses.php
104+
105+
<DbLegacyclassesPHP />
106+
102107
### classes/
103108

104109
<ClassesDir />

docs/devupdate.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ Please note that the same limitations regarding access to the Database, Session,
4747

4848
:::
4949

50+
#### Autoloading legacy classes
51+
52+
<Since version="4.5" issueNumber="MDL-81919" />
53+
54+
The Moodle class autoloader is now able to load legacy classes defined in the relevant `db/legacyclasses.php`. Files should only contain a single class.
55+
56+
```php title="Example entry in lib/db/legacyclasses.php"
57+
$legacyclasses = [
58+
// The legacy \moodle_exception class can be loaded from lib/classes/exception/moodle_exception.php.
59+
\moodle_exception::class => 'exception/moodle_exception.php',
60+
61+
// The legacy \cache class can be loaded from cache/classes/cache.php.
62+
\cache::class => [
63+
'core_cache',
64+
'cache.php',
65+
],
66+
];
67+
```
68+
69+
See MDL-81919 for further information on the rationale behind this change.
70+
5071
### SMS API
5172

5273
A new SMS API was introduced. See the [SMS API documentation](./apis/subsystems/sms/index.md) for more information.

project-words.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ langindex
195195
lastaccess
196196
lastname
197197
lastruntime
198+
legacyclasses
198199
locallib
199200
loglevel
200201
mainmenu

0 commit comments

Comments
 (0)