Skip to content

Commit

Permalink
add template helper and twig extension
Browse files Browse the repository at this point in the history
  • Loading branch information
rande committed Dec 17, 2010
1 parent ec8f16b commit 50c8b73
Show file tree
Hide file tree
Showing 22 changed files with 821 additions and 259 deletions.
37 changes: 31 additions & 6 deletions DependencyInjection/MediaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@
*
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class MediaExtension extends Extension {
class MediaExtension extends Extension
{

/**
* Loads the url shortener configuration.
*
* @param array $config An array of configuration settings
* @param ContainerBuilder $container A ContainerBuilder instance
*/
public function configLoad($config, ContainerBuilder $container) {
public function configLoad($config, ContainerBuilder $container)
{

$definition = new Definition($config['class']);

$definition->addMethodCall('setSettings', array(isset($config['settings']) ? $config['settings'] : array()));

foreach($config['providers'] as $name => $provider) {

$provider_name = sprintf('media.provider.%s', $name);
Expand Down Expand Up @@ -65,24 +68,46 @@ public function configLoad($config, ContainerBuilder $container) {
}

$container->setDefinition('media.provider', $definition);

// register template helper
$definition = new Definition(
'Bundle\MediaBundle\Templating\Helper\MediaHelper',
array(
new Reference('media.provider'),
new Reference('templating')
)
);
$definition->addTag('templating.helper', array('alias' => 'media'));
$definition->addTag('templating.helper', array('alias' => 'thumbnail'));

$container->setDefinition('templating.helper.media', $definition);

// register the twig extension
$container
->register('twig.extension.media', 'Bundle\MediaBundle\Twig\Extension\MediaExtension')
->addTag('twig.extension');

}

/**
* Returns the base path for the XSD files.
*
* @return string The XSD base path
*/
public function getXsdValidationBasePath() {
public function getXsdValidationBasePath()
{

return __DIR__.'/../Resources/config/schema';
}

public function getNamespace() {
public function getNamespace()
{

return 'http://www.sonata-project.org/schema/dic/media';
}

public function getAlias() {
public function getAlias()
{

return "media";
}
Expand Down
1 change: 1 addition & 0 deletions Entity/BaseEntityMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,5 @@ public function getCreatedAt()
{
return $this->created_at;
}

}
43 changes: 43 additions & 0 deletions Entity/BaseGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,47 @@ public function getCreatedAt()
{
return $this->created_at;
}











































}
2 changes: 1 addition & 1 deletion Entity/BaseMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public function getCreatedAt()
*
* @param varchar $contentType
*/
public function setContentType(\varchar $contentType)
public function setContentType($contentType)
{
$this->content_type = $contentType;
}
Expand Down
3 changes: 2 additions & 1 deletion MediaBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Symfony\Component\HttpKernel\Bundle\Bundle;

class MediaBundle extends Bundle {
class MediaBundle extends Bundle
{

}
48 changes: 31 additions & 17 deletions Provider/BaseProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,34 @@

use Bundle\MediaBundle\Entity\BaseMedia as Media;

abstract class BaseProvider {
abstract class BaseProvider
{

protected $formats = array();
protected $settings = array();

public function __construct($name, $em, $settings = array()) {
public function __construct($name, $em, $settings = array())
{

$this->name = $name;
$this->em = $em;
$this->settings = $settings;
}

public function addFormat($name, $format) {
public function addFormat($name, $format)
{

$this->formats[$name] = $format;
}

public function requireThumbnails() {
public function getFormat($name)
{

return isset($this->formats[$name]) ? $this->formats[$name] : false;
}

public function requireThumbnails()
{
return true;
}

Expand All @@ -39,7 +49,8 @@ public function requireThumbnails() {
*
* @return void
*/
public function generateThumbnails(Media $media) {
public function generateThumbnails(Media $media)
{

if(!$this->requireThumbnails()) {
return;
Expand Down Expand Up @@ -131,7 +142,10 @@ abstract function postRemove(Media $media);
*/
abstract function postPersist(Media $media);

public function generatePrivatePath(Media $media) {
abstract function getHelperProperties(Media $media, $format);

public function generatePrivatePath(Media $media)
{
$limit_first_level = 100000;
$limit_second_level = 1000;

Expand All @@ -146,7 +160,8 @@ public function generatePrivatePath(Media $media) {
return $path;
}

public function generatePublicPath(Media $media) {
public function generatePublicPath(Media $media)
{

$limit_first_level = 100000;
$limit_second_level = 1000;
Expand All @@ -159,27 +174,24 @@ public function generatePublicPath(Media $media) {
$rep_second_level + 1
);

if($this->settings['cdn_enabled']) {

$path = sprintf('%s%s', $this->settings['cdn_path'], $path);
}

return $path;
}

public function buildDirectory(Media $media) {
public function buildDirectory(Media $media)
{
$path = $this->generatePrivatePath($media);

if (!is_dir($path)) {
if (!@mkdir($path, 0755, true)) {
throw new RuntimeException('unable to create directory : ' . $path);
throw new \RuntimeException('unable to create directory : ' . $path);
}
}

return $path;
}

public function generatePublicUrl(Media $media, $format) {
public function generatePublicUrl(Media $media, $format)
{

return sprintf('%s/thumb_%d_%s.jpg',
$this->generatePublicPath($media),
Expand All @@ -188,7 +200,8 @@ public function generatePublicUrl(Media $media, $format) {
);
}

public function generatePrivateUrl(Media $media, $format) {
public function generatePrivateUrl(Media $media, $format)
{

return sprintf('%s/thumb_%d_%s.jpg',
$this->generatePrivatePath($media),
Expand All @@ -198,7 +211,8 @@ public function generatePrivateUrl(Media $media, $format) {
}


public function getFormats() {
public function getFormats()
{

return $this->formats;
}
Expand Down
99 changes: 0 additions & 99 deletions Provider/DailyMotion.php

This file was deleted.

Loading

0 comments on commit 50c8b73

Please sign in to comment.