Adding public alias and autowiring aliases

Updating README to reflect how we do things now
This commit is contained in:
Ryan Weaver 2017-11-29 13:29:54 -05:00
parent 816b0ca898
commit 77d1412005
3 changed files with 89 additions and 20 deletions

View file

@ -2,6 +2,8 @@
namespace Knp\Bundle\MarkdownBundle\DependencyInjection;
use Knp\Bundle\MarkdownBundle\MarkdownParserInterface;
use Michelf\MarkdownInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\Alias;
@ -45,5 +47,7 @@ class KnpMarkdownExtension extends Extension
}
$container->setAlias('markdown.parser', new Alias($config['parser']['service'], true));
$container->setAlias(MarkdownParserInterface::class, 'markdown.parser');
$container->setAlias(MarkdownInterface::class, 'markdown.parser');
}
}

View file

@ -10,31 +10,43 @@ Add KnpMarkdownBundle to your project via [Composer](https://getcomposer.org/):
composer require knplabs/knp-markdown-bundle
```
Based on your Composer install, you might need to use `php composer.phar require knplabs/knp-markdown-bundle`.
If you're *not* using Symfony Flex, you will also need to enable
the bundle in your `app/AppKernel.php` file
(`new Knp\Bundle\MarkdownBundle\KnpMarkdownBundle()`).
Next, register the bundle in ``app/AppKernel.php``
```php
$bundles = array(
// ...
new Knp\Bundle\MarkdownBundle\KnpMarkdownBundle(),
);
```
That's it! Start using it!
## USAGE
Once the bundle is installed, you can autowire a `MarkdownParserInterface`
into any service or controller:
```php
// Use the service
$html = $this->container->get('markdown.parser')->transformMarkdown($text);
use Knp\Bundle\MarkdownBundle\MarkdownParserInterface;
// Use the helper with default parser
echo $view['markdown']->transform($text);
// from inside a controller
public function index(MarkdownParserInterface $parser)
{
$html = $parser->transformMarkdown($text);
}
// Use the helper and a select specific parser
echo $view['markdown']->transform($text, $parserName);
// or from inside a service
private $parser;
public function __construct(MarkdownParserInterface $parser)
{
$this->parser = $parser;
}
public function someMethod()
{
$html = $parser->transformMarkdown($text);
}
```
If you have enabled the Twig markdown filter, you can use the following in your Twig templates:
There is also a public `markdown.parser` service you can use.
In Twig, you can use the `markdown` filter:
```twig
{# Use default parser #}
@ -74,7 +86,3 @@ and one which is uses the php sundown extension:
``markdown.parser.sundown`` requires the [php sundown extension](https://github.com/chobie/php-sundown).
For more details, see the implementations in Parser/Preset.
## TEST
phpunit -c myapp vendor/bundles/Knp/Bundle/MarkdownBundle

57
Tests/IntegrationTest.php Normal file
View file

@ -0,0 +1,57 @@
<?php
namespace Knp\Bundle\MarkdownBundle\Tests;
use Knp\Bundle\MarkdownBundle\KnpMarkdownBundle;
use Knp\Bundle\MarkdownBundle\Parser\MarkdownParser;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
class IntegrationTest extends \PHPUnit_Framework_TestCase
{
public function testServicesAvailable()
{
$kernel = new IntegrationKernel('dev', true);
$kernel->boot();
$container = $kernel->getContainer();
$this->assertInstanceOf(MarkdownParser::class, $container->get('markdown.parser'));
}
}
class IntegrationKernel extends Kernel
{
use MicroKernelTrait;
private $cacheDir;
public function registerBundles()
{
return [
new FrameworkBundle(),
new KnpMarkdownBundle(),
];
}
protected function configureRoutes(RouteCollectionBuilder $routes)
{
}
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
{
$c->setParameter('kernel.secret', '1234');
}
public function getCacheDir()
{
if (null === $this->cacheDir) {
$this->cacheDir = sys_get_temp_dir().'/'.rand(100, 999);
}
return $this->cacheDir;
}
}