/** * 获取一个全局定义 * * @param string $scope * @return \Closure|null */ public static function getGlobalScope($scope) { $modelScopes = Arr::get(static::$globalScopes, get_called_class(), []); if (is_string($scope)) { return isset($modelScopes[$scope]) ? $modelScopes[$scope] : null; } return Arr::first($modelScopes, function ($key, $value) use($scope) { return $scope instanceof $value; }); }
/** * 获取已经注册过的服务提供商 * @param \CatLib\Support\ServiceProvider|string $provider 服务提供商 * @return \CatLib\Support\ServiceProvider|null */ public function getProvider($provider) { $name = is_string($provider) ? $provider : get_class($provider); return Arr::first($this->serviceProviders, function ($key, $value) use($name) { return $value instanceof $name; }); }
/** * 返回集合第一个通过指定测试的元素 * 你也可以不传入参数使用 first 方法以获取集合中第一个元素。如果集合是空的,则会返回 null * * @param callable|null $callback * @param mixed $default * @return mixed */ public function first(callable $callback = null, $default = null) { if (is_null($callback)) { return count($this->items) > 0 ? reset($this->items) : value($default); } return Arr::first($this->items, $callback, $default); }
/** * 返回第一个查找到的数组 * * @param array $array * @param callable $callback * @param mixed $default * @return mixed */ function array_first($array, callable $callback, $default = null) { return Arr::first($array, $callback, $default); }