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