-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[11.x] Add contextual attributes to resolve drivers (#52265)
* storage attribute * test storage attribute * test config attribute * correct return type * cache, db, log attrs * guard attr * authed attr * fix cs * fix tests * rename files * add db --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
4d7835b
commit b8a6d7c
Showing
9 changed files
with
384 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Illuminate\Container\Attributes; | ||
|
||
use Attribute; | ||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Contracts\Container\ContextualAttribute; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER)] | ||
class Auth implements ContextualAttribute | ||
{ | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
public function __construct(public ?string $guard = null) | ||
{ | ||
} | ||
|
||
/** | ||
* Resolve the authentication guard. | ||
* | ||
* @param self $attribute | ||
* @param \Illuminate\Contracts\Container\Container $container | ||
* @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard | ||
*/ | ||
public static function resolve(self $attribute, Container $container) | ||
{ | ||
return $container->make('auth')->guard($attribute->guard); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Illuminate\Container\Attributes; | ||
|
||
use Attribute; | ||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Contracts\Container\ContextualAttribute; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER)] | ||
class Authenticated implements ContextualAttribute | ||
{ | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
public function __construct(public ?string $guard = null) | ||
{ | ||
} | ||
|
||
/** | ||
* Resolve the currently authenticated user. | ||
* | ||
* @param self $attribute | ||
* @param \Illuminate\Contracts\Container\Container $container | ||
* @return \Illuminate\Contracts\Auth\Authenticatable|null | ||
*/ | ||
public static function resolve(self $attribute, Container $container) | ||
{ | ||
return $container->make('auth')->guard($attribute->guard)->user(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Illuminate\Container\Attributes; | ||
|
||
use Attribute; | ||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Contracts\Container\ContextualAttribute; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER)] | ||
class Cache implements ContextualAttribute | ||
{ | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
public function __construct(public ?string $store = null) | ||
{ | ||
} | ||
|
||
/** | ||
* Resolve the cache store. | ||
* | ||
* @param self $attribute | ||
* @param \Illuminate\Contracts\Container\Container $container | ||
* @return \Illuminate\Contracts\Cache\Repository | ||
*/ | ||
public static function resolve(self $attribute, Container $container) | ||
{ | ||
return $container->make('cache')->store($attribute->store); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Illuminate\Container\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER)] | ||
class CurrentUser extends Authenticated | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Illuminate\Container\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER)] | ||
class DB extends Database | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Illuminate\Container\Attributes; | ||
|
||
use Attribute; | ||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Contracts\Container\ContextualAttribute; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER)] | ||
class Database implements ContextualAttribute | ||
{ | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
public function __construct(public ?string $connection = null) | ||
{ | ||
} | ||
|
||
/** | ||
* Resolve the database connection. | ||
* | ||
* @param self $attribute | ||
* @param \Illuminate\Contracts\Container\Container $container | ||
* @return \Illuminate\Database\Connection | ||
*/ | ||
public static function resolve(self $attribute, Container $container) | ||
{ | ||
return $container->make('db')->connection($attribute->connection); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Illuminate\Container\Attributes; | ||
|
||
use Attribute; | ||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Contracts\Container\ContextualAttribute; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER)] | ||
class Log implements ContextualAttribute | ||
{ | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
public function __construct(public ?string $channel = null) | ||
{ | ||
} | ||
|
||
/** | ||
* Resolve the log channel. | ||
* | ||
* @param self $attribute | ||
* @param \Illuminate\Contracts\Container\Container $container | ||
* @return \Psr\Log\LoggerInterface | ||
*/ | ||
public static function resolve(self $attribute, Container $container) | ||
{ | ||
return $container->make('log')->channel($attribute->channel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Illuminate\Container\Attributes; | ||
|
||
use Attribute; | ||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Contracts\Container\ContextualAttribute; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER)] | ||
class Storage implements ContextualAttribute | ||
{ | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
public function __construct(public ?string $disk = null) | ||
{ | ||
} | ||
|
||
/** | ||
* Resolve the storage disk. | ||
* | ||
* @param self $attribute | ||
* @param \Illuminate\Contracts\Container\Container $container | ||
* @return \Illuminate\Contracts\Filesystem\Filesystem | ||
*/ | ||
public static function resolve(self $attribute, Container $container) | ||
{ | ||
return $container->make('filesystem')->disk($attribute->disk); | ||
} | ||
} |
Oops, something went wrong.