This commit is contained in:
2020-12-14 17:35:35 +08:00
parent b278c028c3
commit 27f94b97a9
5 changed files with 72 additions and 37 deletions
+8 -7
View File
@@ -6,6 +6,7 @@
* Time: 17:51
*/
declare(strict_types=1);
namespace HttpServer\Http\Formatter;
@@ -29,30 +30,30 @@ class HtmlFormatter extends Application implements IFormatter
public array $header = [];
/**
* @param $data
* @param $context
* @return $this
* @throws \Exception
*/
public function send($data)
public function send($context): static
{
if (!is_string($data)) {
$data = JSON::encode($data);
if (!is_string($context)) {
$context = JSON::encode($context);
}
$this->data = $data;
$this->data = $context;
return $this;
}
/**
* @return mixed
*/
public function getData()
public function getData(): mixed
{
$data = $this->data;
$this->clear();
return $data;
}
public function clear()
public function clear(): void
{
$this->data = null;
unset($this->data);
+9 -9
View File
@@ -6,6 +6,7 @@
* Time: 17:18
*/
declare(strict_types=1);
namespace HttpServer\Http\Formatter;
use Exception;
@@ -25,23 +26,22 @@ class JsonFormatter extends Application implements IFormatter
public array $header = [];
/**
* @param $data
* @return $this|IFormatter
* @throws Exception
* @param $context
* @return JsonFormatter
*/
public function send($data)
public function send($context): static
{
if (!is_string($data)) {
$data = json_encode($data);
if (!is_string($context)) {
$context = json_encode($context);
}
$this->data = $data;
$this->data = $context;
return $this;
}
/**
* @return mixed
*/
public function getData()
public function getData(): mixed
{
$data = $this->data;
$this->clear();
@@ -49,7 +49,7 @@ class JsonFormatter extends Application implements IFormatter
}
public function clear()
public function clear(): void
{
$this->data = null;
unset($this->data);
+8 -7
View File
@@ -6,6 +6,7 @@
* Time: 17:29
*/
declare(strict_types=1);
namespace HttpServer\Http\Formatter;
@@ -30,17 +31,17 @@ class XmlFormatter extends Application implements IFormatter
public array $header = [];
/**
* @param $data
* @param $context
* @return $this
* @throws \Exception
*/
public function send($data)
public function send($context): static
{
if (!is_string($data)) {
if (!is_string($context)) {
// TODO: Implement send() method.
$dom = new SimpleXMLElement('<xml/>');
$this->toXml($dom, $data);
$this->toXml($dom, $context);
$this->data = $dom->saveXML();
}
@@ -48,9 +49,9 @@ class XmlFormatter extends Application implements IFormatter
}
/**
* @return string
* @return mixed
*/
public function getData()
public function getData(): mixed
{
$data = $this->data;
$this->clear();
@@ -80,7 +81,7 @@ class XmlFormatter extends Application implements IFormatter
}
}
public function clear()
public function clear(): void
{
$this->data = null;
unset($this->data);
+34 -7
View File
@@ -50,6 +50,14 @@ class Container extends BaseObject
private array $_param = [];
/**
* @var array
*
* The method attributes
*/
private array $_attributes = [];
/**
* @param $class
* @param array $constrict
@@ -59,7 +67,7 @@ class Container extends BaseObject
* @throws NotFindClassException
* @throws ReflectionException
*/
public function get($class, $constrict = [], $config = [])
public function get($class, $constrict = [], $config = []): mixed
{
if (isset($this->_singletons[$class])) {
return $this->_singletons[$class];
@@ -88,7 +96,7 @@ class Container extends BaseObject
* @throws NotFindClassException
* @throws ReflectionException
*/
private function resolveDefinition($definition, $class, $config, $constrict)
private function resolveDefinition($definition, $class, $config, $constrict): mixed
{
if (!isset($definition['class'])) {
throw new NotFindClassException($class);
@@ -112,11 +120,11 @@ class Container extends BaseObject
* @param $constrict
* @param $config
*
* @return mixed
* @return object
* @throws NotFindClassException
* @throws ReflectionException
*/
private function resolve($class, $constrict, $config)
private function resolve($class, $constrict, $config): object
{
/**
* @var ReflectionClass $reflect
@@ -153,7 +161,7 @@ class Container extends BaseObject
* @return array
* @throws ReflectionException
*/
private function resolveDependencies($class)
private function resolveDependencies($class): array
{
$dependencies = [];
if (isset($this->_reflection[$class])) {
@@ -195,6 +203,25 @@ class Container extends BaseObject
}
/**
* @param string $class
* @return array
* @throws ReflectionException
*/
public function getAttributes(string $class): array
{
$reflection = $this->getReflect($class);
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
$classAttribute = $reflection->getAttributes();
foreach ($methods as $method) {
$this->_attributes[$reflection->getName()][$method->getName()] = $method->getAttributes();
}
return $this->_attributes[$reflection->getName()];
}
/**
* @param $class
*/
@@ -214,7 +241,7 @@ class Container extends BaseObject
/**
* @return $this
*/
public function flush()
public function flush(): static
{
$this->_reflection = [];
$this->_singletons = [];
@@ -229,7 +256,7 @@ class Container extends BaseObject
*
* @return mixed
*/
private function mergeParam($class, $newParam)
private function mergeParam($class, $newParam): array
{
if (empty($this->_param[$class])) {
return $newParam;
+13 -7
View File
@@ -10,9 +10,11 @@ declare(strict_types=1);
namespace Snowflake\Di;
use ReflectionException;
use Snowflake\Exception\ComponentException;
use Snowflake\Abstracts\Component;
use Exception;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
@@ -36,7 +38,7 @@ class Service extends Component
* @return mixed
* @throws
*/
public function get($id)
public function get($id): mixed
{
if (isset($this->_components[$id])) {
return $this->_components[$id];
@@ -69,10 +71,12 @@ class Service extends Component
* @param $id
* @param $definition
*
* @return callable|mixed|void
* @throws Exception
* @return mixed
* @throws ComponentException
* @throws ReflectionException
* @throws NotFindClassException
*/
public function set($id, $definition)
public function set($id, $definition): mixed
{
if ($definition === NULL) {
return $this->remove($id);
@@ -95,7 +99,7 @@ class Service extends Component
* @param $id
* @return bool
*/
public function has($id)
public function has($id): bool
{
return isset($this->_definition[$id]) || isset($this->_components[$id]) || isset($this->_alias[$id]);
}
@@ -117,7 +121,7 @@ class Service extends Component
* @return mixed
* @throws Exception
*/
public function __get($name)
public function __get($name): mixed
{
if ($this->has($name)) {
return $this->get($name);
@@ -128,8 +132,9 @@ class Service extends Component
/**
* @param $id
* @return bool
*/
public function remove($id)
public function remove($id): bool
{
unset($this->_components[$id]);
unset($this->_definition[$id]);
@@ -138,5 +143,6 @@ class Service extends Component
unset($this->_definition[$this->_alias[$id]]);
unset($this->_alias[$id]);
}
return $this->has($id);
}
}