上節,我們研讀了Pimple容器的具體實現,這節我們來查看Slim默認容器的實現細節。簡單瀏覽代碼我們可知,Slim默認容器其實就是對Pimple容器進行的一層封裝。前面我們已知Slim\Container完成了集成默認配置項,註冊默認服務。現在,讓我們接着看剩下的代碼。
/**
* 通過標識符查找容器的條目並將其返回
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
*
* @throws ContainerValueNotFoundException No entry was found for this identifier.
* @throws ContainerException Error while retrieving the entry.
*
* @return mixed Entry.
*/
public function get($id)
{
// 檢查參數或對象是否存在
if (!$this->offsetExists($id)) {
throw new ContainerValueNotFoundException(sprintf('Identifier "%s" is not defined.', $id));
}
try {
// 根據$id返回一個參數或對象
return $this->offsetGet($id);
} catch (\InvalidArgumentException $exception) {
// 是否該類offsetGet()出現異常。
if ($this->exceptionThrownByContainer($exception)) {
throw new SlimContainerException(
sprintf('Container error while retrieving "%s"', $id),
null,
$exception
);
} else {
throw $exception;
}
}
}
- BadFunctionCallException —錯誤函數調用異常
- BadMethodCallException —錯誤方法調用異常
- DomainException —作用域異常
- InvalidArgumentException —非法參數異常
- LengthException —長度異常
- LogicException —邏輯異常
- OutOfBoundsException —違背安全原則異常
- OutOfRangeException —越界索引異常
- OverflowException —上溢異常
- RangeException —範圍異常
- RuntimeException —運行時異常
- UnderflowException —下溢異常
- UnexpectedValueException —意外數值異常
private function exceptionThrownByContainer(\InvalidArgumentException $exception)
{
// 跟蹤異常(包括文件地址、報錯類、函數信息等)
$trace = $exception->getTrace()[0];
return $trace['class'] === PimpleContainer::class && $trace['function'] === 'offsetGet';
}
/**
* 如果容器可以返回給定標識符的條目,則返回true。
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* @param string $id Identifier of the entry to look for.
*
* @return boolean
*/
public function has($id)
{
return $this->offsetExists($id);
}
/********************************************************************************
* Magic methods for convenience
*******************************************************************************/
// 魔術方法,讀取不可訪問的屬性時__get會使用,然後調用get方法
public function __get($name)
{
return $this->get($name);
}
// 魔術方法,當對不可訪問屬性調用isset()或empty()時,__isset()會被調用
public function __isset($name)
{
return $this->has($name);
}