-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
203 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\CronExpressionBundle\Form\TypeGuesser; | ||
|
||
use Brick\Reflection\ImportResolver; | ||
use Cron\CronExpression; | ||
use phpDocumentor\Reflection\DocBlock\Tags\Var_; | ||
use Setono\CronExpressionBundle\Form\Type\CronExpressionType; | ||
use Symfony\Component\Form\FormTypeGuesserInterface; | ||
use Symfony\Component\Form\Guess\Guess; | ||
use Symfony\Component\Form\Guess\TypeGuess; | ||
|
||
final class CronExpressionTypeGuesser implements FormTypeGuesserInterface | ||
{ | ||
public function guessType($class, $property) | ||
{ | ||
try { | ||
$reflectionClass = new \ReflectionClass($class); | ||
} catch (\ReflectionException $e) { | ||
return null; | ||
} | ||
|
||
$reflectionProperty = $reflectionClass->getProperty($property); | ||
|
||
if (false === $reflectionProperty->getDocComment()) { | ||
return null; | ||
} | ||
|
||
$factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); | ||
$docBlock = $factory->create($reflectionProperty->getDocComment()); | ||
$varTags = $docBlock->getTagsByName('var'); | ||
|
||
if (empty($varTags)) { | ||
return null; | ||
} | ||
|
||
/** @var Var_ $varTag */ | ||
$varTag = $varTags[0]; | ||
|
||
$typeName = (string) $varTag->getType(); | ||
|
||
$resolver = new ImportResolver($reflectionClass); | ||
|
||
foreach ([$typeName, ltrim($typeName, '\\')] as $item) { | ||
$fqn = $resolver->resolve($item); | ||
|
||
if (CronExpression::class === $fqn) { | ||
return new TypeGuess(CronExpressionType::class, [], Guess::VERY_HIGH_CONFIDENCE); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function guessRequired($class, $property) | ||
{ | ||
return null; | ||
} | ||
|
||
public function guessMaxLength($class, $property) | ||
{ | ||
return null; | ||
} | ||
|
||
public function guessPattern($class, $property) | ||
{ | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<defaults public="false" /> | ||
|
||
<service id="setono_cron_expression.form.type_guesser.cron_expression" class="Setono\CronExpressionBundle\Form\TypeGuesser\CronExpressionTypeGuesser"> | ||
<tag name="form.type_guesser" /> | ||
</service> | ||
</services> | ||
</container> |
28 changes: 28 additions & 0 deletions
28
Tests/DependencyInjection/SetonoCronExpressionExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\CronExpressionBundle\Tests\DependencyInjection; | ||
|
||
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; | ||
use Setono\CronExpressionBundle\DependencyInjection\SetonoCronExpressionExtension; | ||
|
||
final class SetonoCronExpressionExtensionTest extends AbstractExtensionTestCase | ||
{ | ||
protected function getContainerExtensions() | ||
{ | ||
return [ | ||
new SetonoCronExpressionExtension(), | ||
]; | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function loadServices(): void | ||
{ | ||
$this->load(); | ||
|
||
$this->assertContainerBuilderHasService('setono_cron_expression.form.type_guesser.cron_expression'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\CronExpressionBundle\Tests\Form\TypeGuesser; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Setono\CronExpressionBundle\Form\Type\CronExpressionType; | ||
use Setono\CronExpressionBundle\Form\TypeGuesser\CronExpressionTypeGuesser; | ||
use Symfony\Component\Form\Guess\Guess; | ||
|
||
final class CronExpressionTypeGuesserTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function guessType(): void | ||
{ | ||
$guesser = new CronExpressionTypeGuesser(); | ||
$res = $guesser->guessType(Stub::class, 'property'); | ||
|
||
$this->assertSame(CronExpressionType::class, $res->getType()); | ||
$this->assertSame(Guess::VERY_HIGH_CONFIDENCE, $res->getConfidence()); | ||
|
||
$guesser = new CronExpressionTypeGuesser(); | ||
$res = $guesser->guessType(StubAliased::class, 'property'); | ||
|
||
$this->assertSame(CronExpressionType::class, $res->getType()); | ||
$this->assertSame(Guess::VERY_HIGH_CONFIDENCE, $res->getConfidence()); | ||
|
||
$guesser = new CronExpressionTypeGuesser(); | ||
$res = $guesser->guessType(StubImported::class, 'property'); | ||
|
||
$this->assertSame(CronExpressionType::class, $res->getType()); | ||
$this->assertSame(Guess::VERY_HIGH_CONFIDENCE, $res->getConfidence()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\CronExpressionBundle\Tests\Form\TypeGuesser; | ||
|
||
final class Stub | ||
{ | ||
/** | ||
* @var \Cron\CronExpression | ||
*/ | ||
private $property; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\CronExpressionBundle\Tests\Form\TypeGuesser; | ||
|
||
use Cron\CronExpression as CronExpr; | ||
|
||
final class StubAliased | ||
{ | ||
/** | ||
* @var CronExpr | ||
*/ | ||
private $property; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\CronExpressionBundle\Tests\Form\TypeGuesser; | ||
|
||
use Cron\CronExpression; | ||
|
||
final class StubImported | ||
{ | ||
/** | ||
* @var CronExpression | ||
*/ | ||
private $property; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters