Skip to content

Commit

Permalink
Merge pull request #45 from eschmar/develop
Browse files Browse the repository at this point in the history
Add symfony 5.0 support
  • Loading branch information
eschmar authored Jun 4, 2020
2 parents b7cce02 + 67e853a commit ca9332e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
14 changes: 7 additions & 7 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files.
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
* {@inheritDoc}
*/
class Configuration implements ConfigurationInterface
{
Expand All @@ -21,10 +19,12 @@ public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder(static::ROOT_NODE);

// Symfony 4.2+
if (method_exists($treeBuilder, 'getRootNode')) $rootNode = $treeBuilder->getRootNode();
// Symfony 4.1 and below
else $rootNode = $treeBuilder->root(static::ROOT_NODE);
// BC layer for symfony/config 4.1 and older
if (! \method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->root(static::ROOT_NODE);
} else {
$rootNode = $treeBuilder->getRootNode();
}

$treeBuilder->getRootNode()
->children()
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ Uses a range of +-7 days, after that, the actual date is returned.
## Install
Composer (<a href="https://packagist.org/packages/eschmar/time-ago-bundle" target="_blank">Packagist</a>):
```sh
composer require eschmar/time-ago-bundle ~v1.0 # Symfony ^4.1
composer require eschmar/time-ago-bundle ^v2.0.0 # Symfony ^5.0
```

or for older symfony versions:
```sh
composer require eschmar/time-ago-bundle ^v1.1.0 # Symfony ^4.x
composer require eschmar/time-ago-bundle ~v0.4.0 # Symfony ^2.8
composer require eschmar/time-ago-bundle ~v0.5.0 # Symfony ^3.4
```

app/Appkernel.php:
app/Appkernel.php (Symfony <4):
```php
new Eschmar\TimeAgoBundle\EschmarTimeAgoBundle(),
```
Expand Down
20 changes: 10 additions & 10 deletions Twig/TimeAgoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Eschmar\TimeAgoBundle\Twig;

use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Provides a simple twig filter for expressing time difference in words.
*
* @author Marcel Eschmann, @eschmar
**/
class TimeAgoExtension extends \Twig_Extension
class TimeAgoExtension extends \Twig\Extension\AbstractExtension
{
const TRANSLATION_NAMESPACE = 'time_ago';

Expand Down Expand Up @@ -43,7 +43,7 @@ public function getName()
public function getFilters()
{
return [
new \Twig_SimpleFilter('ago', [$this, 'agoFilter']),
new \Twig\TwigFilter('ago', [$this, 'agoFilter']),
];
}

Expand All @@ -65,19 +65,19 @@ public function agoFilter(\DateTime $date, $format = null)
$days = floor($diff / 86400);

if ($days >= 7) return $date->format($format);
if ($days >= 1) return $this->translator->transChoice($prefix . '.days', $days, ['#' => $days]);
if ($days >= 1) return $this->translator->trans($prefix . '.days', ['#' => $days, '%count%' => $days]);

if ($diff < 60) return $this->translator->trans($prefix . '.now');

$m = floor($diff / 60);
if ($diff < 120) return $this->translator->transChoice($prefix . '.minutes', 1);
if ($diff < 3600) return $this->translator->transChoice($prefix . '.minutes', $m, ['#' => $m]);
if ($diff < 120) return $this->translator->trans($prefix . '.minutes', ['%count%' => 1]);
if ($diff < 3600) return $this->translator->trans($prefix . '.minutes', ['#' => $m, '%count%' => $m]);

$h = floor($diff / 3600);
if ($diff < 7200) return $this->translator->transChoice($prefix . '.hours', 1);
if ($diff < 86400) return $this->translator->transChoice($prefix . '.hours', $h, ['#' => $h]);
if ($diff < 7200) return $this->translator->trans($prefix . '.hours', ['%count%' => 1]);
if ($diff < 86400) return $this->translator->trans($prefix . '.hours', ['#' => $h, '%count%' => $h]);

return $date->format($format);
}

} // END class TimeAgoExtension extends \Twig_Extension
} // END class TimeAgoExtension extends \Twig\Extension\AbstractExtension
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"target-dir": "Eschmar/TimeAgoBundle",
"require": {
"php": ">=7.1.0",
"twig/twig": "^3.0.0",
"symfony/flex": "*"
}
}

0 comments on commit ca9332e

Please sign in to comment.