Sledgehammer Framework

Repository extends Base
in package
Uses Singleton

Repository/DataMapper.

An API to retrieve and store models from their backends and track their changes. A model is a object oriented interface on top of the data the backend provides.

Table of Contents

$autoCompleteFolder  : string|false
Write php files for editor autocompletion.
$instances  : array<string|int, static>
The instances that are accessible by class::instance() [ 'id' => instance, // direct mapping to an instance 'id2' => 'id' // indirect mapping to an instance 'id3' => function () { return new Sington() } // lazy creation of an instance.
$backends  : array<string|int, RepositoryBackend>
Registered backends.
$configs  : array<string|int, mixed>|ModelConfig
Registered models.
$created  : array<string|int, mixed>
References to instances that are not yet added to the backend.
$deleted  : array<string|int, mixed>
References to instances that have been deleted.
$junctions  : array<string|int, mixed>|ModelConfig
Registered junctions (many-to-many).
$namespaces  : array<string|int, mixed>
Registered namespaces that are searched for a classname that matches the model->name.
$objects  : array<string|int, mixed>
References to instances.
$plurals  : array<string|int, mixed>
Mapping of plural notation to singular.
$autoComplete  : array<string|int, mixed>
The configurations of the previously generated AutoComplete Helper classes.
$collectionMappings  : array<string|int, mixed>
Used to speedup the execution RepostoryCollection->where() statements. (allows db WHERE statements).
$lazyConfigurations  : array<string|int, Closure>
Registered closures that configure the default repository.
$loading  : array<string|int, mixed>
Array containing instances that are loading saving/saved in 1 Repository->get() call.
$saving  : array<string|int, mixed>
Array containing instances that are saving/saved in 1 Repository->save() call.
$validated  : array<string|int, mixed>
Models which class is validated.
__call()  : mixed
Handle get$Model(), find$Model(), all$Models(), save$Model(), create$Model() and delete$Model() methods.
__callStatic()  : mixed
Report that the $method doesn't exist.
__get()  : mixed
Report that $property doesn't exist.
__set()  : mixed
Report that $property doesn't exist and set the property to the given $value.
__toString()  : string
The object is used as an string.
all()  : Collection
Retrieve all instances for the specified model.
configureDefault()  : mixed
Register closures that lazily configure the default repository.
convert()  : instance
Create a instance from existing $data.
create()  : object
Create an in-memory instance of the model, ready to be saved.
delete()  : mixed
Delete an instance.
diff()  : array<string|int, mixed>
Get the unsaved changes.
export()  : array<string|int, mixed>
Export an instance (or instance collection) as an array for use before serializing.
free()  : mixed
Free object refercences from memory.
get()  : object
Retrieve an instance from the Repository.
instance()  : static
Get the singleton instance.
instance()  :
isConfigured()  : bool
Check if a model is configured in this repository.
one()  : object|null
Retrieve an instance from the Repository based on criteria other than the id.
registerBackend()  : mixed
Register all models from the backend.
registerBehavior()  : mixed
registerNamespace()  : mixed
Search for model classnames in the given $namespace.
reload()  : mixed
Reload an instance from the connected backend.
resolveModel()  : string
Lookup a modelname for an instance bound to this repository.
resolveProperties()  : mixed
Retrieve all related instances (belongTo) and collection (hasMany) and update the $instance.
resolveProperty()  : mixed
Retrieve a related instance (belongsTo) or collection (hasMany) and update the $instance.
save()  : mixed
Store the instance.
writeAutoCompleteHelper()  : mixed
Generate php sourcecode of an subclass of Repository with all magic model functions written as normal functions.
_getBackend()  : RepositoryBackend
Lookup a RepositoryBackend for a backendname.
_getConfig()  : ModelConfig
Lookup the ModelConfig for a modelname.
convertToData()  : mixed
convertToInstance()  : stdClass
Convert raw backend data into an object instance.
defaultInstance()  : mixed
registerModel()  : mixed
Add an configution for a model.
resolveIndex()  : string
Return the ($this->objects) index.
resolveInstance()  : mixed
Return the instance the Placeholder points to.
_triggerEvent()  : mixed
buildAlias()  : string
Builds a uses array
ref()  : mixed

Properties

$autoCompleteFolder

Write php files for editor autocompletion.

public static string|false $autoCompleteFolder = false

must end with DIRECTORY_SEPARATOR

$instances

The instances that are accessible by class::instance() [ 'id' => instance, // direct mapping to an instance 'id2' => 'id' // indirect mapping to an instance 'id3' => function () { return new Sington() } // lazy creation of an instance.

public static array<string|int, static> $instances = []

].

$configs

Registered models.

protected array<string|int, mixed>|ModelConfig $configs = []

[model => config]

$created

References to instances that are not yet added to the backend.

protected array<string|int, mixed> $created = []

$deleted

References to instances that have been deleted.

protected array<string|int, mixed> $deleted = []

$junctions

Registered junctions (many-to-many).

protected array<string|int, mixed>|ModelConfig $junctions = []

$namespaces

Registered namespaces that are searched for a classname that matches the model->name.

protected array<string|int, mixed> $namespaces = []

$objects

References to instances.

protected array<string|int, mixed> $objects = []

$plurals

Mapping of plural notation to singular.

protected array<string|int, mixed> $plurals = []

[$plural => $singular]

$autoComplete

The configurations of the previously generated AutoComplete Helper classes.

private array<string|int, mixed> $autoComplete

Used to determine if a model has changed.

$collectionMappings

Used to speedup the execution RepostoryCollection->where() statements. (allows db WHERE statements).

private array<string|int, mixed> $collectionMappings = []

[$model => [$propertyPath =>]]

$lazyConfigurations

Registered closures that configure the default repository.

private static array<string|int, Closure> $lazyConfigurations = []

$loading

Array containing instances that are loading saving/saved in 1 Repository->get() call.

private array<string|int, mixed> $loading = []

Used for preventing infinite preloading.

$saving

Array containing instances that are saving/saved in 1 Repository->save() call.

private array<string|int, mixed> $saving = []

Used for preventing duplicate saves.

$validated

Models which class is validated.

private array<string|int, mixed> $validated = []

$model => (bool) $valid.

Methods

__call()

Handle get$Model(), find$Model(), all$Models(), save$Model(), create$Model() and delete$Model() methods.

public __call(string $method, array<string|int, mixed> $arguments) : mixed
Parameters
$method : string
$arguments : array<string|int, mixed>
Return values
mixed

__callStatic()

Report that the $method doesn't exist.

public static __callStatic(string $method, array<string|int, mixed> $arguments) : mixed
Parameters
$method : string
$arguments : array<string|int, mixed>
Return values
mixed

__get()

Report that $property doesn't exist.

public __get(string $property) : mixed
Parameters
$property : string
Return values
mixed

__set()

Report that $property doesn't exist and set the property to the given $value.

public __set(string $property, mixed $value) : mixed
Parameters
$property : string
$value : mixed
Return values
mixed

__toString()

The object is used as an string.

public __toString() : string
Return values
string

all()

Retrieve all instances for the specified model.

public all(string $model[, mixed $conditions = null ][, array<string|int, mixed> $options = [] ]) : Collection
Parameters
$model : string
$conditions : mixed = null
$options : array<string|int, mixed> = []
Return values
Collection

configureDefault()

Register closures that lazily configure the default repository.

public static configureDefault(Closure $closure) : mixed
Parameters
$closure : Closure

A closure that received the repository as the first argument

Return values
mixed

convert()

Create a instance from existing $data.

public convert(string $model, mixed $data[, array<string|int, mixed> $options = [] ]) : instance

This won't store the data. For storing data use $repository->save($instance).

Parameters
$model : string
$data : mixed
$options : array<string|int, mixed> = []

'preload' => int The preload recursion level. false or 0: Only the the relation. 1: Also the relations of the relation. 2: Also the relations of the relations of the relation. N: Etc. true or -1: Load all relations of all relations.

Return values
instance

create()

Create an in-memory instance of the model, ready to be saved.

public create(string $model[, array<string|int, mixed> $values = [] ]) : object
Parameters
$model : string
$values : array<string|int, mixed> = []

Initial contents of the object (optional)

Return values
object

delete()

Delete an instance.

public delete(string $model, instance|id $mixed) : mixed
Parameters
$model : string
$mixed : instance|id

The instance or id

Return values
mixed

diff()

Get the unsaved changes.

public diff(string $model, stdClass $instance) : array<string|int, mixed>
Parameters
$model : string
$instance : stdClass
Return values
array<string|int, mixed>

export()

Export an instance (or instance collection) as an array for use before serializing.

public export(string $model, mixed $instances, int $depth[, mixed $skip = [] ]) : array<string|int, mixed>

Should be used in jsonSerialize when the instance is a JsonSerializable (PHP 5.4)

Parameters
$model : string
$instances : mixed
$depth : int
$skip : mixed = []
Tags
link
http://php.net/manual/en/class.jsonserializable.php
Return values
array<string|int, mixed>

free()

Free object refercences from memory.

public free(string $model[, mixed $instance = null ]) : mixed
Parameters
$model : string
$instance : mixed = null
Return values
mixed

get()

Retrieve an instance from the Repository.

public get(string $model, mixed $id[, array<string|int, mixed> $options = [] ]) : object
Parameters
$model : string
$id : mixed

The instance ID

$options : array<string|int, mixed> = []

'preload' => int The preload recursion level. false or 0: Only the the relation. 1: Also the relations of the relation. 2: Also the relations of the relations of the relation. N: Etc. true or -1: Load all relations of all relations.

Return values
object

instance

instance()

Get the singleton instance.

public static instance([string $identifier = 'default' ]) : static
Parameters
$identifier : string = 'default'

The identifier (string),

Return values
static

instance()

public instance(string $id) :
Parameters
$id : string
Return values

isConfigured()

Check if a model is configured in this repository.

public isConfigured(string $model) : bool
Parameters
$model : string
Return values
bool

one()

Retrieve an instance from the Repository based on criteria other than the id.

public one(string $model, array<string|int, mixed> $conditions[, bool $allowNone = false ][, array<string|int, mixed> $options = [] ]) : object|null

If the id is known use the Repository->get($model, $id) instead.

When the critera matches 0 or more than 1 instances an exception is thrown, unless 'allow_none' is given as an option.

Parameters
$model : string
$conditions : array<string|int, mixed>
$allowNone : bool = false

When no match is found, return null instead of throwing an Exception.

$options : array<string|int, mixed> = []
Return values
object|null

instance

registerBackend()

Register all models from the backend.

public registerBackend(RepositoryBackend $backend) : mixed

Aslo validates and corrects the model configurations.

Parameters
$backend : RepositoryBackend
Return values
mixed

registerNamespace()

Search for model classnames in the given $namespace.

public registerNamespace(string $namespace) : mixed
Parameters
$namespace : string
Return values
mixed

reload()

Reload an instance from the connected backend.

public reload(string $model[, instance|id $mixed = null ][, array<string|int, mixed> $options = [] ]) : mixed

Discards any unsaved changes.

Parameters
$model : string
$mixed : instance|id = null

(optional) The instance or id

$options : array<string|int, mixed> = []

array( 'all' => (optional) bool reload all instances from this model (default: false) 'discard_changes' => (optional) Reload the instance, even when it has pending changes. )

Return values
mixed

resolveModel()

Lookup a modelname for an instance bound to this repository.

public resolveModel(stdClass $instance) : string
Parameters
$instance : stdClass
Tags
throws
Exceptions

on failure

Return values
string

model

resolveProperties()

Retrieve all related instances (belongTo) and collection (hasMany) and update the $instance.

public resolveProperties(object $instance[, array<string|int, mixed> $options = [] ]) : mixed
Parameters
$instance : object

The instance with the relations.

$options : array<string|int, mixed> = []

'preload' => int The recursion level. 0: Only the the relation. 1: Also the relations of the relation. 2: Also the relations of the relations of the relation. N: etc. -1: Load all relations of all relations. 'model' => string // The model Improve speed and reliability of the lookup

Return values
mixed

resolveProperty()

Retrieve a related instance (belongsTo) or collection (hasMany) and update the $instance.

public resolveProperty(object $instance, string $property[, array<string|int, mixed> $options = [] ]) : mixed
Parameters
$instance : object

The instance with the relation.

$property : string

The relation property.

$options : array<string|int, mixed> = []

'preload' => int The preload recursion level. false or 0: Only the the relation. 1: Also the relations of the relation. 2: Also the relations of the relations of the relation. N: Etc. true or -1: Load all relations of all relations. 'reload' => bool // Override the current value with the connection defined in the backend. 'model' => string // The model Improve speed and reliability of the lookup

Return values
mixed

related instance or null

save()

Store the instance.

public save(string $model, stdClass $instance[, array<string|int, mixed> $options = [] ]) : mixed
Parameters
$model : string
$instance : stdClass
$options : array<string|int, mixed> = []

'ignore_relations' => bool true: Only save the instance, false: Save all connected instances, 'add_unknown_instance' => bool, false: Reject unknown instances. (use $repository->create()) 'reject_unknown_related_instances' => bool, false: Auto adds unknown instances 'keep_missing_related_instances' => bool, false: Auto deletes removed instances }

