ComposerJson

Class
extends JsonFile
Final: Yes

Represents a specialized reader for a Composer JSON file.

Description

This class SHALL provide convenient accessors for commonly used composer.json metadata after reading and caching the file contents. Consumers SHOULD use this class when they need normalized access to package-level metadata. The internal data cache MUST reflect the contents returned by the underlying JSON file reader at construction time.

Table of Contents

Properties

$data

Stores the decoded Composer JSON document contents.

 : array<string, mixed>

Methods

__construct()

Initializes the Composer JSON reader.

 : mixed
getAuthors()

Returns the package authors declared in the Composer file.

 : array<string|int, mixed>
getAutoload()

Returns the autoload configuration for the requested autoload type.

 : array<string|int, mixed>
getExtra()

Returns the extra configuration section declared in the Composer file.

 : array<string|int, mixed>
getPackageDescription()

Returns the package description declared in the Composer file.

 : string
getPackageLicense()

Returns the package license when it can be resolved to a single value.

 : string|null
getPackageName()

Returns the package name declared in the Composer file.

 : string
Properties

$data

Private

Stores the decoded Composer JSON document contents.

private array<string, mixed> $data

Description

This property MUST contain the data read from the target Composer file during construction. Consumers SHOULD treat the structure as internal implementation detail and SHALL rely on accessor methods instead of direct access.

Methods

__construct()

Public

Initializes the Composer JSON reader.

public __construct([ string|null  $path = null]) : mixed

Description

When no path is provided, the default Composer file location returned by Composer's factory SHALL be used. The constructor MUST immediately read and cache the JSON document contents so that subsequent accessor methods can operate on the in-memory data.

Parameters
$path : string|null = null

Description

The absolute or relative path to a Composer JSON file. When omitted, the default Composer file path SHALL be used.

getAuthors()

Public

Returns the package authors declared in the Composer file.

public getAuthors() : array<string|int, mixed>

Description

This method SHALL return the value of the authors key when present. If the key is absent, the method MUST return an empty array.

Return values
array<string|int, mixed>

Description

the authors list as declared in the Composer file, or an empty array when undefined

getAutoload()

Public

Returns the autoload configuration for the requested autoload type.

public getAutoload([ string  $type = 'psr-4']) : array<string|int, mixed>

Description

This method SHALL inspect the autoload section and return the nested configuration for the requested type, such as psr-4. When the autoload section or the requested type is not defined, the method MUST return an empty array.

Parameters
$type : string = 'psr-4'

Description

The autoload mapping type to retrieve. This defaults to psr-4.

Return values
array<string|int, mixed>

Description

the autoload configuration for the requested type, or an empty array when unavailable

getExtra()

Public

Returns the extra configuration section declared in the Composer file.

public getExtra() : array<string|int, mixed>

Description

This method SHALL return the value of the extra key when present. If the key is absent, the method MUST return an empty array.

Return values
array<string|int, mixed>

Description

the extra configuration data, or an empty array when undefined

getPackageDescription()

Public

Returns the package description declared in the Composer file.

public getPackageDescription() : string

Description

This method SHALL return the value of the description key when present. If the description is not defined, the method MUST return an empty string.

Return values
string

Description

the package description, or an empty string when undefined

getPackageLicense()

Public

Returns the package license when it can be resolved to a single value.

public getPackageLicense() : string|null

Description

This method SHALL return the license value directly when it is a string. When the license is an array containing exactly one item, that single item SHALL be returned. When the license field is not present, is empty, or cannot be resolved to exactly one string value, the method MUST return null.

Return values
string|null

Description

the resolved license identifier, or null when no single license value can be determined

getPackageName()

Public

Returns the package name declared in the Composer file.

public getPackageName() : string

Description

This method SHALL return the value of the name key when present. If the package name is not defined, the method MUST return an empty string.

Return values
string

Description

the package name, or an empty string when undefined