Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 10 additions & 31 deletions src/Address/Addresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use CraftCms\Cms\ProjectConfig\ProjectConfig;
use CraftCms\Cms\ProjectConfig\ProjectConfigHelper;
use Illuminate\Container\Attributes\Singleton;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;

use function CraftCms\Cms\t;
Expand Down Expand Up @@ -74,13 +73,9 @@ public function getAddressFormatRepository(): AddressFormatRepository
*/
public function defineAddressSubdivisions(array $parents, array $options = []): array
{
if (Event::hasListeners(DefineAddressSubdivisions::class)) {
Event::dispatch($event = new DefineAddressSubdivisions($parents, $options));
event($event = new DefineAddressSubdivisions($parents, $options));

return $event->subdivisions;
}

return $options;
return $event->subdivisions;
}

/**
Expand All @@ -91,13 +86,9 @@ public function getCountryList(?string $locale = null): array
$locale ??= app()->getLocale();
$countries = $this->countryRepository->getList($locale);

if (Event::hasListeners(DefineAddressCountries::class)) {
Event::dispatch($event = new DefineAddressCountries($locale, $countries));
event($event = new DefineAddressCountries($locale, $countries));

return $event->countries;
}

return $countries;
return $event->countries;
}

/**
Expand All @@ -111,13 +102,9 @@ public function getUsedFields(string $countryCode): array
{
$fields = $this->addressFormatRepository->get($countryCode)->getUsedFields();

if (Event::hasListeners(DefineAddressUsedFields::class)) {
Event::dispatch($event = new DefineAddressUsedFields($countryCode, $fields));

return $event->fields;
}
event($event = new DefineAddressUsedFields($countryCode, $fields));

return $fields;
return $event->fields;
}

/**
Expand All @@ -131,13 +118,9 @@ public function getUsedSubdivisionFields(string $countryCode): array
{
$fields = $this->addressFormatRepository->get($countryCode)->getUsedSubdivisionFields();

if (Event::hasListeners(DefineAddressUsedSubdivisionFields::class)) {
Event::dispatch($event = new DefineAddressUsedSubdivisionFields($countryCode, $fields));

return $event->fields;
}
event($event = new DefineAddressUsedSubdivisionFields($countryCode, $fields));

return $fields;
return $event->fields;
}

/**
Expand All @@ -164,13 +147,9 @@ public function getFieldLabel(string $field, string $countryCode): string
AddressField::FAMILY_NAME => t('Last Name'),
};

if (Event::hasListeners(DefineAddressFieldLabel::class)) {
Event::dispatch($event = new DefineAddressFieldLabel($countryCode, $field, $label));

return $event->label;
}
event($event = new DefineAddressFieldLabel($countryCode, $field, $label));

return $label;
return $event->label;
}

/**
Expand Down
43 changes: 16 additions & 27 deletions src/Auth/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Session;
use InvalidArgumentException;
use RuntimeException;
Expand Down Expand Up @@ -80,11 +79,9 @@ public function getAllMethods(?User $user = null): Collection
RecoveryCodes::class,
]);

if (Event::hasListeners(RegisterAuthMethods::class)) {
Event::dispatch(new RegisterAuthMethods($methods));
}
event($event = new RegisterAuthMethods($methods));

$this->methods[$user->id] = $methods->map(function (string $class) use ($user) {
$this->methods[$user->id] = $event->methods->map(function (string $class) use ($user) {
if (! is_subclass_of($class, AuthMethodInterface::class)) {
throw new RuntimeException("$class must implement ".AuthMethodInterface::class);
}
Expand Down Expand Up @@ -237,20 +234,16 @@ public function is2faRequired(User $user): bool

public function authenticate(User $user, #[SensitiveParameter] array $credentials): bool
{
$this->authError = null;

if (Event::hasListeners(Authenticating::class)) {
Event::dispatch($event = new Authenticating($credentials));
event($event = new Authenticating($credentials));

$this->authError = $event->authError;
$this->authError = $event->authError;

if (isset($this->authError)) {
return false;
}
if (isset($this->authError)) {
return false;
}

if (! $event->performAuthentication) {
return true;
}
if (! $event->performAuthentication) {
return true;
}

if (is_null($plain = $credentials['password'])) {
Expand Down Expand Up @@ -288,20 +281,16 @@ public function authenticate(User $user, #[SensitiveParameter] array $credential

public function authenticateWithPasskey(User $user, PublicKeyCredentialRequestOptions|array|string $requestOptions, string $response): bool
{
$this->authError = null;
event($event = new Authenticating);

if (Event::hasListeners(Authenticating::class)) {
Event::dispatch($event = new Authenticating);
$this->authError = $event->authError;

$this->authError = $event->authError;

if (isset($this->authError)) {
return false;
}
if (isset($this->authError)) {
return false;
}

if (! $event->performAuthentication) {
return true;
}
if (! $event->performAuthentication) {
return true;
}

// make sure the passkey exists and belongs to this user
Expand Down
17 changes: 4 additions & 13 deletions src/Auth/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use SensitiveParameter;

#[Scoped]
Expand Down Expand Up @@ -82,21 +81,13 @@ public function retrieveByCredentials(#[SensitiveParameter] array $credentials):

$loginName = $credentials['loginName'];

$user = null;
if (Event::hasListeners(RetrievingLoginUser::class)) {
Event::dispatch($event = new RetrievingLoginUser($loginName));
$user = $event->user;
}

$user ??= $this->users->getUserByUsernameOrEmail($loginName);
event($event = new RetrievingLoginUser($loginName));

if (Event::hasListeners(LoginUserRetrieved::class)) {
Event::dispatch($event = new LoginUserRetrieved($loginName, $user));
$user = $event->user ?? $this->users->getUserByUsernameOrEmail($loginName);

return $event->user;
}
event($event = new LoginUserRetrieved($loginName, $user));

return $user;
return $event->user;
}

/**
Expand Down
14 changes: 5 additions & 9 deletions src/Component/Concerns/ConfigurableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ public function settingsAttributes(): array
{
$attributes = array_keys(Utils::getPublicProperties($this, fn (ReflectionProperty $property) => $property->class === static::class));

if ($this->hasComponentListeners(self::EVENT_DEFINE_SETTINGS_ATTRIBUTES)) {
$this->dispatchComponentEvent(self::EVENT_DEFINE_SETTINGS_ATTRIBUTES, $event = new DefineSettingsAttributes(
component: $this,
attributes: $attributes,
));
$this->dispatchComponentEvent(self::EVENT_DEFINE_SETTINGS_ATTRIBUTES, $event = new DefineSettingsAttributes(
component: $this,
attributes: $attributes,
));

return $event->attributes;
}

return $attributes;
return $event->attributes;
}

public static function onDefineSettingsAttributes(callable $callback): void
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Concerns/HasComponentEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function componentEventName(string $event, ?string $class = null):
public function dispatchComponentEvent(string $event, mixed $payload): void
{
foreach (static::getClasses() as $class) {
Event::dispatch(self::componentEventName($event, $class), $payload);
event(self::componentEventName($event, $class), $payload);

if (property_exists($payload, 'handled') && $payload->handled) {
return;
Expand Down
30 changes: 5 additions & 25 deletions src/Component/Concerns/SavableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ public static function onBeforeSave(QueuedClosure|callable|array|string $callbac

public function beforeSave(bool $isNew): bool
{
if (! Event::hasListeners(self::componentEventName(self::EVENT_BEFORE_SAVE))) {
return true;
}

Event::dispatch(
event(
self::componentEventName(self::EVENT_BEFORE_SAVE),
$event = new ComponentEvent($this, $isNew),
);
Expand All @@ -82,11 +78,7 @@ public static function onAfterSave(QueuedClosure|callable|array|string $callback
*/
public function afterSave(bool $isNew): void
{
if (! Event::hasListeners(self::componentEventName(self::EVENT_AFTER_SAVE))) {
return;
}

Event::dispatch(self::componentEventName(self::EVENT_AFTER_SAVE), new ComponentEvent($this, $isNew));
event(self::componentEventName(self::EVENT_AFTER_SAVE), new ComponentEvent($this, $isNew));
}

