first init

This commit is contained in:
2023-07-18 15:24:39 +02:00
commit 40693949fe
10 changed files with 287 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
lastPing.txt

8
changelog.md Normal file
View File

@@ -0,0 +1,8 @@
# Changelog
All notable changes to `LaravelVersionTracker` will be documented in this file.
## Version 1.0
### Added
- Everything

33
composer.json Normal file
View File

@@ -0,0 +1,33 @@
{
"name": "herrleinit/laravelversiontracker",
"description": "This package allows sending the Laravel version via API to a server and storing it there.",
"license": "MIT",
"version": "1.0.0",
"authors": [
{
"name": "Martin Herrlein",
"email": "info@herrlein.it",
"homepage": "https://www.herrlein.it"
}
],
"keywords": ["Laravel", "LaravelVersionTracker"],
"require": {
"php": ">=7.2",
"guzzlehttp/guzzle": "*",
"laravel/framework": ">=7.*"
},
"require-dev": {
},
"autoload": {
"psr-4": {
"Herrleinit\\LaravelVersionTracker\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Herrleinit\\LaravelVersionTracker\\ServiceProvider"
]
}
}
}

3
contributing.md Normal file
View File

@@ -0,0 +1,3 @@
# Contributing
this Repo is private. If you want to contribute, mail to info@herrlein.it

21
license.md Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2013 Martin Herrlein
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

22
phpunit.xml Normal file
View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Package">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src/</directory>
</whitelist>
</filter>
</phpunit>

48
readme.md Normal file
View File

@@ -0,0 +1,48 @@
# LaravelVersionTracker
track the version of your laravel application to a specific server
## Installation
Via Composer
setup vcs repository before
``` bash
$ composer require herrleinit/laravelversiontracker
```
## Usage
Setup .env for
``` bash
TRACKER_SERVER_URL=https://yourserver.com
TRACKER_TOKEN=yourtoken for bearer auth
```
Once the package is installed, you need nothing more to do. The package will automatically track the version of your application to the server.
```php
## Change log
Please see the [changelog](changelog.md) for more information on what has changed recently.
## Testing
``` bash
$ composer test
```
## Contributing
Please see [contributing.md](contributing.md) for details and a todolist.
## Security
If you discover any security related issues, please email info@herrlein.it instead of using the issue tracker.
## Credits
- Martin Herrlein https://www.herrlein.it
## License
MIT. Please see the [license file](license.md) for more information.

32
src/Console/SendPing.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
/**
* Created with PhpStorm.
* Author: Martin Herrlein
* info@herrlein.it
* https://www.herrlein.it
* Date: 17.07.2023
*/
namespace Herrleinit\LaravelVersionTracker\Console;
use Herrleinit\LaravelVersionTracker\LaravelVersionTracker;
use Illuminate\Console\Command;
class SendPing extends Command
{
protected $signature = 'tracker:ping';
protected $description = 'Send the current Laravel Info to an external API.';
public function __construct()
{
parent::__construct();
}
public function handle(): int
{
(new LaravelVersionTracker())->run();
return 0;
}
}

View File

@@ -0,0 +1,90 @@
<?php
namespace Herrleinit\LaravelVersionTracker;
use Carbon\Carbon;
use Illuminate\Support\Facades\Http;
class LaravelVersionTracker
{
public function run(): bool
{
if(!env('TRACKER_SERVER')) return false;
if(!env('TRACKER_TOKEN')) return false;
if(!$this->checkInterval()) return false;
$arrData = $this->gatherData();
$response = $this->sendData($arrData);
return true;
}
private function checkInterval(): bool
{
$path = __DIR__.'/../lastPing.txt';
$lastPing = false;
if(file_exists($path))$lastPing = file_get_contents($path);
if(empty($lastPing))
{
file_put_contents($path,now()->toDateTimeString());
return true;
}
$now = now();
switch(env('TRACKER_INTERVAL'))
{
case 'daily':
$nextPing = Carbon::createFromTimeString($lastPing)->addDay();
break;
case 'weekly':
$nextPing = Carbon::createFromTimeString($lastPing)->addWeek();
break;
case 'monthly':
$nextPing = Carbon::createFromTimeString($lastPing)->addMonth();
break;
case 'yearly':
$nextPing = Carbon::createFromTimeString($lastPing)->addYear();
break;
default:
$nextPing = Carbon::createFromTimeString($lastPing)->addMonth();
break;
}
$diff = $now->diffInMinutes($nextPing,false);
if($diff > 0) return false;
unlink($path);
file_put_contents($path,$now->toDateTimeString());
return true;
}
public function gatherData(): array
{
return [
'version' => app()->version(),
'phpversion' => phpversion(),
'appname' => config('app.name'),
'url' => config('app.url'),
'packages' => $this->getInstalledPackages(),
'audits' => $this->getComposerAudits(),
];
}
private function sendData($arrData): \Illuminate\Http\Client\Response
{
return Http::retry(3,250)
->withToken(env('TRACKER_TOKEN'))
->post(env('TRACKER_SERVER'), $arrData);
}
private function getInstalledPackages(): string
{
$base = base_path().'/';
return shell_exec("cd $base && composer show -f json");
}
private function getComposerAudits(): string | null
{
$base = base_path().'/';
$composerAudit = shell_exec("cd $base && composer audit -f json");
$arrAudit = json_decode($composerAudit);
return !empty($arrAudit->advisories) ? json_encode($arrAudit->advisories) : null;
}
}

29
src/ServiceProvider.php Normal file
View File

@@ -0,0 +1,29 @@
<?php
namespace Herrleinit\LaravelVersionTracker;
use Herrleinit\LaravelVersionTracker\Console\SendPing;
use Illuminate\Foundation\Console\AboutCommand;
class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot(): void
{
(new LaravelVersionTracker())->run();
}
/**
* Register any package services.
*
* @return void
*/
public function register(): void
{
}
}