1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Media; |
4
|
|
|
|
5
|
|
|
use Pimple\Container; |
6
|
|
|
use Silex\Application; |
7
|
|
|
use Media\Form\Type\MediaType; |
8
|
|
|
use Media\Provider\MediaProvider; |
9
|
|
|
use Pimple\ServiceProviderInterface; |
10
|
|
|
use Silex\Api\BootableProviderInterface; |
11
|
|
|
use Media\Provider\MediaProviderInterface; |
12
|
|
|
use Media\Form\DataTransformer\MediaTypeDataTransformer; |
13
|
|
|
|
14
|
|
|
class MediaServiceProvider implements BootableProviderInterface, ServiceProviderInterface |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
* @param Container $pimple |
20
|
|
|
* |
21
|
|
|
* @throws \RuntimeException |
22
|
|
|
*/ |
23
|
2 |
|
public function register(Container $pimple) |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
if ($pimple->offsetExists('media.type.provider')) { |
27
|
|
|
if (!$pimple->offsetGet('media.type.provider') instanceof MediaProviderInterface) { |
28
|
|
|
throw new \RuntimeException('Service media.type.provider must implements \Media\Provider\MediaProviderInterface'); |
29
|
|
|
} |
30
|
|
|
} else { |
31
|
|
|
$pimple['media.type.provider'] = function() use ($pimple) { |
32
|
|
|
return new MediaProvider($pimple['orm.em'], $pimple['media.upload.path']); |
33
|
|
|
}; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($pimple->offsetExists('media.type.data.transformer')) { |
37
|
|
|
if (!$pimple->offsetGet('media.type.data.transformer') instanceof MediaProviderInterface) { |
38
|
|
|
throw new \RuntimeException('Service media.type.data.transformer must implements \Media\Provider\MediaProviderInterface'); |
39
|
|
|
} |
40
|
|
|
} else { |
41
|
|
|
$pimple['media.type.data.transformer'] = function() { |
42
|
|
|
return MediaTypeDataTransformer::class; |
43
|
|
|
}; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$pimple->extend('twig', function(\Twig_Environment $twig) use ($pimple) { |
47
|
|
|
$twig->addExtension(new \Media\Twig\MediaExtension($pimple['request_stack'])); |
48
|
|
|
return $twig; |
49
|
|
|
}); |
50
|
|
|
|
51
|
2 |
|
$resourceDirectory = __DIR__ . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'views'; |
52
|
|
|
if (is_array($pimple['twig.path'])) { |
53
|
|
|
$pimple['twig.path'] = array_merge($pimple['twig.path'], [$resourceDirectory]); |
54
|
|
|
} else { |
55
|
|
|
$pimple['twig.path'] = [$pimple['twig.path'], $resourceDirectory]; |
56
|
2 |
|
} |
57
|
|
|
|
58
|
|
|
if (is_array($pimple['twig.form.templates'])) { |
59
|
|
|
$pimple['twig.form.templates'] = array_merge($pimple['twig.form.templates'], ['Form/media.widget.twig']); |
60
|
|
|
} else { |
61
|
|
|
$pimple['twig.form.templates'] = [$pimple['twig.form.templates'], 'Form/media.widget.twig']; |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
$pimple->extend('form.types', function ($types) use ($pimple) { |
66
|
|
|
$types[MediaType::class] = new MediaType($pimple['media.type.data.transformer'], $pimple['media.type.provider'], $pimple['media.entity']); |
67
|
|
|
return $types; |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
* @param Application $app |
74
|
|
|
* |
75
|
|
|
* @throws \RuntimeException |
76
|
|
|
*/ |
77
|
|
|
public function boot(Application $app) |
78
|
|
|
{ |
79
|
|
|
if (!$app->offsetExists('media.entity')) { |
80
|
|
|
throw new \RuntimeException('Parameter media.entity must be exists and implemented \Media\Entity\MediaInterface.'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!$app->offsetExists('media.upload.path')) { |
84
|
|
|
throw new \RuntimeException('Parameter media.upload.path must exists and be a writable folder.'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|