Documentation

ClassScanner
in package

The ClassScanner is a helper for finding/loading classes based on their tagged interfaces.

The implementation of scanning+caching are generally built on these assumptions:

  • Scanning the filesystem is expensive. One scan should serve many consumers.
  • Consumers want to know about specific interfaces (get(['interface' => 'CRM_Foo_BarInterface']).

We reconcile these goals by performing a single scan and then storing an index. (Indexes are stored per-interface. So $cache->get(md5('CRM_Foo_BarInterface')) is a list of matching classes.)

Table of Contents

Constants

CIVI_CLASS_FILE_REGEX  = '/^([A-Z][A-Za-z0-9]*)\.php$/'
We load PHP files to find classes. Which files should we load?
CIVI_INTERFACE_REGEX  = ';^(CRM_|Civi\\\\);'
We cache information about classes that support each interface. Which interfaces should we track?
TTL  = 3 * 24 * 60 * 60

Properties

$caches  : array<string|int, mixed>

Methods

get()  : array<string|int, string>
Get a list of classes which match the $criteria.
buildIndex()  : array<string|int, string>
Fill the 'index' cache with information about all available interfaces.
filterLiveClasses()  : array<string|int, mixed>
getRelevantInterfaces()  : array<string|int, mixed>
Does `$class` have any interfaces that we care about?
scanClasses()  : array<string|int, mixed>
Build a list of Civi-related classes (including core and extensions).
scanCoreClasses()  : array<string|int, mixed>
Build a list of Civi-related classes (core-only).

Constants

CIVI_CLASS_FILE_REGEX

We load PHP files to find classes. Which files should we load?

public mixed CIVI_CLASS_FILE_REGEX = '/^([A-Z][A-Za-z0-9]*)\.php$/'

CIVI_INTERFACE_REGEX

We cache information about classes that support each interface. Which interfaces should we track?

public mixed CIVI_INTERFACE_REGEX = ';^(CRM_|Civi\\\\);'

Properties

Methods

get()

Get a list of classes which match the $criteria.

public static get(array<string|int, mixed> $criteria) : array<string|int, string>
Parameters
$criteria : array<string|int, mixed>

Ex: ['interface' => 'Civi\Core\HookInterface']

Return values
array<string|int, string>

List of matching classes.

buildIndex()

Fill the 'index' cache with information about all available interfaces.

private static buildIndex(CRM_Utils_Cache_Interface $cache) : array<string|int, string>

Every extant interface will be stored as a separate cache-item.

Example: assert $cache->get(md5(HookInterface::class)) == ['CRM_Foo_Bar', 'Civi\Whiz\Bang'] assert $cache->get(md5(UserJob::class)) == ['CRM_Foo_Job1', 'Civi\Whiz\Job2']

Parameters
$cache : CRM_Utils_Cache_Interface
Return values
array<string|int, string>

List of PHP interfaces that were detected. Ex: ['\Civi\Core\HookInterface', '\Civi\UserJob\UserJobInterface']

filterLiveClasses()

private static filterLiveClasses(array<string|int, mixed> $classes, array<string|int, mixed> $criteria) : array<string|int, mixed>
Parameters
$classes : array<string|int, mixed>
$criteria : array<string|int, mixed>
Return values
array<string|int, mixed>

getRelevantInterfaces()

Does `$class` have any interfaces that we care about?

private static getRelevantInterfaces(string $class) : array<string|int, mixed>
Parameters
$class : string

Concrete class that we are examining. Ex: 'Civi\Myextension\Foo'

Tags
throws
ReflectionException
Return values
array<string|int, mixed>

List of implemented interfaces that we care about. Ex: ['Civi\Core\HookInterface', 'Civi\Core\Service\AutoServiceInterface']

scanClasses()

Build a list of Civi-related classes (including core and extensions).

private static scanClasses() : array<string|int, mixed>
Return values
array<string|int, mixed>

Ex: ['CRM_Foo_Bar', 'Civi\Whiz\Bang']

scanCoreClasses()

Build a list of Civi-related classes (core-only).

private static scanCoreClasses() : array<string|int, mixed>
Return values
array<string|int, mixed>

Ex: ['CRM_Foo_Bar', 'Civi\Whiz\Bang']


        
On this page

Search results