|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pgs\RestfonyBundle\Manipulator; |
|
4
|
|
|
|
|
5
|
|
|
use Sensio\Bundle\GeneratorBundle\Manipulator\Manipulator; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Changes the PHP code of a YAML routing file. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Fabien Potencier <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class RoutingManipulator extends Manipulator |
|
14
|
|
|
{ |
|
15
|
|
|
private $file; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $file The YAML routing file path |
|
21
|
|
|
*/ |
|
22
|
1 |
|
public function __construct($file) |
|
23
|
|
|
{ |
|
24
|
1 |
|
$this->file = $file; |
|
25
|
1 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Adds a routing resource at the top of the existing ones. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $bundle |
|
31
|
|
|
* @param string $prefix |
|
32
|
|
|
* |
|
33
|
|
|
* @return Boolean true if it worked, false otherwise |
|
34
|
|
|
* |
|
35
|
|
|
* @throws \RuntimeException If bundle is already imported |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function addResource($bundle, $prefix = '/') |
|
38
|
|
|
{ |
|
39
|
1 |
|
$current = ''; |
|
40
|
1 |
|
$code = sprintf( |
|
41
|
1 |
|
"%s:\n", |
|
42
|
1 |
|
Container::underscore(substr($bundle, 0, -6)) . ( |
|
43
|
1 |
|
'/' !== $prefix |
|
44
|
1 |
|
? '_' . str_replace('/', '_', substr($prefix, 1)) |
|
45
|
1 |
|
: '' |
|
46
|
|
|
) |
|
47
|
|
|
); |
|
48
|
1 |
|
if (file_exists($this->file)) { |
|
49
|
1 |
|
$current = file_get_contents($this->file); |
|
50
|
|
|
|
|
51
|
|
|
// Don't add same bundle twice |
|
52
|
1 |
|
if (false !== strpos($current, $code)) { |
|
53
|
1 |
|
return false; |
|
54
|
|
|
} |
|
55
|
1 |
|
} elseif (!is_dir($dir = dirname($this->file))) { |
|
56
|
1 |
|
mkdir($dir, 0777, true); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
$code .= sprintf( |
|
60
|
1 |
|
" resource: pgs.rest.controller.%s\n type: rest\n", |
|
61
|
1 |
|
str_replace('/', '_', substr($prefix, 1)) |
|
62
|
|
|
); |
|
63
|
1 |
|
$code .= "\n"; |
|
64
|
1 |
|
$code .= $current; |
|
65
|
|
|
|
|
66
|
1 |
|
return file_put_contents($this->file, $code) !== false; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|