Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Attribute/CdnReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the VlabsMediaBundle package.
*
* (c) Valentin Ferriere <http://www.v-labs.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vlabs\MediaBundle\Attribute;

use Vlabs\MediaBundle\Entity\BaseFileInterface;
use Vlabs\MediaBundle\Adapter\AdapterInterface;

/**
* Read values for the Cdn annotation
*
* @author Valentin Ferriere <valentin.ferriere@gmail.com>
*/
class CdnReader
{
private string $attributeClass = 'Vlabs\MediaBundle\Attribute\Vlabs\Cdn';

private $baseUrl;
private $adapter;
private $config = [];

public function __construct(AdapterInterface $adapter)
{
$this->adapter = $adapter;
}

public function handle(BaseFileInterface $file)
{
$class = $this->adapter->getClass($file);
$property = new \ReflectionProperty($class, 'path');
$cdn = $property->getAttributes($this->attributeClass);
if (!$cdn)
{
throw new \Exception("Attribute Vlabs\Cdn not found on property {$property->getName()} of class $class");
}
if (\count($cdn) > 1)
{
throw new \Exception("Multiple Vlabs\Cdn attributes found on property {$property->getName()} of class $class");
}
$media_arguments = $cdn[0]->getArguments();

$base_url = $media_arguments['base_url'];

if(array_key_exists($base_url, $this->config)) {
$this->baseUrl = $this->config[$base_url];
} else {
$this->baseUrl = $this->config['default'] ?? null;
}
}

public function getBaseUrl()
{
return $this->baseUrl;
}

public function setConfig($config = array())
{
$this->config = $config;
}
}
62 changes: 62 additions & 0 deletions Attribute/MediaReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the VlabsMediaBundle package.
*
* (c) Valentin Ferriere <http://www.v-labs.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vlabs\MediaBundle\Attribute;

/**
* Read values and store metadatas for the Media annotation
*
* @author Valentin Ferriere <valentin.ferriere@gmail.com>
*/
class MediaReader
{
private string $attributeClass = 'Vlabs\MediaBundle\Attribute\Vlabs\Media';

private $identifier;
private $uploadDir;
private $metadatas = [];

public function handle(string $class, string $property)
{
$property = new \ReflectionProperty($class, $property);
$media = $property->getAttributes($this->attributeClass);
if (!$media)
{
throw new \Exception("Attribute Vlabs\Media not found on property {$property->getName()} of class $class");
}
if (\count($media) > 1)
{
throw new \Exception("Multiple Vlabs\Media attributes found on property {$property->getName()} of class $class");
}
$media_arguments = $media[0]->getArguments();

$this->identifier = $media_arguments['identifier'];
$this->uploadDir = $media_arguments['upload_dir'];

$this->metadatas[$class][$property->getName()]['identifier'] = $this->identifier;
$this->metadatas[$class][$property->getName()]['uploadDir'] = $this->uploadDir;
}

public function getIdentifier()
{
return $this->identifier;
}

public function getUploadDir()
{
return $this->uploadDir;
}

public function getMetaDatas()
{
return $this->metadatas;
}
}
33 changes: 33 additions & 0 deletions Attribute/Vlabs/Cdn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the VlabsMediaBundle package.
*
* (c) Valentin Ferriere <http://www.v-labs.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vlabs\MediaBundle\Attribute\Vlabs;

use Attribute;

/**
* @author Valentin Ferriere <valentin.ferriere@gmail.com>
*/
#[\Attribute(Attribute::TARGET_PROPERTY)]
class Cdn
{
private $baseUrl;

public function __construct(string $base_url)
{
$this->baseUrl = $base_url;
}

public function getBaseUrl()
{
return $this->baseUrl;
}
}
40 changes: 40 additions & 0 deletions Attribute/Vlabs/Media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the VlabsMediaBundle package.
*
* (c) Valentin Ferriere <http://www.v-labs.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vlabs\MediaBundle\Attribute\Vlabs;

