Skip to content

Commit e1d07bc

Browse files
authored
Merge pull request #1612 from mihailges/MDL-88576_docs
MDL-88576 [docs] Add documentation for Composer runtime status checks…
2 parents 6992856 + 330ee41 commit e1d07bc

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

‎general/development/tools/composer.md‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,75 @@ As far as there are a number of variables affecting how that lock file will be g
4747
1. Check that the `composer.lock` file, together with other changes, does include the changes you've performed in the `composer.json` file.
4848
1. Ideally, run both phpunit and behat tests and verify that there isn't any problem, using all the supported PHP versions.
4949
1. Done, you can send the changes for review, integration and, if everything goes ok, will be applied upstream without problem.
50+
51+
## Runtime status checks {/* #runtime-status-checks */}
52+
53+
<Since version="5.3" issueNumber="MDL-88576" />
54+
55+
Moodle provides a Composer runtime status API for inspecting the state of Composer-managed dependencies.
56+
57+
The API can be used to:
58+
59+
- Check whether Composer dependencies are installed.
60+
- Verify that installed package versions match those recorded in `composer.lock`.
61+
- Identify missing packages.
62+
- Identify outdated packages.
63+
64+
### Obtaining the service {/* #obtaining-the-service */}
65+
66+
The Composer service must be obtained through Moodle's dependency injection container.
67+
68+
```php
69+
$composer = \core\di::get(\core\composer::class);
70+
```
71+
72+
### Checking Composer installation status {/* #checking-composer-installation-status */}
73+
74+
To determine whether Composer dependencies are installed:
75+
76+
```php
77+
if ($composer->is_installed()) {
78+
echo 'Composer dependencies are installed.';
79+
}
80+
```
81+
82+
### Retrieving overall status {/* #retrieving-overall-status */}
83+
84+
The `get_status()` method returns a `\core\composer\status` object containing the overall Composer runtime status and the status of all packages defined in `composer.lock`.
85+
86+
```php
87+
$status = $composer->get_status();
88+
89+
if (!$status->installed) {
90+
echo 'Composer dependencies are not installed.';
91+
} else if (!$status->current) {
92+
echo 'One or more Composer packages require attention.';
93+
}
94+
```
95+
96+
The status object also provides convenience methods for identifying packages in a particular state:
97+
98+
```php
99+
$current = $status->current_packages();
100+
$missing = $status->missing_packages();
101+
$outdated = $status->outdated_packages();
102+
```
103+
104+
### Checking a specific package {/* #checking-a-specific-package */}
105+
106+
The `get_package_status()` method returns a `\core\composer\package_status` object containing information about the package's installation state and version information.
107+
108+
```php
109+
$package = $composer->get_package_status('composer/installers');
110+
111+
if (!$package->installed) {
112+
echo 'Package is not installed.';
113+
}
114+
115+
if (!$package->current) {
116+
echo 'Package is not up to date.';
117+
}
118+
119+
echo "Required version: {$package->requiredversion}";
120+
echo "Installed version: {$package->installedversion}";
121+
```

0 commit comments

Comments
 (0)