Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit c4e3cc9

Browse files
committed
add two tool trait class
1 parent b3f5227 commit c4e3cc9

File tree

2 files changed

+257
-0
lines changed

2 files changed

+257
-0
lines changed

src/traits/TraitSimpleAlias.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-02-28
6+
* Time: 9:20
7+
*/
8+
9+
namespace inhere\librarys\traits;
10+
11+
/**
12+
* Class TraitSimpleAlias
13+
* @package inhere\librarys\traits
14+
*/
15+
trait TraitSimpleAlias
16+
{
17+
/**
18+
* path alias
19+
* @var array
20+
*/
21+
protected static $aliases = [];
22+
23+
/**
24+
* set/get path alias
25+
* @param array|string $path
26+
* @param string|null $value
27+
* @return bool|string
28+
*/
29+
public static function alias($path, $value=null)
30+
{
31+
// get path by alias
32+
if ( is_string($path) && !$value ) {
33+
// don't use alias
34+
if ( $path[0] !== '@' ) {
35+
return $path;
36+
}
37+
38+
$sep = '/';
39+
$path = str_replace(['/','\\'], $sep , $path);
40+
41+
// only a alias. e.g. @project
42+
if ( !strpos($path, $sep) ) {
43+
return isset(self::$aliases[$path]) ? self::$aliases[$path] : $path;
44+
}
45+
46+
// have other partial. e.g: @project/temp/logs
47+
$realPath = $path;
48+
list($alias, $other) = explode($sep, $path, 2);
49+
50+
if ( isset(self::$aliases[$alias]) ) {
51+
$realPath = self::$aliases[$alias] . $sep . $other;
52+
}
53+
54+
return $realPath;
55+
}
56+
57+
if ( $path && $value && is_string($path) && is_string($value) ) {
58+
$path = [$path => $value];
59+
}
60+
61+
// custom set path's alias. e.g: Slim::alias([ 'alias' => 'path' ]);
62+
if ( is_array($path) ) {
63+
foreach ($path as $alias => $realPath) {
64+
// 1th char must is '@'
65+
if ( $alias[0] !== '@' ) {
66+
continue;
67+
}
68+
69+
self::$aliases[$alias] = $realPath;
70+
}
71+
}
72+
73+
return true;
74+
}
75+
76+
/**
77+
* @return array
78+
*/
79+
public static function getAliases()
80+
{
81+
return self::$aliases;
82+
}
83+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-02-28
6+
* Time: 9:22
7+
*/
8+
9+
namespace inhere\librarys\traits;
10+
11+
/**
12+
* Class TraitSimpleContainer
13+
* @package inhere\librarys\traits
14+
*/
15+
trait TraitSimpleContainer
16+
{
17+
/**
18+
* all raw register service list
19+
* @var array
20+
*/
21+
private static $services = [];
22+
23+
/**
24+
* all parsed instance list by call service.
25+
* @var array
26+
*/
27+
private static $instances = [];
28+
29+
/**********************************************************
30+
* application service
31+
**********************************************************/
32+
33+
/**
34+
* register a app service
35+
* @param string $name the service name
36+
* @param object|\Closure $service service
37+
* @param bool $replace replace exists service
38+
* @return static
39+
*/
40+
public function register($name, $service, $replace = false)
41+
{
42+
$this->set($name, $service, $replace);
43+
44+
return $this;
45+
}
46+
47+
/**
48+
* register a app service
49+
* @param string $name
50+
* @param object $service
51+
* @param bool $replace replace exists service
52+
* @return bool
53+
*/
54+
public function set($name, $service, $replace = false)
55+
{
56+
// have been used.
57+
if ( isset(self::$instances[$name]) ) {
58+
throw new \LogicException("The service [$name] have been instanced, don't allow override it.");
59+
}
60+
61+
// setting
62+
if ( !isset(self::$services[$name]) || $replace ) {
63+
self::$services[$name] = $service;
64+
}
65+
66+
return true;
67+
}
68+
69+
/**
70+
* get a app service by name
71+
* if is a closure, only run once.
72+
* @param string $name
73+
* @param bool $call if service is 'Closure', call it.
74+
* @return mixed
75+
*/
76+
public function get($name, $call = true)
77+
{
78+
if ( !isset(self::$services[$name]) ) {
79+
throw new \RuntimeException("The service [$name] don't register.");
80+
}
81+
82+
$service = self::$services[$name];
83+
84+
if ( is_object($service) && $service instanceof \Closure && $call) {
85+
if ( !isset(self::$instances[$name]) ) {
86+
self::$instances[$name] = $service($this);
87+
}
88+
89+
return self::$instances[$name];
90+
}
91+
92+
return $service;
93+
}
94+
95+
/**
96+
* @param string $name
97+
* @return mixed
98+
*/
99+
public function raw($name)
100+
{
101+
if ( !isset(self::$services[$name]) ) {
102+
throw new \RuntimeException("The service [$name] don't register.");
103+
}
104+
105+
return self::$services[$name];
106+
}
107+
108+
/**
109+
* create a app service by name
110+
* it always return a new instance.
111+
* @param string $name
112+
* @return object
113+
*/
114+
public function factory($name)
115+
{
116+
if ( !isset(self::$services[$name]) ) {
117+
throw new \RuntimeException("The service [$name] don't register.");
118+
}
119+
120+
$service = self::$services[$name];
121+
122+
if ( is_object($service) && $service instanceof \Closure ) {
123+
return $service($this);
124+
}
125+
126+
return $service;
127+
}
128+
129+
/**
130+
* @param $name
131+
* @return bool
132+
*/
133+
public function has($name)
134+
{
135+
return isset(self::$services[$name]);
136+
}
137+
138+
/**
139+
* @return array
140+
*/
141+
public function getServiceNames()
142+
{
143+
return array_keys(self::$services);
144+
}
145+
146+
/**
147+
* allow register a app service by property
148+
* ```
149+
* $app->logger = function(){
150+
* return new xx\yy\Logger;
151+
* };
152+
* ```
153+
* @param string $name
154+
* @param object $service
155+
* @return bool
156+
*/
157+
public function __set($name, $service)
158+
{
159+
return $this->set($name, $service);
160+
}
161+
162+
/**
163+
* allow call service by property
164+
* ```
165+
* $logger = Micro::$me->logger;
166+
* ```
167+
* @param string $name service name
168+
* @return object
169+
*/
170+
public function __get($name)
171+
{
172+
return $this->get($name);
173+
}
174+
}

0 commit comments

Comments
 (0)