use Attribute;

/**
* @author Valentin Ferriere <valentin.ferriere@gmail.com>
*/
#[\Attribute(Attribute::TARGET_PROPERTY)]
class Media
{
private $identifier;
private $uploadDir;

public function __construct(string $identifier, string $upload_dir)
{
$this->identifier = $identifier;
$this->uploadDir = $upload_dir;
}

public function getIdentifier()
{
return $this->identifier;
}

public function getUploadDir()
{
return $this->uploadDir;
}
}
99 changes: 24 additions & 75 deletions Entity/BaseFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,32 @@

use Doctrine\ORM\Mapping as ORM;

/**
* Vlabs\MediaBundle\Entity\BaseFile
*
* @ORM\MappedSuperclass
*/
#[ORM\MappedSuperclass]
abstract class BaseFile implements BaseFileInterface
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
protected ?int $id = null;

/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255)
*/
protected $name;
#[ORM\Column(type: 'string')]
protected ?string $name = null;

/**
* @var string $size
*
* @ORM\Column(name="size", type="integer", nullable=true)
*/
protected $size;
#[ORM\Column(type: 'integer', nullable: true)]
protected ?int $size = null;

/**
* @var string $createdAt
*
* @ORM\Column(name="created_at", type="datetime")
*/
protected $createdAt;
#[ORM\Column(type: 'datetime')]
protected \DateTime $created_at;

/**
* @var string $contentType
*
* @ORM\Column(name="content_type", type="string", length=50, nullable=true)
*/
protected $contentType;
#[ORM\Column(type: 'string', length: 50, nullable: true)]
protected ?string $content_type;

public function __construct()
{
$this->createdAt = new \DateTime();
$this->created_at = new \DateTime();
}

/**
* Get id
*
* @return integer
*/
public function getId()
public function getId(): ?int
{
return $this->id;
}
Expand Down Expand Up @@ -113,54 +84,32 @@ public function setSize($size)
*
* @return integer
*/
public function getSize()
public function getSize(): ?int
{
return $this->size;
}

/**
* Set createdAt
*
* @param \DateTime $createdAt
* @return BaseFile
*/
public function setCreatedAt($createdAt)
public function setCreatedAt(\DateTime $created_at): self
{
$this->createdAt = $createdAt;
$this->created_at = $created_at;

return $this;
}

/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
public function getCreatedAt(): \DateTime
{
return $this->createdAt;
return $this->created_at;
}

/**
* Set contentType
*
* @param string $contentType
* @return BaseFile
*/
public function setContentType($contentType)
public function setContentType(string $content_type): self
{
$this->contentType = $contentType;
$this->content_type = $content_type;

return $this;
}

/**
* Get contentType
*
* @return string
*/
public function getContentType()
public function getContentType(): ?string
{
return $this->contentType;
return $this->content_type;
}
}
4 changes: 1 addition & 3 deletions Entity/BaseFileInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ public function getName();

/**
* Set File content type
*
* @param string $contentType
*/
public function setContentType($contentType);
public function setContentType(string $content_type);

/**
* Get File content type
Expand Down
2 changes: 1 addition & 1 deletion Handler/GaufretteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Gaufrette\Filesystem;
use Vlabs\MediaBundle\Entity\BaseFileInterface;
use Vlabs\MediaBundle\Annotation\CdnReader;
use Vlabs\MediaBundle\Attribute\CdnReader;

/**
* @author Valentin Ferriere <valentin.ferriere@gmail.com>
Expand Down
2 changes: 1 addition & 1 deletion Handler/HandlerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Vlabs\MediaBundle\Handler;

use Vlabs\MediaBundle\Annotation\MediaReader;
use Vlabs\MediaBundle\Attribute\MediaReader;
use Vlabs\MediaBundle\Entity\BaseFileInterface;
use Vlabs\MediaBundle\Adapter\AdapterInterface;

Expand Down
Loading