Skip to content

Commit 88b8b4c

Browse files
authored
Merge pull request #787 from swoft-cloud/feature
Feature
2 parents a3aa889 + b7a08c6 commit 88b8b4c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2030
-250
lines changed

Dockerfile

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1-
FROM php:7.1
1+
# @description php image base on the debian 9.x
2+
#
3+
# Some Information
4+
# ------------------------------------------------------------------------------------
5+
# @link https://hub.docker.com/_/debian/ alpine image
6+
# @link https://hub.docker.com/_/php/ php image
7+
# @link https://github.com/docker-library/php php dockerfiles
8+
# @see https://github.com/docker-library/php/tree/master/7.2/stretch/cli/Dockerfile
9+
# ------------------------------------------------------------------------------------
10+
# @build-example docker build . -f Dockerfile -t swoft/swoft
11+
#
12+
FROM php:7.2
213

314
LABEL maintainer="inhere <in.798@qq.com>" version="2.0"
415

5-
# Version
6-
ENV PHPREDIS_VERSION=4.3.0 \
7-
SWOOLE_VERSION=4.3.5
16+
# --build-arg timezone=Asia/Shanghai
17+
ARG timezone
18+
# app env: prod pre test dev
19+
ARG app_env=prod
20+
# default use www-data user
21+
ARG work_user=www-data
22+
23+
ENV APP_ENV=${app_env:-"prod"} \
24+
TIMEZONE=${timezone:-"Asia/Shanghai"} \
25+
PHPREDIS_VERSION=4.3.0 \
26+
SWOOLE_VERSION=4.3.5 \
27+
COMPOSER_ALLOW_SUPERUSER=1
828

929
ADD . /var/www/swoft
1030

@@ -13,35 +33,25 @@ RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
1333
&& echo 'Asia/Shanghai' > /etc/timezone \
1434
# Libs
1535
&& apt-get update \
16-
&& apt-get install -y \
17-
curl \
18-
wget \
19-
git \
20-
zip \
36+
&& apt-get install -y --no-install-recommends \
37+
curl wget git zip unzip less vim openssl \
2138
libz-dev \
2239
libssl-dev \
2340
libnghttp2-dev \
2441
libpcre3-dev \
25-
&& apt-get clean \
26-
&& apt-get autoremove \
2742
# Install composer
2843
&& curl -sS https://getcomposer.org/installer | php \
2944
&& mv composer.phar /usr/local/bin/composer \
3045
&& composer self-update --clean-backups \
31-
# Some php extension
32-
&& docker-php-ext-install pdo_mysql \
33-
bcmath \
34-
sockets \
35-
zip \
36-
sysvmsg \
37-
sysvsem \
38-
sysvshm \
39-
# Redis extension
46+
# Install PHP extensions
47+
&& docker-php-ext-install \
48+
bcmath gd pdo_mysql mbstring sockets zip sysvmsg sysvsem sysvshm \
49+
# Install redis extension
4050
&& wget http://pecl.php.net/get/redis-${PHPREDIS_VERSION}.tgz -O /tmp/redis.tar.tgz \
4151
&& pecl install /tmp/redis.tar.tgz \
4252
&& rm -rf /tmp/redis.tar.tgz \
4353
&& docker-php-ext-enable redis \
44-
# Swoole extension
54+
# Install swoole extension
4555
&& wget https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O swoole.tar.gz \
4656
&& mkdir -p swoole \
4757
&& tar -xf swoole.tar.gz -C swoole --strip-components=1 \
@@ -55,9 +65,16 @@ RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
5565
) \
5666
&& rm -r swoole \
5767
&& docker-php-ext-enable swoole \
68+
# Clear dev deps
69+
&& apt-get clean \
70+
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
71+
# Timezone
72+
&& cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
73+
&& echo "${TIMEZONE}" > /etc/timezone \
74+
&& echo "[Date]\ndate.timezone=${TIMEZONE}" > /usr/local/etc/php/conf.d/timezone.ini \
5875
# Install composer deps
5976
&& cd /var/www/swoft \
60-
&& composer install \
77+
&& composer install --no-dev \
6178
&& composer clearcache
6279

6380
WORKDIR /var/www/swoft

app/Annotation/Mapping/AlphaDash.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\Annotation\Mapping;
4+
5+
use Doctrine\Common\Annotations\Annotation\Attribute;
6+
use Doctrine\Common\Annotations\Annotation\Attributes;
7+
8+
/**
9+
* Class AlphaDash
10+
*
11+
* @since 2.0
12+
*
13+
* @Annotation
14+
* @Attributes({
15+
* @Attribute("message",type="string")
16+
* })
17+
*/
18+
class AlphaDash
19+
{
20+
/**
21+
* @var string
22+
*/
23+
private $message = '';
24+
25+
/**
26+
* @var string
27+
*/
28+
private $name = '';
29+
30+
/**
31+
* StringType constructor.
32+
*
33+
* @param array $values
34+
*/
35+
public function __construct(array $values)
36+
{
37+
if (isset($values['value'])) {
38+
$this->message = $values['value'];
39+
}
40+
if (isset($values['message'])) {
41+
$this->message = $values['message'];
42+
}
43+
if (isset($values['name'])) {
44+
$this->name = $values['name'];
45+
}
46+
}
47+
48+
/**
49+
* @return string
50+
*/
51+
public function getMessage(): string
52+
{
53+
return $this->message;
54+
}
55+
56+
/**
57+
* @return string
58+
*/
59+
public function getName(): string
60+
{
61+
return $this->name;
62+
}
63+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\Annotation\Parser;
4+
5+
use ReflectionException;
6+
use Swoft\Annotation\Annotation\Mapping\AnnotationParser;
7+
use Swoft\Annotation\Annotation\Parser\Parser;
8+
use App\Annotation\Mapping\AlphaDash;
9+
use Swoft\Validator\Exception\ValidatorException;
10+
use Swoft\Validator\ValidatorRegister;
11+
12+
/**
13+
* Class AlphaDashParser
14+
*
15+
* @AnnotationParser(annotation=AlphaDash::class)
16+
*/
17+
class AlphaDashParser extends Parser
18+
{
19+
/**
20+
* @param int $type
21+
* @param object $annotationObject
22+
*
23+
* @return array
24+
* @throws ReflectionException
25+
* @throws ValidatorException
26+
*/
27+
public function parse(int $type, $annotationObject): array
28+
{
29+
if ($type != self::TYPE_PROPERTY) {
30+
return [];
31+
}
32+
ValidatorRegister::registerValidatorItem($this->className, $this->propertyName, $annotationObject);
33+
return [];
34+
}
35+
}

app/Common/DbSelector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ public function select(Connection $connection): void
3636
$dbName = sprintf('%s%s', $createDbName, (string)$selectIndex);
3737
$connection->db($dbName);
3838
}
39-
}
39+
}

app/Common/RpcProvider.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php declare(strict_types=1);
2+
3+
4+
namespace App\Common;
5+
6+
7+
use ReflectionException;
8+
use Swoft\Bean\Annotation\Mapping\Bean;
9+
use Swoft\Bean\Annotation\Mapping\Inject;
10+
use Swoft\Bean\Exception\ContainerException;
11+
use Swoft\Consul\Agent;
12+
use Swoft\Consul\Exception\ClientException;
13+
use Swoft\Consul\Exception\ServerException;
14+
use Swoft\Rpc\Client\Client;
15+
use Swoft\Rpc\Client\Contract\ProviderInterface;
16+
17+
/**
18+
* Class RpcProvider
19+
*
20+
* @since 2.0
21+
*
22+
* @Bean()
23+
*/
24+
class RpcProvider implements ProviderInterface
25+
{
26+
/**
27+
* @Inject()
28+
*
29+
* @var Agent
30+
*/
31+
private $agent;
32+
33+
/**
34+
* @param Client $client
35+
*
36+
* @return array
37+
* @throws ReflectionException
38+
* @throws ContainerException
39+
* @throws ClientException
40+
* @throws ServerException
41+
* @example
42+
* [
43+
* 'host:port',
44+
* 'host:port',
45+
* 'host:port',
46+
* ]
47+
*/
48+
public function getList(Client $client): array
49+
{
50+
// Get health service from consul
51+
$services = $this->agent->services();
52+
53+
$services = [
54+
55+
];
56+
57+
return $services;
58+
}
59+
}

app/Console/Command/AgentCommand.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php declare(strict_types=1);
2+
3+
4+
namespace App\Console\Command;
5+
6+
use ReflectionException;
7+
use Swoft\Apollo\Config;
8+
use Swoft\Apollo\Exception\ApolloException;
9+
use Swoft\Bean\Annotation\Mapping\Inject;
10+
use Swoft\Bean\Exception\ContainerException;
11+
use Swoft\Co;
12+
use Swoft\Console\Annotation\Mapping\Command;
13+
use Swoft\Console\Annotation\Mapping\CommandMapping;
14+
use Swoft\Http\Server\HttpServer;
15+
use Swoft\Log\Helper\CLog;
16+
use Swoft\Rpc\Server\ServiceServer;
17+
use Swoft\WebSocket\Server\WebSocketServer;
18+
use Throwable;
19+
20+
/**
21+
* Class AgentCommand
22+
*
23+
* @since 2.0
24+
*
25+
* @Command("agent")
26+
*/
27+
class AgentCommand
28+
{
29+
/**
30+
* @Inject()
31+
*
32+
* @var Config
33+
*/
34+
private $config;
35+
36+
/**
37+
* @CommandMapping(name="index")
38+
*/
39+
public function index(): void
40+
{
41+
$namespaces = [
42+
'application'
43+
];
44+
45+
while (true) {
46+
try {
47+
$this->config->listen($namespaces, [$this, 'updateConfigFile']);
48+
} catch (Throwable $e) {
49+
CLog::error('Config agent fail(%s %s %d)!', $e->getMessage(), $e->getFile(), $e->getLine());
50+
}
51+
}
52+
}
53+
54+
/**
55+
* @param array $data
56+
*
57+
* @throws ContainerException
58+
* @throws ReflectionException
59+
*/
60+
public function updateConfigFile(array $data): void
61+
{
62+
foreach ($data as $namespace => $namespaceData) {
63+
$configFile = sprintf('@config/%s.php', $namespace);
64+
65+
$configKVs = $namespaceData['configurations'] ?? '';
66+
$content = '<?php return ' . var_export($configKVs, true) . ';';
67+
Co::writeFile(alias($configFile), $content, FILE_NO_DEFAULT_CONTEXT);
68+
69+
CLog::info('Apollo update success!');
70+
71+
// /** @var HttpServer $server */
72+
// $server = bean('httpServer');
73+
// $server->restart();
74+
75+
// /** @var ServiceServer $server */
76+
// $server = bean('rpcServer');
77+
// $server->restart();
78+
79+
/* @var WebSocketServer $server */
80+
$server = bean('wsServer');
81+
$server->restart();
82+
}
83+
}
84+
}

app/Console/Command/DemoCommand.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Swoft\Console\Annotation\Mapping\Command;
66
use Swoft\Console\Annotation\Mapping\CommandMapping;
7+
use Swoft\Console\Exception\ConsoleErrorException;
78
use Swoft\Console\Helper\Show;
89
use Swoft\Console\Input\Input;
910

@@ -25,4 +26,13 @@ public function test(Input $input): void
2526
'opts' => $input->getOptions(),
2627
]);
2728
}
29+
30+
/**
31+
* @CommandMapping("err")
32+
* @throws ConsoleErrorException
33+
*/
34+
public function coError(): void
35+
{
36+
ConsoleErrorException::throw('this is an error message');
37+
}
2838
}

0 commit comments

Comments
 (0)