Skip to content

Commit

Permalink
feat: add static helpers for WpService and AcfService
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbrink committed Oct 4, 2024
1 parent 6caf4be commit 5613124
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
6 changes: 6 additions & 0 deletions library/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
$wpService = new NativeWpService();
$acfService = new NativeAcfService();

/**
* Service helpers.
*/
\Municipio\Helper\AcfService::set($acfService);
\Municipio\Helper\WpService::set($wpService);

/**
* Acf auto import and export
*/
Expand Down
44 changes: 44 additions & 0 deletions library/Helper/AcfService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Municipio\Helper;

use AcfService\AcfService as OriginalAcfService;

/**
* Class AcfService
*
* Static class to hold the AcfService instance.
* Use this class when you are unable to pass the AcfService instance as a parameter.
*/
class AcfService
{
private static ?OriginalAcfService $acfService = null;

/**
* Set the AcfService instance.
*
* @param OriginalAcfService $acfService
*/
public static function set(OriginalAcfService $acfService): void
{
if (self::$acfService === null) {
// Allow setting once to prevent accidental overwriting.
self::$acfService = $acfService;
}
}

/**
* Get the AcfService instance.
*
* @return OriginalAcfService
* @throws \Exception
*/
public static function get(): OriginalAcfService
{
if (self::$acfService === null) {
throw new \Exception('AcfService not set');
}

return self::$acfService;
}
}
43 changes: 43 additions & 0 deletions library/Helper/WpService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Municipio\Helper;

use WpService\WpService as OriginalWpService;

/**
* Class WpService
*
* Static class to hold the WpService instance.
* Use this class when you are unable to pass the WpService instance as a parameter.
*/
class WpService
{
private static ?OriginalWpService $wpService = null;

/**
* Set the WpService instance.
*
* @param OriginalWpService $wpService
*/
public static function set(OriginalWpService $wpService): void
{
if (self::$wpService === null) {
// Allow setting once to prevent accidental overwriting.
self::$wpService = $wpService;
}
}

/**
* Get the WpService instance.
*
* @return OriginalWpService|null
*/
public static function get(): ?OriginalWpService
{
if (self::$wpService === null) {
throw new \Exception('WpService not set');
}

return self::$wpService;
}
}

0 comments on commit 5613124

Please sign in to comment.