1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acacha\LaravelSocial\Services; |
4
|
|
|
|
5
|
|
|
use Acacha\Filesystem\Compiler\StubFileCompiler; |
6
|
|
|
use Acacha\Filesystem\Filesystem; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class LaravelSocialiteService. |
10
|
|
|
* |
11
|
|
|
* @package Acacha\LaravelSocial\Services |
12
|
|
|
*/ |
13
|
|
|
class LaravelSocialiteService |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* OAuth app. |
17
|
|
|
* |
18
|
|
|
* @var |
19
|
|
|
*/ |
20
|
|
|
protected $oauthApp; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Social network name. |
24
|
|
|
* |
25
|
|
|
* @var |
26
|
|
|
*/ |
27
|
|
|
protected $socialNetwork; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Compiler for stub file. |
31
|
|
|
* |
32
|
|
|
* @var StubFileCompiler |
33
|
|
|
*/ |
34
|
|
|
protected $compiler; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* ConfigureLaravelSocialiteService constructor. |
38
|
|
|
* |
39
|
|
|
* @param StubFileCompiler $compiler |
40
|
|
|
* @param Filesystem $filesystem |
41
|
|
|
*/ |
42
|
|
|
public function __construct(StubFileCompiler $compiler, Filesystem $filesystem) |
43
|
|
|
{ |
44
|
|
|
$this->compiler = $compiler; |
45
|
|
|
$this->filesystem = $filesystem; |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set OAuth app. |
50
|
|
|
* |
51
|
|
|
* @param $oauthApp |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function app($oauthApp) |
55
|
|
|
{ |
56
|
|
|
$this->oauthApp = $oauthApp; |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set social network. |
62
|
|
|
* |
63
|
|
|
* @param $socialNetwork |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function social($socialNetwork) |
67
|
|
|
{ |
68
|
|
|
$this->socialNetwork = $socialNetwork; |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Configure Laravel Socialite Service. |
74
|
|
|
*/ |
75
|
|
|
public function handle() |
76
|
|
|
{ |
77
|
|
|
$this->addLaravelSocialiteService(); |
78
|
|
|
$this->addEnvironmentVariables(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add Laravel Socialite service to config/services.php file. |
83
|
|
|
*/ |
84
|
|
|
protected function addLaravelSocialiteService() |
85
|
|
|
{ |
86
|
|
|
passthru('llum service ' . $this->getSocialiteServiceStubFile()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add environment variables to .env file. |
91
|
|
|
*/ |
92
|
|
|
protected function addEnvironmentVariables() |
93
|
|
|
{ |
94
|
|
|
file_put_contents( |
95
|
|
|
base_path('.env'), |
96
|
|
|
$this->getEnvironmentFile(), |
97
|
|
|
FILE_APPEND | LOCK_EX |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get environment file. |
103
|
|
|
* |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
protected function getEnvironmentFile() |
107
|
|
|
{ |
108
|
|
|
return $this->compiler->compile( |
109
|
|
|
$this->filesystem->get($this->getSocialiteEnvironmentStubFile()), |
110
|
|
|
[ |
111
|
|
|
strtoupper($this->socialNetwork) . '_OAUTH_APP_ID' => $this->oauthApp->getId(), |
112
|
|
|
strtoupper($this->socialNetwork) . '_OAUTH_APP_SECRET' => $this->oauthApp->getSecret(), |
113
|
|
|
strtoupper($this->socialNetwork) . '_OAUTH_APP_REDIRECT_URL' => $this->oauthApp->getRedirectUrl(), |
114
|
|
|
] |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get socialite services stub file. |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
private function getSocialiteServiceStubFile() |
124
|
|
|
{ |
125
|
|
|
return __DIR__ . '/stubs/' . strtolower($this->socialNetwork) . '_service.stub'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get socialite environment stub file. |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
private function getSocialiteEnvironmentStubFile() |
134
|
|
|
{ |
135
|
|
|
return __DIR__ . '/stubs/' . strtolower($this->socialNetwork) . '_env.stub'; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: