Skip to content

Commit d80489f

Browse files
committed
Fix app level service provider registration issue, improve event listeners
1 parent 2ddce60 commit d80489f

12 files changed

Lines changed: 130 additions & 32 deletions

File tree

‎example/app/Events/SampleEvent.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Example\App\Events;
4+
5+
use Framework\Concerns\Dispatchable;
6+
7+
class SampleEvent
8+
{
9+
use Dispatchable;
10+
11+
/**
12+
* The blog.
13+
*
14+
* @var \Example\App\Models\Blog
15+
*
16+
* @since 1.0.0
17+
*/
18+
public $blog;
19+
20+
/**
21+
* Create a new sample event instance.
22+
*
23+
* @param \Example\App\Models\Blog $blog The blog.
24+
*
25+
* @return void
26+
*
27+
* @since 1.0.0
28+
*/
29+
public function __construct($blog)
30+
{
31+
$this->blog = $blog;
32+
}
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Example\App\Listeners;
4+
5+
use Example\App\Events\SampleEvent;
6+
use Framework\Listener;
7+
8+
class AnotherListener extends Listener
9+
{
10+
public function handle(SampleEvent $event)
11+
{
12+
file_put_contents(__DIR__ . '/test.log', 'from-another-listener: ' . $event->blog->id . PHP_EOL, FILE_APPEND);
13+
}
14+
15+
public function priority()
16+
{
17+
return 1;
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Example\App\Listeners;
4+
5+
use Example\App\Events\SampleEvent;
6+
use Framework\Listener;
7+
8+
use function Framework\dd;
9+
10+
class SampleListener extends Listener
11+
{
12+
public function handle(SampleEvent $event)
13+
{
14+
file_put_contents(__DIR__ . '/test.log', 'from-sample-listener: ' . $event->blog->id . PHP_EOL, FILE_APPEND);
15+
}
16+
17+
public function priority()
18+
{
19+
return 2;
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Example\App\Providers;
4+
5+
use Framework\ServiceProvider;
6+
7+
use function Framework\config;
8+
9+
class TestServiceProvider extends ServiceProvider
10+
{
11+
public function register()
12+
{
13+
$config = config('app.name');
14+
}
15+
}

‎example/bootstrap/providers.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

3+
use Example\App\Providers\TestServiceProvider;
34
use Example\Providers\AppServiceProvider;
45

56
return [
67
AppServiceProvider::class,
8+
TestServiceProvider::class,
79
];

‎example/config/listeners.cache.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@
33
/** Auto generated by discovering the listeners. Don't edit this file manually. */
44

55
return array (
6+
'Example\\App\\Events\\SampleEvent' =>
7+
array (
8+
0 => 'Example\\App\\Listeners\\SampleListener',
9+
1 => 'Example\\App\\Listeners\\AnotherListener',
10+
),
611
);

‎example/routes/api.php‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,23 @@
44
use Example\App\Http\Controllers\EventsController;
55
use Example\App\Http\Controllers\SpeakersController;
66
use Example\App\Http\Requests\ExampleRequest;
7+
use Example\App\Models\Blog;
78
use Example\App\Models\Event;
89
use Framework\Http\Request;
910
use Framework\Middlewares\AdminMiddleware;
1011
use Framework\Middlewares\AuthMiddleware;
1112
use Framework\Route;
1213
use Framework\Supports\Arr;
13-
use Framework\Supports\Facades\DB;
14-
use Framework\Supports\Facades\File;
15-
use Framework\Supports\Facades\Option;
1614

17-
use function Framework\app;
18-
use function Framework\collection;
19-
use function Framework\config;
20-
use function Framework\dd;
21-
use function Framework\dump;
22-
use function Framework\request;
2315
use function Framework\response;
2416

2517
Route::set_namespace('framework/v1');
2618

2719
Route::get('/ping', function (Request $request) {
20+
$blog = Blog::with(['comments' => fn ($query) => $query->select('id', 'blog_id', 'body')])->find(1);
21+
2822
return response()->json([
29-
'status' => 'ok',
30-
'dev_mode' => app()->is_dev_mode(),
31-
'prefix' => app()->prefix(),
23+
'data' => $blog,
3224
]);
3325
});
3426

‎src/Application.php‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class Application extends Container
111111
/**
112112
* Service providers
113113
*
114-
* @var array<string,
114+
* @var array<string, ServiceProvider>
115115
*
116116
* @since 1.0.0
117117
*/
@@ -156,7 +156,7 @@ class Application extends Container
156156
/**
157157
* Cached namespaces resolved from composer PSR-4 paths.
158158
*
159-
* @var array<string,
159+
* @var array<string, string>
160160
*
161161
* @since 1.0.0
162162
*/
@@ -199,8 +199,8 @@ public function __construct(?string $base_path = null)
199199
$this->register_base_service_providers();
200200
$this->register_base_aliases();
201201

202-
$this->register_app_defined_providers();
203-
$this->register_app_defined_aliases();
202+
// $this->register_app_defined_providers();
203+
// $this->register_app_defined_aliases();
204204
}
205205

206206
/**
@@ -437,7 +437,12 @@ public function is_booted()
437437
*/
438438
public static function configure(string $base_path)
439439
{
440-
return static::get_instance($base_path);
440+
$instance = static::get_instance($base_path);
441+
442+
$instance->register_app_defined_providers()
443+
->register_app_defined_aliases();
444+
445+
return $instance;
441446
}
442447

443448
/**
@@ -801,7 +806,7 @@ protected function fire_app_callbacks(array $callbacks)
801806
/**
802807
* Register other on demand provided providers.
803808
*
804-
* @return void
809+
* @return $this
805810
*
806811
* @throws \InvalidArgumentException
807812
*
@@ -812,13 +817,13 @@ protected function register_app_defined_providers()
812817
$path = $this->bootstrap_service_provider_path();
813818

814819
if (!file_exists($path)) {
815-
return;
820+
return $this;
816821
}
817822

818823
$providers = require $path;
819824

820825
if (empty($providers)) {
821-
return;
826+
return $this;
822827
}
823828

824829
foreach ($providers as $provider) {
@@ -834,13 +839,15 @@ protected function register_app_defined_providers()
834839

835840
$this->register(new $provider($this));
836841
}
842+
843+
return $this;
837844
}
838845

839846
/**
840847
* Register tha aliases defined at the application level.
841848
* Generally it will come from the bootstrap/aliases.php file.
842849
*
843-
* @return void
850+
* @return $this
844851
*
845852
* @since 1.0.0
846853
*/
@@ -849,20 +856,22 @@ protected function register_app_defined_aliases()
849856
$aliases_path = $this->bootstrap_path('aliases.php');
850857

851858
if (!file_exists($aliases_path)) {
852-
return;
859+
return $this;
853860
}
854861

855862
$aliases = require $aliases_path;
856863

857864
if (empty($aliases)) {
858-
return;
865+
return $this;
859866
}
860867

861868
foreach ($aliases as $alias => $abstracts) {
862869
foreach ($abstracts as $abstract) {
863870
$this->alias($alias, $abstract);
864871
}
865872
}
873+
874+
return $this;
866875
}
867876

868877
/**

‎src/Collections/Collection.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,14 @@ public function get_arrayable_items($items)
259259
* access a single model or value from a previously constrained result set
260260
* without performing additional checks.
261261
*
262-
* @param (callable(TValue, TKey): bool)|null $callback The callback to use to determine the first item
262+
* @param (callable(TValue, TKey): bool)|null $callback The to use to determine the first item
263263
* @param TFirstDefault|(\Closure(): TFirstDefault) $default The default value to return if no items exist
264264
*
265265
* @return TValue|TFirstDefault The first item or null when no items exist
266266
*
267267
* @since 1.0.0
268268
*/
269-
public function first($callback = null, $default = null)
269+
public function first(?callable $callback = null, $default = null)
270270
{
271271
return Arr::first($this->items, $callback, $default);
272272
}

‎src/Database/Query/QueryBuilder.php‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,7 +2324,9 @@ public function oldest($column = 'created_at')
23242324
*/
23252325
public function limit($limit)
23262326
{
2327-
$this->limit = $limit;
2327+
if ($limit >= 0) {
2328+
$this->limit = $limit;
2329+
}
23282330

23292331
return $this;
23302332
}
@@ -2788,7 +2790,7 @@ protected function run_select()
27882790
*/
27892791
public function first($columns = ['*'])
27902792
{
2791-
return $this->take(1)->get($columns)->first();
2793+
return $this->limit(1)->get($columns)->first();
27922794
}
27932795

27942796
/**
@@ -3556,14 +3558,14 @@ public function decrement_each(array $columns, array $extra = [])
35563558
*
35573559
* @return mixed The sole record.
35583560
*
3559-
* @throws \RecordNotFoundException
3560-
* @throws \MultipleRecordsFoundException
3561+
* @throws RecordNotFoundException
3562+
* @throws MultipleRecordsFoundException
35613563
*
35623564
* @since 1.0.0
35633565
*/
35643566
public function sole($columns = ['*'])
35653567
{
3566-
$result = $this->take(2)->get($columns);
3568+
$result = $this->limit(2)->get($columns);
35673569

35683570
$count = $result->count();
35693571

0 commit comments

Comments
 (0)