Skip to content

Commit 7befd9a

Browse files
jimwinstheodorejb
andauthored
Add information about Composer and example of using its autoloader (#3677)
Co-authored-by: Theodore Brown <[email protected]>
1 parent 0c2a0d7 commit 7befd9a

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

install/composer.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- $Revision$ -->
3+
4+
<chapter xml:id="install.composer" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
5+
<title>Installation of Composer and third-party packages</title>
6+
7+
<sect1 xml:id="install.composer.intro">
8+
<title>Introduction to Composer</title>
9+
<simpara>
10+
&link.composer; is a dependency manager for PHP that makes it possible
11+
to define third-party code packages used by a project that can
12+
then be easily installed and updated. It leverages the built-in
13+
<link linkend="language.oop5.autoload">class autoloading features</link>
14+
of PHP, repositories of PHP packages such as
15+
<link xlink:href="&url.packagist;">Packagist</link>, and common project
16+
layout and coding conventions.
17+
</simpara>
18+
<simpara>
19+
For example, if a PHP application or website needs
20+
to work with <abbrev>UUID</abbrev> values,
21+
<link xlink:href="&url.packagist.package;ramsey/uuid">Ben Ramsey's
22+
<literal>ramsey/uuid</literal> package</link> that implements the
23+
widely known and used types of UUIDs that are defined by
24+
<link xlink:href="&url.rfc;4122">RFC 4122</link> could be used.
25+
</simpara>
26+
<simpara>
27+
Briefly, this is done by creating a <literal>composer.json</literal>
28+
in the project, using Composer to install the latest version of the
29+
package, and including Composer's autoload script to make it available
30+
to the code. The <link xlink:href="&url.composer;/doc/01-basic-usage.md">Composer
31+
"Basic Usage" documentation</link> goes into this in more depth.
32+
</simpara>
33+
<example>
34+
<title>
35+
<literal>composer.json</literal> that requires a single package
36+
</title>
37+
<programlisting role="javascript">
38+
<![CDATA[
39+
{
40+
"require": {
41+
"ramsey/uuid": "^4.7"
42+
}
43+
}
44+
]]>
45+
</programlisting>
46+
</example>
47+
48+
</sect1>
49+
</chapter>

language/oop5/autoload.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ string(5) "ITest"
7979
Fatal error: Interface 'ITest' not found in ...
8080
*/
8181
?>
82+
]]>
83+
</programlisting>
84+
</example>
85+
<example>
86+
<title>Using Composer's autoloader</title>
87+
<simpara>
88+
&link.composer; generates a <literal>vendor/autoload.php</literal>
89+
which is set up to automatically load packages that are being managed
90+
by Composer. By including this file, those packages can be used without
91+
any additional work.
92+
</simpara>
93+
<programlisting role="php">
94+
<![CDATA[
95+
<?php
96+
require __DIR__ . '/vendor/autoload.php';
97+
98+
$uuid = new Ramsey\Uuid\Uuid::uuid7();
99+
100+
echo "Generated new UUID -> ", $uuid->toString(), "\n";
101+
?>
82102
]]>
83103
</programlisting>
84104
</example>

0 commit comments

Comments
 (0)