Skip to content

Commit

Permalink
Feat: PHP8 / Symfony 5+ support (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek committed Jan 17, 2022
1 parent 6619130 commit 8e88207
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 1,853 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/tests.yml
Expand Up @@ -11,12 +11,15 @@ jobs:

strategy:
matrix:
php: ['5.6', '7.0', '7.1']
symfony: [2.7.*, 2.8.*, 3.2.*, 3.4.*]

include:
- php: '7.1'
symfony: 4.0.*
php:
- '7.4'
- '8.0'

symfony:
- '5.0.*'
- '5.1.*'
- '5.2.*'
- '5.3.*'

steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,4 +1,5 @@
.idea/*
vendor/*
build/
.DS_Store
.DS_Store
composer.lock
48 changes: 29 additions & 19 deletions BotDetector.php
Expand Up @@ -11,8 +11,13 @@

namespace Vipx\BotDetect;

use InvalidArgumentException;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\ConfigCache;
use Vipx\BotDetect\Metadata\Dumper\MetadataDumper;
use Vipx\BotDetect\Metadata\Dumper\PhpMetadataDumper;
use Vipx\BotDetect\Metadata\Metadata;
use Vipx\BotDetect\Metadata\MetadataCollection;

class BotDetector implements BotDetectorInterface
{
Expand All @@ -23,11 +28,11 @@ class BotDetector implements BotDetectorInterface
private $options;

/**
* @param \Symfony\Component\Config\Loader\LoaderInterface $loader
* @param LoaderInterface $loader
* @param $resource
* @param array $options
*/
public function __construct(LoaderInterface $loader, $resource, array $options = array())
public function __construct(LoaderInterface $loader, $resource, array $options = [])
{
$this->loader = $loader;
$this->resource = $resource;
Expand All @@ -37,19 +42,19 @@ public function __construct(LoaderInterface $loader, $resource, array $options =

/**
* @param array $options
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setOptions(array $options)
public function setOptions(array $options): void
{
$this->options = array(
'cache_dir' => null,
'debug' => false,
'metadata_cache_file' => 'project_vipx_bot_detect_metadata.php',
'metadata_dumper_class' => 'Vipx\\BotDetect\\Metadata\\Dumper\\PhpMetadataDumper',
);
$this->options = [
'cache_dir' => null,
'debug' => false,
'metadata_cache_file' => 'project_vipx_bot_detect_metadata.php',
'metadata_dumper_class' => PhpMetadataDumper::class,
];

// check option names and live merge, if errors are encountered Exception will be thrown
$invalid = array();
$invalid = [];
foreach ($options as $key => $value) {
if (array_key_exists($key, $this->options)) {
$this->options[$key] = $value;
Expand All @@ -59,7 +64,9 @@ public function setOptions(array $options)
}

if ($invalid) {
throw new \InvalidArgumentException(sprintf('The BotDetector does not support the following options: "%s".', implode('\', \'', $invalid)));
throw new InvalidArgumentException(
sprintf('The BotDetector does not support the following options: "%s".', implode('\', \'', $invalid))
);
}
}

Expand All @@ -68,15 +75,15 @@ public function setOptions(array $options)
*
* @return array
*/
public function getOptions()
public function getOptions(): array
{
return $this->options;
}

/**
* @return \Vipx\BotDetect\Metadata\Metadata[]
* @return Metadata[]
*/
public function getMetadatas()
public function getMetadatas(): array
{
if (null !== $this->metadatas) {
return $this->metadatas;
Expand All @@ -88,18 +95,21 @@ public function getMetadatas()
return $this->metadatas = $metadataCollection->getMetadatas();
}

$cache = new ConfigCache($this->options['cache_dir'] . '/' . $this->options['metadata_cache_file'], $this->options['debug']);
$cache = new ConfigCache(
$this->options['cache_dir'].'/'.$this->options['metadata_cache_file'],
$this->options['debug']
);

if ($cache->isFresh()) {
return $this->metadatas = require $cache->getPath();
}

/** @var $metadataCollection \Vipx\BotDetect\Metadata\MetadataCollection */
/** @var $metadataCollection MetadataCollection */
$metadataCollection = $this->loader->load($this->resource);
$dumperClass = $this->options['metadata_dumper_class'];
$metadatas = $metadataCollection->getMetadatas();

/** @var $dumper \Vipx\BotDetect\Metadata\Dumper\MetadataDumper */
/** @var $dumper MetadataDumper */
$dumper = new $dumperClass($metadatas);

$cache->write($dumper->dump(), $metadataCollection->getResources());
Expand All @@ -110,7 +120,7 @@ public function getMetadatas()
/**
* {@inheritdoc}
*/
public function detect($agent, $ip)
public function detect(string $agent, string $ip): ?Metadata
{
$agent = trim($agent);
foreach ($this->getMetadatas() as $metadata) {
Expand Down
6 changes: 4 additions & 2 deletions BotDetectorInterface.php
Expand Up @@ -11,6 +11,8 @@

namespace Vipx\BotDetect;

use Vipx\BotDetect\Metadata\Metadata;

interface BotDetectorInterface
{

Expand All @@ -19,8 +21,8 @@ interface BotDetectorInterface
*
* @param string $agent
* @param string $ip
* @return null|\Vipx\BotDetect\Metadata\Metadata
* @return null|Metadata
*/
function detect($agent, $ip);
public function detect(string $agent, string $ip): ?Metadata;

}
11 changes: 7 additions & 4 deletions Metadata/Dumper/MetadataDumper.php
Expand Up @@ -11,13 +11,16 @@

namespace Vipx\BotDetect\Metadata\Dumper;

use Vipx\BotDetect\Metadata\Metadata;
use Vipx\BotDetect\Metadata\MetadataInterface;

abstract class MetadataDumper implements MetadataDumperInterface
{

private $metadatas = array();
private $metadatas;

/**
* @param \Vipx\BotDetect\Metadata\MetadataInterface[] $metadatas
* @param MetadataInterface[] $metadatas
*/
public function __construct(array $metadatas)
{
Expand All @@ -27,9 +30,9 @@ public function __construct(array $metadatas)
/**
* Returns all bot metadatas
*
* @return \Vipx\BotDetect\Metadata\Metadata[]
* @return Metadata[]
*/
public function getMetadatas()
public function getMetadatas(): array
{
return $this->metadatas;
}
Expand Down
2 changes: 1 addition & 1 deletion Metadata/Dumper/MetadataDumperInterface.php
Expand Up @@ -19,6 +19,6 @@ interface MetadataDumperInterface
*
* @return string
*/
function dump();
public function dump(): string;

}
14 changes: 8 additions & 6 deletions Metadata/Dumper/PhpMetadataDumper.php
Expand Up @@ -19,7 +19,7 @@ class PhpMetadataDumper extends MetadataDumper
/**
* {@inheritdoc}
*/
public function dump()
public function dump(): string
{
return <<<EOF
<?php
Expand All @@ -37,19 +37,21 @@ public function dump()
EOF;
}

private function dumpMetadatas()
private function dumpMetadatas(): string
{
$metadatas = $this->getMetadatas();
$dump = array();
$dump = [];

foreach ($metadatas as $name => $metadata) {
/* $ip = null, $type = self::TYPE_BOT, $agentMatch = self::AGENT_MATCH_REGEXP */
$dump[] = sprintf("\$metdatas['%s'] = new %s('%s', '%s', %s);",
$dump[] = sprintf(
"\$metdatas['%s'] = new %s('%s', '%s', %s);",
$name,
get_class($metadata),
$name,
$metadata->getAgent(),
var_export($metadata->getIp(), true));
var_export($metadata->getIp(), true)
);

$type = $metadata->getType();

Expand All @@ -70,7 +72,7 @@ private function dumpMetadatas()
}
}

return implode($dump, "\n");
return implode("\n", $dump);
}

}
30 changes: 19 additions & 11 deletions Metadata/Loader/YamlFileLoader.php
Expand Up @@ -11,6 +11,8 @@

namespace Vipx\BotDetect\Metadata\Loader;

use Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException;
use Symfony\Component\Config\Exception\LoaderLoadException;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Config\Resource\FileResource;
Expand All @@ -25,14 +27,14 @@ class YamlFileLoader extends FileLoader
* @param null|string $type
* @return MetadataCollection
*/
public function load($file, $type = null)
public function load($file, ?string $type = null): MetadataCollection
{
$file = $this->locator->locate($file, __DIR__.'/../../Resources/metadata');
$collection = new MetadataCollection();

$collection->addResource(new FileResource($file));

$content = Yaml::parse( file_get_contents($file) );
$content = Yaml::parse(file_get_contents($file));

$this->parseImports($collection, $content, $file);
$this->parseBots($collection, $content);
Expand All @@ -45,7 +47,7 @@ public function load($file, $type = null)
* @param null|string $type
* @return bool
*/
public function supports($resource, $type = null)
public function supports($resource, ?string $type = null): bool
{
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
}
Expand All @@ -56,19 +58,25 @@ public function supports($resource, $type = null)
* @param MetadataCollection $collection
* @param array $content
* @param string $file
* @throws \Exception
* @throws \Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException
* @throws \Symfony\Component\Config\Exception\FileLoaderLoadException
* @throws FileLoaderImportCircularReferenceException
* @throws LoaderLoadException
*/
private function parseImports(MetadataCollection $collection, $content, $file)
private function parseImports(MetadataCollection $collection, array $content, string $file): void
{
if (!isset($content['imports'])) {
return;
}

foreach ($content['imports'] as $import) {
$this->setCurrentDir(dirname($file));
$collection->addCollection($this->import($import['resource'], null, isset($import['ignore_errors']) ? (Boolean) $import['ignore_errors'] : false, $file));
$collection->addCollection(
$this->import(
$import['resource'],
null,
isset($import['ignore_errors']) && $import['ignore_errors'],
$file
)
);
}
}

Expand All @@ -77,16 +85,16 @@ private function parseImports(MetadataCollection $collection, $content, $file)
*
* @param MetadataCollection $collection
* @param $content
* @return array
* @return void
*/
private function parseBots(MetadataCollection $collection, $content)
private function parseBots(MetadataCollection $collection, $content): void
{
if (!isset($content['bots'])) {
return;
}

foreach ($content['bots'] as $name => $bot) {
$ip = isset($bot['ip']) ? $bot['ip'] : null;
$ip = $bot['ip'] ?? null;
$metadata = new Metadata($name, $bot['agent'], $ip);

if (isset($bot['type'])) {
Expand Down

0 comments on commit 8e88207

Please sign in to comment.