Skip to content

Commit

Permalink
Merge pull request #9 from mtawil/support-laravel-10
Browse files Browse the repository at this point in the history
Support laravel 10
  • Loading branch information
JhumanJ authored Aug 24, 2023
2 parents 15080a9 + 9164f6c commit 88a47ed
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 126 deletions.
20 changes: 10 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
}
],
"require": {
"php": "^8.0",
"spatie/laravel-package-tools": "^1.4.3",
"illuminate/contracts": "^8.37|^v9.0.0",
"illuminate/support": "^5.8|^6.0|^7.0|^8.0|^9.0",
"php": "^8.0|^8.1|^8.2",
"spatie/laravel-package-tools": "^1.4",
"illuminate/contracts": "^8.0|^v9.0|^v10.0",
"illuminate/support": "^8.0|^9.0|^10.0",
"laravel/tinker": "^1.0|^2.0"
},
"require-dev": {
"brianium/paratest": "^6.2",
"nunomaduro/collision": "^5.3",
"orchestra/testbench": "^6.15",
"phpunit/phpunit": "^9.3",
"spatie/laravel-ray": "^1.20",
"vimeo/psalm": "^4.4"
"brianium/paratest": "^6.2|^7.0",
"nunomaduro/collision": "^5.3|^6.0|^7.0",
"orchestra/testbench": "^6.15|^7.0|^8.0",
"phpunit/phpunit": "^9.3|^10.0",
"spatie/laravel-ray": "^1.2",
"vimeo/psalm": "^4.4|^5.0"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 6 additions & 6 deletions config/model-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
|
*/

'enabled' => env('MODEL_STATS_ENABLED', true),
'allow_custom_code' => env('MODEL_STATS_CUSTOM_CODE', false),
'enabled' => env('MODEL_STATS_ENABLED', true),
'allow_custom_code' => env('MODEL_STATS_CUSTOM_CODE', false),

/*
|--------------------------------------------------------------------------
Expand All @@ -29,7 +29,7 @@
| the existing middleware. Or, you can simply stick with this list.
|
*/
'middleware' => [
'middleware' => [
'web',
\Jhumanj\LaravelModelStats\Http\Middleware\Authorize::class,
],
Expand All @@ -43,7 +43,7 @@
| this configures the table name based on your connection.
|
*/
'table_name' => 'model_stats_dashboards',
'table_name' => 'model_stats_dashboards',

/*
|--------------------------------------------------------------------------
Expand All @@ -54,7 +54,7 @@
| This can be used to ensure a read-only connection, by using a custom connection with a read-only user.
|
*/
'query_database_connection' => env('MODEL_STATS_DB_CONNECTION', env('DB_CONNECTION')),
'query_database_connection' => env('MODEL_STATS_DB_CONNECTION', env('DB_CONNECTION')),

/*
|--------------------------------------------------------------------------
Expand All @@ -65,6 +65,6 @@
| be starting the '/stats' prefix, and names will start with 'stats.'.
|
*/
'routes_prefix' => 'stats',
'routes_prefix' => 'stats',
'route_names_prefix' => 'stats.',
];
6 changes: 3 additions & 3 deletions database/factories/DashboardFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Jhumanj\LaravelModelStats\Database\Factories;

use Jhumanj\LaravelModelStats\Models\Dashboard;
use Illuminate\Database\Eloquent\Factories\Factory;
use Jhumanj\LaravelModelStats\Models\Dashboard;

class DashboardFactory extends Factory
{
Expand All @@ -12,9 +12,9 @@ class DashboardFactory extends Factory
public function definition(): array
{
return [
'name' => $this->faker->name,
'name' => $this->faker->name,
'description' => $this->faker->sentence,
'body' => '{"widgets":[]}',
'body' => '{"widgets":[]}',
];
}
}
6 changes: 3 additions & 3 deletions database/migrations/create_model-stats_table.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

use Illuminate\Support\Facades\Config;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {

return new class() extends Migration
{
public function up(): void
{
Schema::create(Config::get('model-stats.table_name'), function (Blueprint $table) {
Expand Down
58 changes: 21 additions & 37 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
verbose="true"
>
<testsuites>
<testsuite name="Jhumanj Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Jhumanj Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
18 changes: 12 additions & 6 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@

Route::prefix('api')->name('api.')->group(function () {
Route::apiResource('dashboards', \Jhumanj\LaravelModelStats\Http\Controllers\DashboardController::class);
Route::post('widgets/data',
[\Jhumanj\LaravelModelStats\Http\Controllers\StatController::class, 'widgetData']);
Route::post('widgets/custom-code/data',
[\Jhumanj\LaravelModelStats\Http\Controllers\CustomCodeController::class, 'widgetData']);
Route::post(
'widgets/data',
[\Jhumanj\LaravelModelStats\Http\Controllers\StatController::class, 'widgetData']
);
Route::post(
'widgets/custom-code/data',
[\Jhumanj\LaravelModelStats\Http\Controllers\CustomCodeController::class, 'widgetData']
);

Route::post('widgets/custom-code/execute',
[\Jhumanj\LaravelModelStats\Http\Controllers\CustomCodeController::class, 'executeCustomCode']);
Route::post(
'widgets/custom-code/execute',
[\Jhumanj\LaravelModelStats\Http\Controllers\CustomCodeController::class, 'executeCustomCode']
);
});

Route::get('/{view?}', [\Jhumanj\LaravelModelStats\Http\Controllers\HomeController::class, 'home'])
Expand Down
14 changes: 2 additions & 12 deletions src/AuthorizesRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,24 @@ trait AuthorizesRequests
{
/**
* The callback that should be used to authenticate ModelStats users.
*
* @var \Closure
*/
public static Closure $authUsing;

/**
* Register the ModelStats authentication callback.
*
* @param \Closure $callback
*
* @return static
*/
public static function auth(Closure $callback): static
{
static::$authUsing = $callback;

return new static;
return new static();
}

/**
* Determine if the given request can access the ModelStats dashboard.
*
* @param \Illuminate\Http\Request $request
*
* @return bool
*/
public static function check(Request $request): bool
{
return (static::$authUsing ?: fn () => app()->environment('local'))($request);
return (static::$authUsing)($request);
}
}
3 changes: 0 additions & 3 deletions src/Console/InstallModelStatsPackage.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace Jhumanj\LaravelModelStats\Console;

use Illuminate\Console\Command;
Expand Down Expand Up @@ -32,8 +31,6 @@ public function handle(): void

/**
* Register the ModelStats service provider in the application configuration file.
*
* @return void
*/
private function registerModelStatsServiceProvider(): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function destroy(Dashboard $dashboard): JsonResponse
$dashboard->delete();

return $this->success([
'message' => 'Dashboard deleted',
'message' => 'Dashboard deleted',
]);
}
}
8 changes: 3 additions & 5 deletions src/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

namespace Jhumanj\LaravelModelStats\Http\Controllers;

use Illuminate\Container\Container;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use ReflectionClass;
use Schema;

class HomeController extends Controller
{
Expand Down Expand Up @@ -41,7 +40,7 @@ private function getModels(): Collection

return sprintf(
'\%s%s',
Container::getInstance()->getNamespace(),
app()->getNamespace(),
strtr(substr($path, 0, strrpos($path, '.')), '/', '\\')
);
})
Expand All @@ -57,7 +56,6 @@ private function getModels(): Collection
return $valid;
});


return $models->map(fn (string $class) => [
'class' => $class,
'fields' => $this->getClassFields($class),
Expand All @@ -66,6 +64,6 @@ private function getModels(): Collection

private function getClassFields(string $class)
{
return Schema::getColumnListing((new $class)->getTable());
return Schema::getColumnListing((new $class())->getTable());
}
}
5 changes: 2 additions & 3 deletions src/Http/Middleware/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class Authorize
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return \Illuminate\Http\Response
*/
public function handle($request, $next)
Expand Down
8 changes: 3 additions & 5 deletions src/Http/Middleware/CustomCodeEnabled.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php


namespace Jhumanj\LaravelModelStats\Http\Middleware;

class CustomCodeEnabled
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return \Illuminate\Http\Response
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
*/
public function handle($request, $next)
{
Expand Down
2 changes: 0 additions & 2 deletions src/Http/Requests/Dashboard/StoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class StoreRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
Expand Down
2 changes: 0 additions & 2 deletions src/Http/Requests/Dashboard/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class UpdateRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
Expand Down
12 changes: 6 additions & 6 deletions src/Http/Requests/Widgets/DataRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Jhumanj\LaravelModelStats\Http\Requests\Widgets;

use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -32,8 +31,6 @@ class DataRequest extends FormRequest

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
Expand All @@ -53,15 +50,18 @@ private function getModels(): Collection
->map(function ($item) {
$path = $item->getRelativePathName();

return sprintf('\%s%s', Container::getInstance()
->getNamespace(), strtr(substr($path, 0, strrpos($path, '.')), '/', '\\'));
return sprintf(
'\%s%s',
app()->getNamespace(),
strtr(substr($path, 0, strrpos($path, '.')), '/', '\\')
);
})->filter(function ($class) {
$valid = false;

if (class_exists($class)) {
$reflection = new \ReflectionClass($class);
$valid = $reflection->isSubclassOf(Model::class)
&& ! $reflection->isAbstract();
&& ! $reflection->isAbstract();
}

return $valid;
Expand Down
Loading

0 comments on commit 88a47ed

Please sign in to comment.