Skip to content

Commit 314bc67

Browse files
committed
初始化目录结构
1 parent 7a0d840 commit 314bc67

19 files changed

+544
-1
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# phpstorm
2+
.idea
3+
4+
# composer
5+
composer.lock
6+
vendor

.travis.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: php
2+
3+
php:
4+
- 5.5
5+
- 5.6
6+
- 7.0
7+
- 7.1
8+
9+
before_install: bash build_delayqueue.sh
10+
before_script:
11+
- composer install
12+
13+
script: vendor/bin/phpunit
14+
15+
notifications:
16+
on_success: never
17+
on_failure: always

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# delayqueue-php
2-
延迟队列PHP客户端
2+
[延迟队列](https://github.com/ouqiang/delay-queue)PHP客户端

bin/delayqueue-php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env php
2+
<?php

build_delayqueue.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
set -x -u

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "start-point/delayqueue-php",
3+
"type": "library",
4+
"description": "PHP client for DelayQueue",
5+
"keywords": ["delayqueue","delayqueue-php"],
6+
"homepage": "https://github.com/ouqiang/delayqueue-php",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "ouqiang",
11+
"email": "qingqianludao@gmail.com",
12+
"homepage": "https://github.com/ouqiang"
13+
}
14+
],
15+
"require": {
16+
"php": ">= 5.4",
17+
"ext-pcntl": "*",
18+
"monolog/monolog": "^1.0",
19+
"guzzlehttp/guzzle": "^5.0",
20+
"psr/container": "^1.0"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "4.5.*"
24+
},
25+
"bin": [
26+
"bin/delayqueue-php"
27+
],
28+
"autoload": {
29+
"psr-4": {
30+
"DelayQueue\\": "src/",
31+
"Demo\\": "demo/"
32+
}
33+
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"DelayQueue\\Tests\\": "tests/"
37+
}
38+
}
39+
}

demo/push_job.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use DelayQueue\Job;
4+
use DelayQueue\Util\Time;
5+
use DelayQueue\DelayQueue;
6+
7+
require '../vendor/autoload.php';
8+
9+
$job = new Job();
10+
$job->setTopic('order');
11+
$job->setId('15702398321');
12+
$job->setDelay(1 * Time::HOUR);
13+
$job->setTtr(60 * Time::SECOND);
14+
$job->setBody([
15+
'uid' => 10829378,
16+
'created' => 1498657365,
17+
]);

phpunit.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php">
3+
<testsuites>
4+
<testsuite>
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
</phpunit>

service.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+

src/Container/Container.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace DelayQueue\Container;
4+
5+
use Closure;
6+
use ArrayAccess;
7+
use Psr\Container\ContainerInterface;
8+
use DelayQueue\Exception\ServiceNotFoundException;
9+
10+
/**
11+
* 服务容器
12+
*/
13+
class Container implements ContainerInterface, ArrayAccess
14+
{
15+
/**
16+
* @var array 服务定义
17+
*/
18+
protected $definitions = [];
19+
/**
20+
* @var array 已实例化的服务
21+
*/
22+
protected $instances = [];
23+
24+
/**
25+
* 添加服务
26+
*
27+
* @param string $id 服务唯一标识
28+
* @param Closure $callback
29+
*/
30+
public function set($id, Closure $callback)
31+
{
32+
if ($id) {
33+
$this->definitions[$id] = $callback;
34+
}
35+
}
36+
37+
/**
38+
* 查找服务是否存在
39+
*
40+
* @param string $id 服务唯一标识
41+
* @return bool
42+
*/
43+
public function has($id)
44+
{
45+
return isset($this->definitions[$id]);
46+
}
47+
48+
/**
49+
* 获取服务
50+
*
51+
* @param string $id 服务唯一标识
52+
* @return mixed
53+
* @throws ServiceNotFoundException
54+
*/
55+
public function get($id)
56+
{
57+
if (isset($this->instances[$id])) {
58+
return $this->instances[$id];
59+
}
60+
61+
if (!isset($this->definitions[$id])) {
62+
$message = sprintf('service [%s] not exists', $id);
63+
throw new ServiceNotFoundException($message);
64+
}
65+
66+
/** @var Closure $callback */
67+
$callback = $this->definitions[$id];
68+
$callback = $callback->bindTo($this);
69+
70+
$this->instances[$id] = $callback();
71+
72+
return $this->instances[$id];
73+
}
74+
75+
public function __get($name)
76+
{
77+
return $this->get($name);
78+
}
79+
80+
public function __set($name, $value)
81+
{
82+
$this->set($name, $value);
83+
}
84+
85+
public function offsetExists($offset)
86+
{
87+
return $this->has($offset);
88+
}
89+
90+
public function offsetGet($offset)
91+
{
92+
return $this->get($offset);
93+
}
94+
95+
public function offsetSet($offset, $value)
96+
{
97+
$this->set($offset, $value);
98+
}
99+
100+
public function offsetUnset($offset)
101+
{
102+
unset($this->definitions[$offset]);
103+
unset($this->instances[$offset]);
104+
}
105+
}

0 commit comments

Comments
 (0)