Skip to content

Commit

Permalink
Merge pull request #12 from tattersoftware/get-attributes
Browse files Browse the repository at this point in the history
Add getAttributes
  • Loading branch information
MGatner authored Sep 28, 2020
2 parents 3a10894 + 2ff73a2 commit 50c75ec
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 70 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
},
"require-dev": {
"codeigniter4/codeigniter4": "dev-develop",
"phpunit/phpunit": "^8.5",
"fzaninotto/faker": "^1.9@dev",
"phpstan/phpstan": "^0.12"
"phpunit/phpunit": "^8.5",
"phpstan/phpstan": "^0.12",
"squizlabs/php_codesniffer": "^3.5",
"codeigniter4/codeigniter4-standard": "^1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -49,6 +51,7 @@
},
"scripts": {
"analyze": "phpstan analyze",
"style": "phpcbf --standard=./vendor/codeigniter4/codeigniter4-standard/CodeIgniter4 src/ tests/",
"test": "phpunit",
"post-update-cmd": [
"composer dump-autoload"
Expand Down
12 changes: 6 additions & 6 deletions src/Commands/HandlersList.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class HandlersList extends BaseCommand
{
protected $group = 'Housekeeping';
protected $name = 'handlers:list';
protected $description = 'List all discovered handlers';
protected $group = 'Housekeeping';
protected $name = 'handlers:list';
protected $description = 'List all discovered handlers';
protected $usage = 'handlers:list';

public function run(array $params = [])
{
// Load the library
{
// Load the library
$handlers = new Handlers();

// Make sure auto-discovery is enabled
Expand All @@ -34,7 +34,7 @@ public function run(array $params = [])
CLI::write('No handlers detected.', 'yellow');
continue;
}

// Display each class
foreach ($classes as $class)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Commands/HandlersRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class HandlersRegister extends BaseCommand
{
protected $group = 'Housekeeping';
protected $name = 'handlers:register';
protected $description = 'Regsiter all discovered handlers';
protected $group = 'Housekeeping';
protected $name = 'handlers:register';
protected $description = 'Regsiter all discovered handlers';
protected $usage = 'handlers:register';

public function run(array $params = [])
{
// Load the library
{
// Load the library
$handlers = new Handlers();

// Make sure auto-discovery is enabled
Expand Down
10 changes: 5 additions & 5 deletions src/Commands/HandlersReset.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class HandlersReset extends BaseCommand
{
protected $group = 'Housekeeping';
protected $name = 'handlers:reset';
protected $description = 'Clear cached versions of discovered handlers';
protected $group = 'Housekeeping';
protected $name = 'handlers:reset';
protected $description = 'Clear cached versions of discovered handlers';
protected $usage = 'handlers:reset';

public function run(array $params = [])
{
// Load the library
{
// Load the library
$handlers = new Handlers();

// Make sure auto-discovery is enabled
Expand Down
32 changes: 16 additions & 16 deletions src/Config/Handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@

class Handlers extends BaseConfig
{
/**
* Classes to ignore across all handlers.
*
* @var array<string>
*/
/**
* Classes to ignore across all handlers.
*
* @var array<string>
*/
public $ignoredClasses = [];

/**
* Paths to check during automatic discovery.
*
* @var array<string>
*/
/**
* Paths to check during automatic discovery.
*
* @var array<string>
*/
public $autoDiscover = [];

/**
* Number of seconds to cache discovered handlers.
* Null disables caching
*
* @var int|null
*/
/**
* Number of seconds to cache discovered handlers.
* Null disables caching
*
* @var integer|null
*/
public $cacheDuration = DAY;
}
8 changes: 4 additions & 4 deletions src/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
class Services extends BaseService
{
/**
* @param string $path
* @param string $path
* @param HandlersConfig|null $config
* @param CacheInterface|null $cache
* @param boolean $getShared
* @param boolean $getShared
*/
public static function handlers(string $path = '', HandlersConfig $config = null, CacheInterface $cache = null, bool $getShared = true)
{
public static function handlers(string $path = '', HandlersConfig $config = null, CacheInterface $cache = null, bool $getShared = true)
{
if ($getShared)
{
return static::getSharedInstance('handlers', $path, $config, $cache);
Expand Down
50 changes: 35 additions & 15 deletions src/Handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Handlers
/**
* Initializes the library.
*
* @param string $path
* @param string $path
* @param HandlersConfig|null $config
* @param CacheInterface|null $cache
*/
Expand Down Expand Up @@ -94,6 +94,20 @@ public function setPath(string $path): self
return $this;
}

/**
* Returns the attributes for a discovered class.
*
* @param string $class
*
* @return array|null
*/
public function getAttributes(string $class): ?array
{
$this->discoverHandlers();

return $this->discovered[$class] ?? null;
}

//--------------------------------------------------------------------

/**
Expand Down Expand Up @@ -167,7 +181,7 @@ public function findAll(): array
* Returns a handler with a given name. Ignores filters.
* Searches: attribute "name" or "uid", namespaced class, and short class name.
*
* @param string $name The name of the handler
* @param string $name The name of the handler
*
* @return string|null The full class name, or null if none found
*/
Expand Down Expand Up @@ -213,7 +227,7 @@ public function find(string $name): ?string
/**
* Returns an array of all matched classes.
*
* @return array<string>
* @return array<string>
* @deprecated Use findAll()
*/
public function all(): array
Expand All @@ -225,9 +239,9 @@ public function all(): array
* Returns a handler with a given name. Ignores filters.
* Searches: attribute "name" or "uid", namespaced class, and short class name.
*
* @param string $name The name of the handler
* @param string $name The name of the handler
*
* @return string|null The full class name, or null if none found
* @return string|null The full class name, or null if none found
* @deprecated Use find()
*/
public function named(string $name): ?string
Expand All @@ -240,8 +254,8 @@ public function named(string $name): ?string
/**
* Parses "where" $criteria and adds to $filters
*
* @param array $criteria Array of 'key [operator]' => 'value'
* @param bool $combine Whether the resulting filter should be combined with others
* @param array $criteria Array of 'key [operator]' => 'value'
* @param boolean $combine Whether the resulting filter should be combined with others
*/
protected function parseCriteria(array $criteria, bool $combine): void
{
Expand All @@ -258,7 +272,12 @@ protected function parseCriteria(array $criteria, bool $combine): void
$operator = '==';
}

$this->filters[] = [$key, $operator, $value, $combine];
$this->filters[] = [
$key,
$operator,
$value,
$combine,
];
}
}

Expand Down Expand Up @@ -299,7 +318,7 @@ public function register(): array
/**
* Filters discovered classes by the defined criteria.
*
* @param int|null $limit Limit on how many classes to match
* @param integer|null $limit Limit on how many classes to match
*
* @return array<string>
* @throws \RuntimeException
Expand Down Expand Up @@ -345,11 +364,11 @@ protected function filterHandlers(int $limit = null): array
break;

case '>':
$test = $attributes[$key] > $value;
$test = $attributes[$key] > $value;
break;

case '>=':
$test = $attributes[$key] >= $value;
$test = $attributes[$key] >= $value;
break;

case '<':
Expand Down Expand Up @@ -407,7 +426,8 @@ protected function discoverHandlers(): self
}

// Check the cache first
$this->cacheRestore();
$this->cacheRestore();

if ($this->discovered !== null)
{
return $this;
Expand Down Expand Up @@ -436,7 +456,7 @@ protected function discoverHandlers(): self
{
continue;
}

// A match! Get the instance attributes
$attributes = (new $class())->toArray();

Expand All @@ -456,8 +476,8 @@ protected function discoverHandlers(): self
* Validates that a file path contains a HandlerInterface and
* returns its full class name.
*
* @param string $file Full path to the file in question
* @param string $namespace The file's namespace
* @param string $file Full path to the file in question
* @param string $namespace The file's namespace
*
* @return string|null The fully-namespaced class
*/
Expand Down
1 change: 1 addition & 0 deletions src/Interfaces/HandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Note:
* This interface will always be compatible with CodeIgniter\Entity.
*/

interface HandlerInterface
{
/**
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/HandlerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function setUp(): void
{
parent::setUp();

$this->config = new HandlersConfig();
$this->config = new HandlersConfig();
$this->config->cacheDuration = MINUTE;

$this->handlers = new Handlers('Factories', $this->config);
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public function testDiscoveryUsesCache()
$class = 'Foo\Bar\Baz';

cache()->save('handlers-factories', [
$class => ['name' => 'foobar']
]);
$class => ['name' => 'foobar'],
]);

$result = $this->handlers->first();

Expand All @@ -23,10 +23,10 @@ public function testDiscoveryIgnoresCache()
$expected = 'Tests\Support\Factories\PopFactory';

$this->config->cacheDuration = null;
$handlers = new Handlers('Factories', $this->config);
$handlers = new Handlers('Factories', $this->config);

cache()->save('handlers-factories', [
'Foo\Bar\Baz' => ['name' => 'foobar']
'Foo\Bar\Baz' => ['name' => 'foobar'],
]);

$result = $this->handlers->first();
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function testHelperReturnsLibrary()

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

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

$this->assertEquals($path, $result);
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/LibraryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Tatter\Handlers\Handlers;
use Tatter\Handlers\Config\Handlers as HandlersConfig;
use Tests\Support\HandlerTestCase;
use Tests\Support\Factories\WidgetFactory;

class LibraryTest extends HandlerTestCase
{
Expand Down Expand Up @@ -90,4 +91,19 @@ public function testRegisterCallsHandlerRegister()

$this->assertEquals(true, session('didRegister'));
}

public function testGetAttributesReturnsAttributes()
{
$result = $this->handlers->getAttributes(WidgetFactory::class);

$this->assertIsArray($result);
$this->assertEquals('Widget Plant', $result['name']);
}

public function testGetAttributesReturnsNull()
{
$result = $this->handlers->getAttributes('Imaginary\Handler\Class');

$this->assertNull($result);
}
}
Loading

0 comments on commit 50c75ec

Please sign in to comment.