Skip to content

Commit 4ae88b5

Browse files
author
hogus
committed
first commit
0 parents commit 4ae88b5

20 files changed

+727
-0
lines changed

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = false
10+
11+
[*.{vue,js,scss}]
12+
charset = utf-8
13+
indent_style = space
14+
indent_size = 2
15+
end_of_line = lf
16+
insert_final_newline = true
17+
trim_trailing_whitespace = true
18+
19+
[*.md]
20+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
* text=auto
2+
3+
/tests export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
.scrutinizer.yml export-ignore
7+
.travis.yml export-ignore
8+
phpunit.php export-ignore
9+
phpunit.xml.dist export-ignore
10+
phpunit.xml export-ignore
11+
.php_cs export-ignore

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.idea
2+
*.DS_Store
3+
/vendor
4+
/coverage
5+
sftp-config.json
6+
composer.lock
7+
.subsplit
8+
.php_cs.cache

.php_cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
$header = <<<EOF
3+
This file is part of the hogus/laravel-miniprogram.
4+
5+
(c) hogus <hogus2037@gmail.com>
6+
7+
This source file is subject to the MIT license that is bundled.
8+
EOF;
9+
10+
return PhpCsFixer\Config::create()
11+
->setRiskyAllowed(true)
12+
->setRules(array(
13+
'@Symfony' => true,
14+
'header_comment' => array('header' => $header),
15+
'array_syntax' => array('syntax' => 'short'),
16+
'ordered_imports' => true,
17+
'no_useless_else' => true,
18+
'no_useless_return' => true,
19+
'php_unit_construct' => true,
20+
'php_unit_strict' => true,
21+
))
22+
->setFinder(
23+
PhpCsFixer\Finder::create()
24+
->exclude('vendor')
25+
->in(__DIR__)
26+
)
27+
;

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<h1 align="center"> laravel-miniprogram </h1>
2+
3+
<p align="center"> .</p>
4+
5+
6+
## Installing
7+
8+
```shell
9+
$ composer require hogus/laravel-miniprogram -vvv
10+
```
11+
## Usage
12+
13+
```php
14+
$gateway = Miniprogram::gateway('wechat');
15+
16+
$gateway->code2session($code); //code2session
17+
$gateway->getAccessToken(); //获取用户授权信息
18+
```
19+
20+
## Contributing
21+
22+
目前仅支持:`wechat``toutiao``baidu`
23+
24+
使用前复制配置文件到项目中: `config/miniprogram.php`
25+
26+
`php artisan vendor:publish --provider="Hogus\LaravelMiniProgram\ServiceProvider" --tag="config"`
27+
28+
29+
## License
30+
31+
MIT

composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "hogus/laravel-miniprogram",
3+
"description": "各平台小程序集合",
4+
"keywords": ["miniprogram", "mini-program", "WeChat", "TouTiao", "BaiDU"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "hogus",
9+
"email": "hogus2037@gmail.com"
10+
}
11+
],
12+
"require": {
13+
"php": ">=7.0",
14+
"guzzlehttp/guzzle": "^6.5"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"Hogus\\LaravelMiniProgram\\": "src"
19+
}
20+
},
21+
"extra": {
22+
"laravel": {
23+
"providers": [
24+
"Hogus\\LaravelMiniProgram\\ServiceProvider"
25+
],
26+
"aliases": {
27+
"Miniprogram": "Hogus\\LaravelMiniProgram\\Facade\\Miniprogram"
28+
}
29+
}
30+
}
31+
}

phpunit.xml.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Application Test Suite">
13+
<directory>./tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist>
18+
<directory suffix=".php">src/</directory>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

src/.gitkeep

Whitespace-only changes.

src/Facade/Miniprogram.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Hogus\LaravelMiniProgram\Facade;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class Miniprogram extends Facade
8+
{
9+
public static function getFacadeAccessor()
10+
{
11+
return 'miniprogram';
12+
}
13+
}

src/MiniProgramManager.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Hogus\LaravelMiniProgram;
4+
5+
use Hogus\LaravelMiniProgram\Supports\Config;
6+
7+
/**
8+
* Class MiniProgramManager
9+
* @package Hogus\LaravelMiniProgram
10+
*/
11+
class MiniProgramManager
12+
{
13+
/**
14+
* @var Config
15+
*/
16+
protected $config;
17+
18+
/**
19+
* @var array
20+
*/
21+
protected $gateways = [];
22+
23+
/**
24+
* MiniProgramManager constructor.
25+
* @param array $config
26+
*/
27+
public function __construct(array $config)
28+
{
29+
$this->config = new Config($config);
30+
}
31+
32+
/**
33+
* gateway
34+
*
35+
* @param null $driver
36+
* @return mixed
37+
*/
38+
public function gateway($driver = null)
39+
{
40+
$driver = $driver ?: $this->getDefaultGateway();
41+
42+
if (!isset($this->gateways[$driver])) {
43+
$this->gateways[$driver] = $this->createGateway($driver);
44+
}
45+
46+
return $this->gateways[$driver];
47+
}
48+
49+
/**
50+
* getDefaultGateway
51+
*
52+
* @return array|mixed|null
53+
*/
54+
protected function getDefaultGateway()
55+
{
56+
return $this->config->get('default');
57+
}
58+
59+
/**
60+
* createGateway
61+
*
62+
* @param $driver
63+
* @return mixed
64+
*/
65+
public function createGateway($driver)
66+
{
67+
$config = $this->config->get("gateways.".$driver);
68+
69+
if (is_null($config)) {
70+
throw new \UnexpectedValueException("Gateway [$driver] is not defined.");
71+
}
72+
73+
$class = __NAMESPACE__.'\\Providers\\'.$config['driver'].'Provider';
74+
75+
if (!class_exists($class)) {
76+
throw new \UnexpectedValueException("Class '$class' not found");
77+
}
78+
79+
return $this->buildProvider($class, $config);
80+
}
81+
82+
/**
83+
* buildProvider
84+
*
85+
* @param $provider
86+
* @param $config
87+
* @return mixed
88+
*/
89+
public function buildProvider($provider, $config)
90+
{
91+
return new $provider($config['appid'], $config['secret']);
92+
}
93+
}
94+

0 commit comments

Comments
 (0)