public static function onBeforeDelete(QueuedClosure|callable|array|string $callback): void
Expand All @@ -99,11 +91,7 @@ public static function onBeforeDelete(QueuedClosure|callable|array|string $callb
*/
public function beforeDelete(): bool
{
if (! Event::hasListeners(self::componentEventName(self::EVENT_BEFORE_DELETE))) {
return true;
}

Event::dispatch(
event(
self::componentEventName(self::EVENT_BEFORE_DELETE),
$event = new ComponentEvent($this),
);
Expand All @@ -121,11 +109,7 @@ public static function onBeforeApplyDelete(QueuedClosure|callable|array|string $
*/
public function beforeApplyDelete(): void
{
if (! Event::hasListeners(self::componentEventName(self::EVENT_BEFORE_APPLY_DELETE))) {
return;
}

Event::dispatch(self::componentEventName(self::EVENT_BEFORE_APPLY_DELETE), new ComponentEvent($this));
event(self::componentEventName(self::EVENT_BEFORE_APPLY_DELETE), new ComponentEvent($this));
}

public static function onAfterDelete(QueuedClosure|callable|array|string $callback): void
Expand All @@ -138,10 +122,6 @@ public static function onAfterDelete(QueuedClosure|callable|array|string $callba
*/
public function afterDelete(): void
{
if (! Event::hasListeners(self::componentEventName(self::EVENT_AFTER_DELETE))) {
return;
}

Event::dispatch(self::componentEventName(self::EVENT_AFTER_DELETE), new ComponentEvent($this));
event(self::componentEventName(self::EVENT_AFTER_DELETE), new ComponentEvent($this));
}
}
38 changes: 12 additions & 26 deletions src/Dashboard/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use Throwable;
Expand All @@ -50,13 +49,9 @@ public function getAllWidgetTypes(): Collection
UpdatesWidget::class,
]);

if (Event::hasListeners(RegisterWidgetTypes::class)) {
Event::dispatch($event = new RegisterWidgetTypes($widgetTypes));
event($event = new RegisterWidgetTypes($widgetTypes));

return $event->types;
}

return $widgetTypes;
return $event->types;
}

/**
Expand Down Expand Up @@ -140,16 +135,14 @@ public function saveWidget(WidgetInterface $widget, bool $runValidation = true):
{
$isNewWidget = ! $widget->id;

if (Event::hasListeners(WidgetSaving::class)) {
Event::dispatch($event = new WidgetSaving($widget, $isNewWidget));
event($event = new WidgetSaving($widget, $isNewWidget));

if (! $event->isValid) {
return false;
}

$widget = $event->widget;
if (! $event->isValid) {
return false;
}

$widget = $event->widget;

if (! $widget->beforeSave($isNewWidget)) {
return false;
}
Expand Down Expand Up @@ -193,9 +186,7 @@ public function saveWidget(WidgetInterface $widget, bool $runValidation = true):
throw $e;
}

if (Event::hasListeners(WidgetSaved::class)) {
Event::dispatch(new WidgetSaved($widget, $isNewWidget));
}
event(new WidgetSaved($widget, $isNewWidget));

return true;
}
Expand All @@ -221,13 +212,10 @@ public function deleteWidgetById(int $widgetId): bool
*/
public function deleteWidget(WidgetInterface $widget): bool
{
if (Event::hasListeners(WidgetDeleting::class)) {
$event = new WidgetDeleting($widget);
Event::dispatch($event);
event($event = new WidgetDeleting($widget));

if (! $event->isValid) {
return false;
}
if (! $event->isValid) {
return false;
}

if (! $widget->beforeDelete()) {
Expand All @@ -247,9 +235,7 @@ public function deleteWidget(WidgetInterface $widget): bool
throw $e;
}

if (Event::hasListeners(WidgetDeleted::class)) {
Event::dispatch(new WidgetDeleted($widget));
}
event(new WidgetDeleted($widget));

return true;
}
Expand Down
Loading
Loading