Skip to content

Commit

Permalink
[docs] Document use of legacyclasses.php
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jul 29, 2024
1 parent b54758a commit 33391fd
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/apis/_files/classes-dir.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import DefaultDescription from './classes-dir.mdx';

export default (initialProps: Props): ComponentFileSummary => (
<ComponentFileSummary
refreshedDuringPurge
filepath="/classes/"
summary="Autoloaded classes"
defaultDescription={DefaultDescription}
Expand Down
8 changes: 8 additions & 0 deletions docs/apis/_files/db-legacyclasses-php.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- markdownlint-disable first-line-heading -->
Details of legacy classes that have been moved to the classes directory to support autoloading but are not yet named properly.

:::note

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.

:::
51 changes: 51 additions & 0 deletions docs/apis/_files/db-legacyclasses-php.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) Moodle Pty Ltd.
*
* Moodle is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Moodle is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moodle. If not, see <http://www.gnu.org/licenses/>.
*/
import React from 'react';
import { ComponentFileSummary } from '../../_utils';
import type { Props } from '../../_utils';
import DefaultDescription from './db-legacyclasses-php.mdx';

const defaultExample = `
defined('MOODLE_INTERNAL') || die;
$legacyclasses = [
'old_class_name' => 'path/within/classes/directory.php',
// Examples:
\\coding_exception::class => 'exception/coding_exception.php',
\\moodle_exception::class => 'exception/moodle_exception.php',
// Example loading a class from a different subsystem.
// This should typically only be used in core.
\\cache::class => [
'core_cache', // The name of the subsystem to load from.
'cache.php', // The file name within that filesystem's classes directory.
],
];
`;

export default (initialProps: Props): ComponentFileSummary => (
<ComponentFileSummary
refreshedDuringPurge
defaultExample={defaultExample}
defaultDescription={DefaultDescription}
filepath="/db/legacyclasses.php"
summary="Legacy classes"
examplePurpose="Legacy classes"
{...initialProps}
/>
);
2 changes: 2 additions & 0 deletions docs/apis/_files/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import DbInstallPHP from './db-install-php';
import DbInstallXML from './install-xml';
import DbMessagesPHP from './db-messages-php';
import DbMobilePHP from './db-mobile-php';
import DbLegacyclassesPHP from './db-legacyclasses-php';
import DbRenamedclassesPHP from './db-renamedclasses-php';
import DbServicesPHP from './db-services-php';
import DbTasksPHP from './db-tasks-php';
Expand Down Expand Up @@ -57,6 +58,7 @@ export {
DbInstallXML,
DbMessagesPHP,
DbMobilePHP,
DbLegacyclassesPHP,
DbRenamedclassesPHP,
DbServicesPHP,
DbTasksPHP,
Expand Down
5 changes: 5 additions & 0 deletions docs/apis/commonfiles/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
DbInstallPHP,
DbInstallXML,
DbMessagesPHP,
DbLegacyclassesPHP,
DbRenamedclassesPHP,
DbServicesPHP,
DbTasksPHP,
Expand Down Expand Up @@ -99,6 +100,10 @@ import extraLangDescription from '../_files/lang-extra.md';

<DbRenamedclassesPHP />

### db/legacyclasses.php

<DbLegacyclassesPHP />

### classes/

<ClassesDir />
Expand Down
21 changes: 21 additions & 0 deletions docs/devupdate.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ Please note that the same limitations regarding access to the Database, Session,

:::

#### Autoloading legacy classes

<Since version="4.5" issueNumber="MDL-81919" />

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.

```php title="Example entry in lib/db/legacyclasses.php"
$legacyclasses = [
// The legacy \moodle_exception class can be loaded from lib/classes/exception/moodle_exception.php.
\moodle_exception::class => 'exception/moodle_exception.php',

// The legacy \cache class can be loaded from cache/classes/cache.php.
\cache::class => [
'core_cache',
'cache.php',
],
];
```

See MDL-81919 for further information on the rationale behind this change.

### SMS API

A new SMS API was introduced. See the [SMS API documentation](./apis/subsystems/sms/index.md) for more information.
Expand Down
1 change: 1 addition & 0 deletions project-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ langindex
lastaccess
lastname
lastruntime
legacyclasses
locallib
loglevel
mainmenu
Expand Down

0 comments on commit 33391fd

Please sign in to comment.