Skip to content

Commit 7d10004

Browse files
Karl MonsonKarl Monson
Karl Monson
authored and
Karl Monson
committed
Initial Commit
0 parents  commit 7d10004

File tree

6 files changed

+190
-0
lines changed

6 files changed

+190
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_STORE

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2016 Karl Monson <[email protected]>
4+
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in
13+
> all copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
> THE SOFTWARE.

README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Ping For Laravel
2+
3+
This Laravel package is simple and unopinionated. It simply returns the HTTP Status Code for a provided URL.
4+
5+
## Installation
6+
7+
```php
8+
// config/app.php
9+
10+
'providers' => [
11+
// ...
12+
Karlmonson\Ping\PingServiceProvider::class,
13+
];
14+
15+
'aliases' => [
16+
// ...
17+
'Ping' => Karlmonson\Ping\Facades\Ping::class,
18+
];
19+
```
20+
21+
## Usage
22+
23+
```php
24+
<?php
25+
26+
namespace App\Http\Controllers;
27+
28+
use Ping;
29+
use App\Http\Controllers\Controller;
30+
31+
class LinkController extends Controller
32+
{
33+
/**
34+
* Show the current health of a given URL.
35+
*
36+
* @param string $url
37+
* @return string
38+
*/
39+
public function healthCheck($url)
40+
{
41+
$health = Ping::check($url);
42+
43+
if($health == 200) {
44+
return 'Alive!';
45+
} else {
46+
return 'Dead :(';
47+
}
48+
}
49+
}
50+
```
51+
52+
## Credits
53+
54+
- [Karl Monson](https://github.com/karlmonson) - Author
55+
- [Eric Blount](https://github.com/ericmakesstuff) - Inspiration ([ericmakesstuff/laravel-server-monitor](https://github.com/ericmakesstuff/laravel-server-monitor))
56+
57+
## License
58+
59+
The MIT License (MIT). Please see [License File](https://github.com/karlmonson/laravel-ping/blob/master/LICENSE.md) for more information.

src/Facades/Ping.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Karlmonson\Ping\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class Ping extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'ping';
17+
}
18+
}

src/Ping.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Karlmonson\Ping;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Exception\ClientException;
7+
use GuzzleHttp\Exception\ConnectException;
8+
9+
class Ping
10+
{
11+
/**
12+
* @var \GuzzleHttp\Client
13+
*/
14+
protected $client;
15+
16+
/**
17+
* @var int
18+
*/
19+
protected $responseCode;
20+
21+
/**
22+
* @var int
23+
*/
24+
protected $timeout = 5;
25+
26+
/**
27+
* @var bool
28+
*/
29+
protected $allowRedirects = true;
30+
31+
public function __construct()
32+
{
33+
$this->client = new Client([
34+
'timeout' => $this->timeout,
35+
'allow_redirects' => $this->allowRedirects,
36+
]);
37+
}
38+
39+
public function check($url)
40+
{
41+
try {
42+
$response = $this->client->get($url);
43+
$this->responseCode = $response->getStatusCode();
44+
} catch(ClientException $e) {
45+
$response = $e->getResponse();
46+
$this->responseCode = $response->getStatusCode();
47+
} catch(ConnectException $e) {
48+
$response = $e->getResponse();
49+
$this->responseCode = $response->getStatusCode();
50+
}
51+
52+
return $this->responseCode;
53+
}
54+
}

src/PingServiceProvider.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Karlmonson\Ping;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class PingServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Indicates if loading of the provider is deferred.
11+
*
12+
* @var bool
13+
*/
14+
protected $defer = true;
15+
16+
/**
17+
* Register the service provider.
18+
*
19+
* @return void
20+
*/
21+
public function register()
22+
{
23+
$this->app->singleton('ping', function ($app) {
24+
return new Ping();
25+
});
26+
}
27+
28+
/**
29+
* Get the services provided by the provider.
30+
*
31+
* @return array
32+
*/
33+
public function provides()
34+
{
35+
return ['ping'];
36+
}
37+
}

0 commit comments

Comments
 (0)