Browse Source

Loading analyzers, possibly in recursive groups

pull/3/head
Richard Dern 3 months ago
parent
commit
df66401b7f
  1. 28
      config/analyzer.php
  2. 61
      package/Analyzer.php

28
config/analyzer.php

@ -11,4 +11,32 @@ return [
* Available values: anything from Cyca's _config/filesystems.php_ disks
*/
'storage_disk' => env('ANALYZER_STORAGE_DISK', 'local'),
/**
* Analyzers per mimetype.
*/
'analyzers' => [
'text/html' => [
'groups' => [
'html'
],
'analyzers' => []
]
],
/**
* Groups of analyzers.
*/
'groups' => [
'html' => [
'groups' => [],
'analyzers' => [
\Cyca\Analyzer\Analyzers\HtmlAnalyzer::class
]
]
],
];

61
package/Analyzer.php

@ -124,13 +124,15 @@ class Analyzer extends GenericObject
*/
public function analyzeUrl(string $url): self
{
$this->source = 'url';
$this->source = self::SOURCE_URL;
$this->sourceUrl = $url;
$client = Fetcher::fetch($url);
$this->setFilesystem($client->getFilesystem());
$this->setAttribute('fetcher', $client->toArray());
return $this->analyzeFile($client->filename);
}
@ -145,7 +147,7 @@ class Analyzer extends GenericObject
*/
public function analyzeBody(string $body, bool $deleteAfter = true): self
{
$this->source = 'body';
$this->source = self::SOURCE_BODY;
$filename = $this->storeBodyOnDisk($body);
@ -162,7 +164,7 @@ class Analyzer extends GenericObject
*/
public function analyzeFile(string $file, bool $deleteAfter = false): self
{
$this->source = 'file';
$this->source = self::SOURCE_FILE;
$this->sourcePath = $file;
if (empty($this->fileSystem)) {
@ -215,7 +217,58 @@ class Analyzer extends GenericObject
{
$this->storeBasicMetaData();
//TODO
$analyzers = $this->getAnalyzers();
foreach ($analyzers as $className) {
//TODO
}
}
/**
* Recursively find analyzers associated with file's mime type. Returns an
* array of analyzer class names.
*
* @return array
*/
protected function getAnalyzers(): array
{
if (empty($this->mimeType)) {
throw new Exception("Specified file has no mime-type, cannot analyze");
}
$classes = [];
$groups = config(sprintf('analyzer.analyzers.%s.groups', $this->mimeType), []);
$analyzers = config(sprintf('analyzer.analyzers.%s.analyzers', $this->mimeType), []);
foreach ($groups as $subGroup) {
$classes += $this->getAnalyzersPerGroup($subGroup);
}
$classes += $analyzers;
return $classes;
}
/**
* Return an array of analyzer class names, taken from the configuration
* file, associated with specified group. Groups are recursively fetched.
*
* @param string $groupName
* @return array
*/
protected function getAnalyzersPerGroup(string $groupName): array
{
$classes = [];
$groups = config(sprintf('analyzer.group.%s.groups', $groupName), []);
$analyzers = config(sprintf('analyzer.group.%s.analyzers', $groupName), []);
foreach ($groups as $subGroup) {
$classes += $this->getAnalyzersPerGroup($subGroup);
}
$classes += $analyzers;
return $classes;
}
/**

Loading…
Cancel
Save