/** * 设定配置 * * @param array|string $key * @param mixed $value * @return void */ public function set($key, $value = null) { if (is_array($key)) { foreach ($key as $innerKey => $innerValue) { Arr::set($this->items, $innerKey, $innerValue); } } else { Arr::set($this->items, $key, $value); } }
/** * 创建一个新的PDO链接 * * @param string $dsn * @param array $config * @param array $options * @return \PDO */ public function createConnection($dsn, array $config, array $options) { $username = Arr::get($config, 'username'); $password = Arr::get($config, 'password'); try { $pdo = new PDO($dsn, $username, $password, $options); } catch (Exception $e) { $pdo = $this->tryAgainIfCausedByLostConnection($e, $dsn, $username, $password, $options); } return $pdo; }
/** * 获取链接配置文件 * * @param string $name * @return array * * @throws \InvalidArgumentException */ protected function getConfig($name) { $name = $name ?: $this->getDefaultConnection(); $connections = $this->app['env']['redis']['connections']; if (is_null($config = Arr::get($connections, $name))) { throw new InvalidArgumentException("Redis [{$name}] not configured."); } return $config; }
/** * 获取配置信息 * * @param string $option * @return mixed */ public function getConfig($option) { return Arr::get($this->config, $option); }
/** * 获取当前查询实例的绑定(一纬数组化) * * @return array */ public function getBindings() { return Arr::flatten($this->bindings); }
/** * 完成保存进程后 * * @param array $options * @return void */ protected function finishSave(array $options) { $this->event('saved', false); $this->syncOriginal(); if (Arr::get($options, 'touch', true)) { $this->touchOwners(); } }
/** * 获取一个宏定义(闭包) * * @param string $name * @return \Closure */ public function getMacro($name) { return Arr::get($this->macros, $name); }
/** * 返回集合中,最后一个通过指定测试的元素 * 你也可以不传入参数使用 last 方法以获取集合中最后一个元素 * * @param callable|null $callback * @param mixed $default * @return mixed */ public function last(callable $callback = null, $default = null) { if (is_null($callback)) { return count($this->items) > 0 ? end($this->items) : value($default); } return Arr::last($this->items, $callback, $default); }
/** * 分析配置 * * @param array $config * @param string $name * @return array */ protected function parseConfig(array $config, $name) { return Arr::add(Arr::add($config, 'prefix', ''), 'name', $name); }
/** * 获取已经注册过的服务提供商 * @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; }); }
/** * 通过点号来设置数组 * * @param mixed $target * @param string|array $key * @param mixed $value * @param bool $overwrite * @return mixed */ function data_set(&$target, $key, $value, $overwrite = true) { $segments = is_array($key) ? $key : explode('.', $key); if (($segment = array_shift($segments)) === '*') { if (!Arr::accessible($target)) { $target = []; } if ($segments) { foreach ($target as &$inner) { data_set($inner, $segments, $value, $overwrite); } } elseif ($overwrite) { foreach ($target as &$inner) { $inner = $value; } } } elseif (Arr::accessible($target)) { if ($segments) { if (!Arr::exists($target, $segment)) { $target[$segment] = []; } data_set($target[$segment], $segments, $value, $overwrite); } elseif ($overwrite || !Arr::exists($target, $segment)) { $target[$segment] = $value; } } elseif (is_object($target)) { if ($segments) { if (!isset($target->{$segment})) { $target->{$segment} = []; } data_set($target->{$segment}, $segments, $value, $overwrite); } elseif ($overwrite || !isset($target->{$segment})) { $target->{$segment} = $value; } } return $target; }