Browse Source

Modular analyzers

pull/3/head
Richard Dern 3 months ago
parent
commit
5567cd8064
  1. 2
      config/analyzer.php
  2. 17
      package/Analyzer.php
  3. 60
      package/Analyzers/BaseAnalyzer.php
  4. 14
      package/Analyzers/HtmlAnalyzer.php
  5. 29
      package/Contracts/Analyzer.php

2
config/analyzer.php

@ -34,7 +34,7 @@ return [
'html' => [
'groups' => [],
'analyzers' => [
\Cyca\Analyzer\Analyzers\HtmlAnalyzer::class
\Cyca\Analyzer\Html\HtmlAnalyzer::class
]
]

17
package/Analyzer.php

@ -47,7 +47,8 @@ class Analyzer extends GenericObject
'mimeType',
'fileSize',
'lastModified',
'fetcher'
'fetcher',
'analyzers'
];
/**
@ -62,7 +63,8 @@ class Analyzer extends GenericObject
'mimeType',
'fileSize',
'lastModified',
'fetcher'
'fetcher',
'analyzers'
];
/**
@ -228,9 +230,20 @@ class Analyzer extends GenericObject
$this->storeBasicMetaData();
$analyzers = $this->getAnalyzers();
$attributes = $this->toArray();
foreach ($analyzers as $className) {
$instance = new $className;
$instance
->setFilesystem($this->fileSystem)
->setDiscoveredAttributes($attributes)
->analyze();
$attributes['analyzers'][$className] = $instance->toArray();
}
$this->setAttribute('analyzers', $attributes['analyzers']);
}
/**

60
package/Analyzers/BaseAnalyzer.php

@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace Cyca\Analyzer\Analyzers;
use Cyca\Analyzer\Contracts\Analyzer;
use Cyca\Helpers\Helpers\GenericObject;
use Illuminate\Contracts\Filesystem\Filesystem;
/**
* Analyzes HTML files
*/
abstract class BaseAnalyzer extends GenericObject implements Analyzer
{
/**
* Attributes already discovered.
*
* @var array
*/
protected array $baseAttributes = [];
/**
* Filesystem
*
* @var Filesystem|null
*/
protected ?Filesystem $fileSystem = null;
/**
* Pass every discovered attribute, including from other analyzers, to this
* instance.
*
* Chainable method.
*
* @param array $attributes
* @return self
*/
public function setDiscoveredAttributes(array $attributes = []): self
{
$this->baseAttributes = $attributes;
return $this;
}
/**
* Set Filesystem.
*
* Chainable method.
*
* @param Filesystem $store
* @return void
*/
public function setFilesystem(Filesystem $store): self
{
$this->fileSystem = $store;
return $this;
}
}

14
package/Analyzers/HtmlAnalyzer.php

@ -1,14 +0,0 @@
<?php
declare(strict_types=1);
namespace Cyca\Analyzer\Analyzers;
use Cyca\Helpers\Helpers\GenericObject;
/**
* Analyzes HTML files
*/
class HtmlAnalyzer extends GenericObject
{
}

29
package/Contracts/Analyzer.php

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Cyca\Analyzer\Contracts;
/**
* Every anaylzer must implement this interface
*/
interface Analyzer
{
/**
* Pass every discovered attribute, including from other analyzers, to this
* instance.
*
* Chainable method.
*
* @param array $attributes
* @return self
*/
public function setDiscoveredAttributes(array $attributes = []): self;
/**
* Launch document's analyzis.
*
* @return void
*/
public function analyze(): void;
}
Loading…
Cancel
Save