Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Commit

Permalink
Fixed some minor syntax issues. Organized the Context\Depository to b…
Browse files Browse the repository at this point in the history
…e alphabetical.
  • Loading branch information
milesj committed Feb 22, 2015
1 parent 97a28e2 commit af7b023
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 142 deletions.
2 changes: 1 addition & 1 deletion docs/en/packages/common/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Common Package #

The Common package is a collection of interfaces, traits, and abstract implementations for common patterns and use cases. Some of the functionality found in this package is memoization, instantiation, mutability, and much more.
The Common package is a collection of interfaces, traits, and abstract implementations for common patterns and use cases. Some of the functionality found in this package is memoization, mutability, and much more.

### Requirements ###

Expand Down
1 change: 1 addition & 0 deletions src/Titon/Cache/Cache.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use Titon\Cache\Storage;
* @package Titon\Cache
*/
class Cache {

/**
* Storage engines.
*
Expand Down
281 changes: 142 additions & 139 deletions src/Titon/Context/Depository.hh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class Depository {
protected AliasMap $aliases = Map {};

/**
* Instantiate a new container object.
* Instantiate a persistent container object.
*
* @var \Titon\Context\Depository
*/
protected static ?Depository $instance;

Expand All @@ -60,10 +62,43 @@ class Depository {
$this->singleton('Titon\Context\Depository', $this);
}

/**
* Alias a string to map to a registered item in the container. This allows
* you to call 'make' on an alias that maps to a more complex class name,
* Closure, or singleton instance.
*
* @param $alias string The alias to register
* @param $key string The original class name or key registered
*
* @return $this Return the depository for fluent method chaining
*/
public function alias(string $alias, string $key): this {
if ($this->aliases->contains($alias)) {
throw new AlreadyRegisteredException("Alias $alias has already been mapped to {$this->aliases[$alias]}");
}

$this->aliases[$alias] = $key;

return $this;
}

/**
* Clear all registered items, singletons, and aliases in the Depository.
*
* @return $this Return the depository for fluent method chaining
*/
public function clear(): this {
$this->aliases->clear();
$this->singletons->clear();
$this->items->clear();

return $this;
}

/**
* Retrieve the Depository singleton
*
* @return Depository Return the Depository instance
* @return \Titon\Context\Depository
*/
public static function getInstance(): Depository {
if (is_null(self::$instance)) {
Expand All @@ -74,88 +109,70 @@ class Depository {
}

/**
* Register a new class, callable, or object in the container
* Return whether or not an alias has been registered in the container.
*
* @param string $key The alias (container key) for the registered item
* @param mixed $concrete The class name, closure, object to register in
* the container, or null to use the alias as the
* class name
* @param bool $singleton Whether or not the container should register the
* concrete as a singleton or not (only if concrete
* is class name or a closure)
* @param string $alias Registered key or class name
*
* @return object|$definition Either the concrete (if an object is registered)
* or the definition of the registered item
* @return bool
*/
public function register(string $key, mixed $concrete = null, bool $singleton = false): mixed {
if ($this->isRegistered($key)) {
throw new AlreadyRegisteredException("Key $key has already been registered");
}

if (!$concrete) {
$concrete = $key;
public function isRegistered(string $alias): bool {
if ($this->aliases->contains($alias)) {
return true;
}

if (is_object($concrete) && !($concrete instanceof Closure)) {
if ($key !== get_class($concrete)) {
$this->aliases[$key] = get_class($concrete);
$key = get_class($concrete);
}

$this->singletons[$key] = $concrete;

return $concrete;
if ($this->singletons->contains($alias)) {
return true;
}

if (is_string($concrete) && $key !== $concrete && !is_callable($concrete)) {
$this->aliases[$key] = $concrete;
$key = $concrete;
if ($this->items->contains($alias)) {
return true;
}

// we need to build a definition
$definition = DefinitionFactory::factory($key, $concrete, $this);

$this->items[$key] = shape(
'definition' => $definition,
'singleton' => $singleton
);

return $definition;
return false;
}

/**
* Alias a string to map to a registered item in the container. This allows
* you to call 'make' on an alias that maps to a more complex class name,
* Closure, or singleton instance.
* Return whether or not an alias has been registered as a singleton in
* the container.
*
* @param $alias string The alias to register
* @param $key string The original class name or key registered
* @param string $alias Registered key or class name
*
* @return $this Return the depository for fluent method chaining
* @return bool
*/
public function alias(string $alias, string $key): this {
public function isSingleton(string $alias): bool {
if ($this->aliases->contains($alias)) {
throw new AlreadyRegisteredException("Alias $alias has already been mapped to {$this->aliases[$alias]}");
return $this->isSingleton($this->aliases[$alias]);
}

$this->aliases[$alias] = $key;
if ($this->singletons->contains($alias) || ($this->items->contains($alias) && $this->items[$alias]['singleton'] === true)) {
return true;
}

return $this;
return false;
}

/**
* Register a new singleton in the container.
* Retrieve (and build if necessary) the registered item from the container.
*
* @param string $alias The alias (container key) for the registered item
* @param mixed $concrete The class name, Closure, or object to register in
* the container, or null to use the alias as the
* class name
* @param string $alias Key or alias of a registered item or a class
* name, callable, or Closure to construct
* @param array<mixed> ...$arguments Additional arguments to pass into the item at
* construction
*
* @return object|$definition Either the concrete (if an object is registered)
* or the definition of the registered item
* @return mixed The resolved registered item or return value
*/
public function singleton(string $alias, mixed $concrete = null): mixed {
return $this->register($alias, $concrete, true);
public function make(mixed $alias, ...$arguments): mixed {
if (is_string($alias) && $this->isRegistered($alias)) {
return $this->getRegisteredItem($alias, ...$arguments);
}

if (is_string($alias) && class_exists($alias)) {
$definition = $this->buildClass($alias, ...$arguments);
} else {
$definition = $this->buildCallable($alias);
}

return $definition->create(...$arguments);
}

/**
Expand Down Expand Up @@ -191,61 +208,53 @@ class Depository {
}

/**
* Retrieve (and build if necessary) the registered item from the container.
* Register a new class, callable, or object in the container
*
* @param string $alias Key or alias of a registered item or a class
* name, callable, or Closure to construct
* @param array<mixed> ...$arguments Additional arguments to pass into the item at
* construction
* @param string $key The alias (container key) for the registered item
* @param mixed $concrete The class name, closure, object to register in
* the container, or null to use the alias as the
* class name
* @param bool $singleton Whether or not the container should register the
* concrete as a singleton or not (only if concrete
* is class name or a closure)
*
* @return mixed The resolved registered item or return value
* @return object|$definition Either the concrete (if an object is registered)
* or the definition of the registered item
*/
public function make(mixed $alias, ...$arguments): mixed {
if (is_string($alias) && $this->isRegistered($alias)) {
return $this->getRegisteredItem($alias, ...$arguments);
public function register(string $key, mixed $concrete = null, bool $singleton = false): mixed {
if ($this->isRegistered($key)) {
throw new AlreadyRegisteredException("Key $key has already been registered");
}

if (is_string($alias) && class_exists($alias)) {
$definition = $this->buildClass($alias, ...$arguments);
} else {
$definition = $this->buildCallable($alias);
if (!$concrete) {
$concrete = $key;
}

return $definition->create(...$arguments);
}
if (is_object($concrete) && !($concrete instanceof Closure)) {
if ($key !== get_class($concrete)) {
$this->aliases[$key] = get_class($concrete);
$key = get_class($concrete);
}

/**
* Retrieve the created definition or stored instance from the depository
* by key
*
* @param string $alias The key the item is stored under
* @param array<mixed> ...$arguments Arguments passed into creating the
* definition
*
* @return mixed
*/
protected function getRegisteredItem(string $alias, ...$arguments): mixed {
if ($this->aliases->contains($alias)) {
return $this->make($this->aliases[$alias], ...$arguments);
$this->singletons[$key] = $concrete;

return $concrete;
}

if ($this->singletons->contains($alias)) {
$retval = $this->singletons[$alias];
} else {
$definition = $this->items[$alias]['definition'];
$retval = $definition;
if (is_string($concrete) && $key !== $concrete && !is_callable($concrete)) {
$this->aliases[$key] = $concrete;
$key = $concrete;
}

if ($definition instanceof Definition) {
$retval = $definition->create(...$arguments);
}
// we need to build a definition
$definition = DefinitionFactory::factory($key, $concrete, $this);

if ($this->items->contains($alias) && $this->items[$alias]['singleton'] === true) {
$this->items->remove($alias);
$this->singletons[$alias] = $retval;
}
}
$this->items[$key] = shape(
'definition' => $definition,
'singleton' => $singleton
);

return $retval;
return $definition;
}

/**
Expand All @@ -269,16 +278,18 @@ class Depository {
}

/**
* Clear all registered items, singletons, and aliases in the Depository.
* Register a new singleton in the container.
*
* @return $this Return the depository for fluent method chaining
* @param string $alias The alias (container key) for the registered item
* @param mixed $concrete The class name, Closure, or object to register in
* the container, or null to use the alias as the
* class name
*
* @return object|$definition Either the concrete (if an object is registered)
* or the definition of the registered item
*/
public function clear(): this {
$this->aliases->clear();
$this->singletons->clear();
$this->items->clear();

return $this;
public function singleton(string $alias, mixed $concrete = null): mixed {
return $this->register($alias, $concrete, true);
}

/**
Expand Down Expand Up @@ -375,45 +386,37 @@ class Depository {
}

/**
* Return whether or not an alias has been registered in the container.
* Retrieve the created definition or stored instance from the depository
* by key
*
* @param string $alias Registered key or class name
* @param string $alias The key the item is stored under
* @param array<mixed> ...$arguments Arguments passed into creating the
* definition
*
* @return bool
* @return mixed
*/
public function isRegistered(string $alias): bool {
protected function getRegisteredItem(string $alias, ...$arguments): mixed {
if ($this->aliases->contains($alias)) {
return true;
return $this->make($this->aliases[$alias], ...$arguments);
}

if ($this->singletons->contains($alias)) {
return true;
}

if ($this->items->contains($alias)) {
return true;
}

return false;
}
$retval = $this->singletons[$alias];
} else {
$definition = $this->items[$alias]['definition'];
$retval = $definition;

/**
* Return whether or not an alias has been registered as a singleton in
* the container.
*
* @param string $alias Registered key or class name
*
* @return bool
*/
public function isSingleton(string $alias): bool {
if ($this->aliases->contains($alias)) {
return $this->isSingleton($this->aliases[$alias]);
}
if ($definition instanceof Definition) {
$retval = $definition->create(...$arguments);
}

if ($this->singletons->contains($alias) || ($this->items->contains($alias) && $this->items[$alias]['singleton'] === true)) {
return true;
if ($this->items->contains($alias) && $this->items[$alias]['singleton'] === true) {
$this->items->remove($alias);
$this->singletons[$alias] = $retval;
}
}

return false;
return $retval;
}
}

}

0 comments on commit af7b023

Please sign in to comment.