Return values
mixed

writeAutoCompleteHelper()

Generate php sourcecode of an subclass of Repository with all magic model functions written as normal functions.

public writeAutoCompleteHelper(string $filename[, string $class = 'DefaultRepository' ][, string $namespace = 'Generated' ]) : mixed

Allows AutoCompletion of the magic get$Model(), save$Model(), etc functions.

Parameters
$filename : string
$class : string = 'DefaultRepository'

The classname of the genereted class

$namespace : string = 'Generated'

(optional) The namespace of the generated class

Return values
mixed

_getConfig()

Lookup the ModelConfig for a modelname.

protected _getConfig(string $model) : ModelConfig
Parameters
$model : string
Return values
ModelConfig

convertToData()

protected convertToData(mixed $instance, ModelConfig $config) : mixed
Parameters
$instance : mixed
$config : ModelConfig
Return values
mixed

convertToInstance()

Convert raw backend data into an object instance.

protected convertToInstance(mixed $data, ModelConfig $config[, string|null $index = null ][, bool $reload = false ]) : stdClass
Parameters
$data : mixed
$config : ModelConfig
$index : string|null = null

(optional) speedoptim: Prevents resolving the index again.

$reload : bool = false

true: Overwrite properties in the instance.

Return values
stdClass

defaultInstance()

protected static defaultInstance() : mixed
Return values
mixed

registerModel()

Add an configution for a model.

protected registerModel(ModelConfig $config) : mixed
Parameters
$config : ModelConfig
Return values
mixed

resolveIndex()

Return the ($this->objects) index.

protected resolveIndex(mixed $from[, ModelConfig $config = null ]) : string
Parameters
$from : mixed

data, instance or an id string or array

$config : ModelConfig = null
Return values
string

_triggerEvent()

private _triggerEvent(mixed $instance, mixed $event) : mixed
Parameters
$instance : mixed
$event : mixed
Return values
mixed

buildAlias()

Builds a uses array

private buildAlias(string $fqcn, array<string|int, mixed> &$aliases, mixed &$use) : string
Parameters
$fqcn : string

full qualified classname

$aliases : array<string|int, mixed>

array with the

$use : mixed
Return values
string

alias

ref()

private ref() : mixed
Return values
mixed

Search results