|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Webcook security bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* See LICENSE file in the root of the bundle. Webcook |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Webcook\Cms\SecurityBundle\Common; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Component\Finder\Finder; |
|
12
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Basic helper class. |
|
16
|
|
|
*/ |
|
17
|
|
|
class SecurityHelper |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Get system resources name. |
|
21
|
|
|
* |
|
22
|
|
|
* |
|
23
|
|
|
* @return array Array of system resources. |
|
24
|
49 |
|
*/ |
|
25
|
|
|
public static function getResourcesNames($path) |
|
26
|
49 |
|
{ |
|
27
|
49 |
|
$resources = array(); |
|
28
|
|
|
$finder = new Finder(); |
|
29
|
49 |
|
try { |
|
30
|
1 |
|
$finder->files()->in($path)->name('*.php'); |
|
31
|
1 |
|
} catch (\InvalidArgumentException $e) { |
|
32
|
|
|
return $resources; |
|
33
|
|
|
} |
|
34
|
49 |
|
|
|
35
|
49 |
|
$added = 0; |
|
|
|
|
|
|
36
|
49 |
|
foreach ($finder as $file) { |
|
37
|
49 |
|
$fileContent = file_get_contents($file->getRealPath()); |
|
38
|
49 |
|
if (strpos($fileContent, '@ApiResource') !== false) { |
|
39
|
49 |
|
$resources[] = self::extractName($file->getRealPath(), str_replace('.php', '', $file->getFilename())); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
49 |
|
return $resources; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Extract name from the controller path string. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $resourcePath Path of the controller. |
|
50
|
|
|
* |
|
51
|
|
|
* @return string Name. |
|
52
|
|
|
*/ |
|
53
|
49 |
|
public static function extractName($resourcePath, $name) |
|
54
|
|
|
{ |
|
55
|
49 |
|
$resourcePath = str_replace('\\', '/', $resourcePath); |
|
56
|
|
|
|
|
57
|
49 |
|
return self::extract($resourcePath, 'Bundle').' - '.$name; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Extract string. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $string String from which will be name extraced. |
|
64
|
|
|
* @param string $name Extracted string. |
|
65
|
|
|
* |
|
66
|
|
|
* @return string Name. |
|
67
|
|
|
*/ |
|
68
|
49 |
|
private static function extract($string, $name) |
|
69
|
|
|
{ |
|
70
|
49 |
|
preg_match("/^.*\/(.*)$name.*$/", $string, $matches); |
|
71
|
|
|
|
|
72
|
49 |
|
return count($matches) > 0 ? $matches[1] : null; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.