mirror of
https://github.com/KnpLabs/KnpMarkdownBundle.git
synced 2024-05-14 03:16:39 +02:00
Adding public alias and autowiring aliases
Updating README to reflect how we do things now
This commit is contained in:
parent
816b0ca898
commit
77d1412005
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace Knp\Bundle\MarkdownBundle\DependencyInjection;
|
namespace Knp\Bundle\MarkdownBundle\DependencyInjection;
|
||||||
|
|
||||||
|
use Knp\Bundle\MarkdownBundle\MarkdownParserInterface;
|
||||||
|
use Michelf\MarkdownInterface;
|
||||||
use Symfony\Component\Config\FileLocator;
|
use Symfony\Component\Config\FileLocator;
|
||||||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
|
||||||
use Symfony\Component\DependencyInjection\Alias;
|
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('markdown.parser', new Alias($config['parser']['service'], true));
|
||||||
|
$container->setAlias(MarkdownParserInterface::class, 'markdown.parser');
|
||||||
|
$container->setAlias(MarkdownInterface::class, 'markdown.parser');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,31 +10,43 @@ Add KnpMarkdownBundle to your project via [Composer](https://getcomposer.org/):
|
||||||
composer require knplabs/knp-markdown-bundle
|
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``
|
That's it! Start using it!
|
||||||
|
|
||||||
```php
|
|
||||||
$bundles = array(
|
|
||||||
// ...
|
|
||||||
new Knp\Bundle\MarkdownBundle\KnpMarkdownBundle(),
|
|
||||||
);
|
|
||||||
```
|
|
||||||
|
|
||||||
## USAGE
|
## USAGE
|
||||||
|
|
||||||
|
Once the bundle is installed, you can autowire a `MarkdownParserInterface`
|
||||||
|
into any service or controller:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// Use the service
|
use Knp\Bundle\MarkdownBundle\MarkdownParserInterface;
|
||||||
$html = $this->container->get('markdown.parser')->transformMarkdown($text);
|
|
||||||
|
|
||||||
// Use the helper with default parser
|
// from inside a controller
|
||||||
echo $view['markdown']->transform($text);
|
public function index(MarkdownParserInterface $parser)
|
||||||
|
{
|
||||||
|
$html = $parser->transformMarkdown($text);
|
||||||
|
}
|
||||||
|
|
||||||
// Use the helper and a select specific parser
|
// or from inside a service
|
||||||
echo $view['markdown']->transform($text, $parserName);
|
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
|
```twig
|
||||||
{# Use default parser #}
|
{# 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).
|
``markdown.parser.sundown`` requires the [php sundown extension](https://github.com/chobie/php-sundown).
|
||||||
|
|
||||||
For more details, see the implementations in Parser/Preset.
|
For more details, see the implementations in Parser/Preset.
|
||||||
|
|
||||||
## TEST
|
|
||||||
|
|
||||||
phpunit -c myapp vendor/bundles/Knp/Bundle/MarkdownBundle
|
|
||||||
|
|
57
Tests/IntegrationTest.php
Normal file
57
Tests/IntegrationTest.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue