Skip to content

Commit

Permalink
Merge pull request #11 from tattersoftware/helper
Browse files Browse the repository at this point in the history
Add service and helper
  • Loading branch information
MGatner authored Sep 20, 2020
2 parents c509ca7 + 1bc0c1a commit 3a10894
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
7 changes: 3 additions & 4 deletions phpstan.neon → phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ parameters:
paths:
- src
- tests
ignoreErrors:
- '#Constant SUPPORTPATH not found#'
bootstrapFiles:
- phpstan-bootstrap.php
- vendor/codeigniter4/codeigniter4/system/Test/bootstrap.php
scanDirectories:
- vendor/codeigniter4/codeigniter4/system/Helpers
dynamicConstantNames:
- ENVIRONMENT
- CI_DEBUG
treatPhpDocTypesAsCertain: false
ignoreErrors:
- '#Call to an undefined static method Config\\Services::handlers\(\)#'
25 changes: 25 additions & 0 deletions src/Config/Services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php namespace Tatter\Handlers\Config;

use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\Config\BaseService;
use Tatter\Handlers\Handlers;
use Tatter\Handlers\Config\Handlers as HandlersConfig;

class Services extends BaseService
{
/**
* @param string $path
* @param HandlersConfig|null $config
* @param CacheInterface|null $cache
* @param boolean $getShared
*/
public static function handlers(string $path = '', HandlersConfig $config = null, CacheInterface $cache = null, bool $getShared = true)
{
if ($getShared)
{
return static::getSharedInstance('handlers', $path, $config, $cache);
}

return new Handlers($path, $config, $cache);
}
}
19 changes: 19 additions & 0 deletions src/Helpers/handlers_helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Config\Services;
use Tatter\Handlers\Handlers;

if (! function_exists('handlers'))
{
/**
* Returns the Handlers service set to the specified path.
*
* @param string $path
*
* @return Handlers
*/
function handlers(string $path = ''): Handlers
{
return $path ? Services::handlers()->setPath($path) : Services::handlers();
}
}
31 changes: 31 additions & 0 deletions tests/unit/HelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Tatter\Handlers\Handlers;
use Tests\Support\HandlerTestCase;

class HelperTest extends HandlerTestCase
{
protected function setUp(): void
{
parent::setUp();

helper('handlers');
}

public function testHelperReturnsLibrary()
{
$result = handlers();

$this->assertInstanceOf(Handlers::class, $result);
}

public function testHelperRetainsPath()
{
$path = 'Marmalade';
handlers($path);

$result = handlers()->getPath();

$this->assertEquals($path, $result);
}
}

0 comments on commit 3a10894

Please sign in to comment.