\Charcoal\Model\ServiceMetadataLoader

Load metadata from JSON file(s).

The Metadata Loader is different than the FileLoader class it extends mainly because it tries to find all files matching the "ident" in all search path and merge them together in an array, to be filled in a Metadata object.

If ident is an actual class name, then it will also try to load all the JSON matching the class' parents and interfaces.

Summary

Methods
Properties
Constants
__construct()
load()
loadData()
loadDataArray()
No public properties found
No constants found
loadJsonFile()
No protected properties found
N/A
setCachePool()
cachePool()
setBasePath()
basePath()
setPaths()
paths()
addPaths()
addPath()
resolvePath()
validatePath()
hierarchy()
classLineage()
loadFileFromIdent()
loadFile()
filenameFromIdent()
identToClassname()
classnameToIdent()
$cachePool
$lineageCache
$snakeCache
$camelCache
$basePath
$paths
N/A

Properties

$cachePool

$cachePool : \Psr\Cache\CacheItemPoolInterface

The PSR-6 caching service.

Type

\Psr\Cache\CacheItemPoolInterface

$lineageCache

$lineageCache : array

The cache of class/interface lineages.

Type

array

$snakeCache

$snakeCache : array

The cache of snake-cased words.

Type

array

$camelCache

$camelCache : array

The cache of camel-cased words.

Type

array

$basePath

$basePath : string

The base path to prepend to any relative paths to search in.

Type

string

$paths

$paths : array

The paths to search in.

Type

array

Methods

__construct()

__construct(array  $data = null) : void

Return new MetadataLoader object.

The application's metadata paths, if any, are merged with the loader's search paths.

Required dependencie

  • logger
  • cache
  • paths
  • base_path

Parameters

array $data

The loader's dependencies.

load()

load(string  $ident, \Charcoal\Model\MetadataInterface  $metadata, array|null  $interfaces = null) : array

Load the metadata for the given identifier or interfaces.

Parameters

string $ident

The metadata identifier to load or to use as the cache key if $interfaces is provided.

\Charcoal\Model\MetadataInterface $metadata

The metadata object to load into.

array|null $interfaces

One or more metadata identifiers to load.

Throws

\InvalidArgumentException

If the identifier is not a string.

Returns

array —

Returns the cached metadata instance or if it's stale or empty, loads a fresh copy of the data into $metadata and returns it;

loadData()

loadData(string  $ident) : array

Fetch the metadata for the given identifier.

Parameters

string $ident

The metadata identifier to load.

Throws

\InvalidArgumentException

If the identifier is not a string.

Returns

array

loadDataArray()

loadDataArray(array  $idents) : array

Fetch the metadata for the given identifiers.

Parameters

array $idents

One or more metadata identifiers to load.

Returns

array

loadJsonFile()

loadJsonFile(mixed  $filename) : array|null

Load the contents of a JSON file.

Parameters

mixed $filename

The file path to retrieve.

Throws

\InvalidArgumentException

If a JSON decoding error occurs.

Returns

array|null

setCachePool()

setCachePool(\Psr\Cache\CacheItemPoolInterface  $cache) : \Charcoal\Model\Service\MetadataLoader

Set the cache service.

Parameters

\Psr\Cache\CacheItemPoolInterface $cache

A PSR-6 compliant cache pool instance.

Returns

\Charcoal\Model\Service\MetadataLoader

Chainable

cachePool()

cachePool() : \Psr\Cache\CacheItemPoolInterface

Retrieve the cache service.

Throws

\RuntimeException

If the cache service was not previously set.

Returns

\Psr\Cache\CacheItemPoolInterface

setBasePath()

setBasePath(string  $basePath) : \Charcoal\Model\Service\MetadataLoader

Assign a base path for relative search paths.

Parameters

string $basePath

The base path to use.

Throws

\InvalidArgumentException

If the base path parameter is not a string.

Returns

\Charcoal\Model\Service\MetadataLoader

Chainable

basePath()

basePath() : string

Retrieve the base path for relative search paths.

Returns

string

setPaths()

setPaths(array<mixed,string>  $paths) : self

Assign a list of paths.

Parameters

array<mixed,string> $paths

The list of paths to add.

Returns

self

paths()

paths() : array<mixed,string>

Retrieve the searchable paths.

Returns

array<mixed,string>

addPaths()

addPaths(array<mixed,string>  $paths) : self

Append a list of paths.

Parameters

array<mixed,string> $paths

The list of paths to add.

Returns

self

addPath()

addPath(string  $path) : self

Append a path.

Parameters

string $path

A file or directory path.

Throws

\InvalidArgumentException

If the path does not exist or is invalid.

Returns

self

resolvePath()

resolvePath(string  $path) : string

Parse a relative path using the base path if needed.

Parameters

string $path

The path to resolve.

Throws

\InvalidArgumentException

If the path is invalid.

Returns

string

validatePath()

validatePath(string  $path) : string

Validate a resolved path.

Parameters

string $path

The path to validate.

Returns

string

hierarchy()

hierarchy(string  $ident) : array

Build a class/interface lineage from the given snake-cased namespace.

Parameters

string $ident

The FQCN (in snake-case) to load the hierarchy from.

Returns

array

classLineage()

classLineage(string  $classname, string|null  $ident = null) : array

Build a class/interface lineage from the given PHP namespace.

Parameters

string $classname

The FQCN to load the hierarchy from.

string|null $ident

Optional. The snake-cased $classname.

Returns

array

loadFileFromIdent()

loadFileFromIdent(string  $ident) : array|null

Load a metadata file from the given metdata identifier.

The file is converted to JSON, the only supported format.

Parameters

string $ident

The metadata identifier to fetch.

Returns

array|null

loadFile()

loadFile(string  $filename) : array|null

Load a metadata file.

Supported file types: JSON.

Parameters

string $filename

A supported metadata file.

Returns

array|null

filenameFromIdent()

filenameFromIdent(string  $ident) : string

Convert a snake-cased namespace to a file path.

Parameters

string $ident

The identifier to convert.

Returns

string

identToClassname()

identToClassname(string  $ident) : string

Convert a snake-cased namespace to CamelCase.

Parameters

string $ident

The namespace to convert.

Returns

string —

Returns a valid PHP namespace.

classnameToIdent()

classnameToIdent(string  $classname) : string

Convert a CamelCase namespace to snake-case.

Parameters

string $classname

The namespace to convert.

Returns

string —

Returns a snake-cased namespace.