1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BringYourOwnIdeas\UpdateChecker; |
4
|
|
|
|
5
|
|
|
use Composer\Config; |
6
|
|
|
use Composer\IO\IOInterface; |
7
|
|
|
use Composer\Repository\VcsRepository; |
8
|
|
|
use ReflectionObject; |
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
class DriverReflection |
12
|
|
|
{ |
13
|
|
|
public static function getDriverWithoutException(VcsRepository $repo, IOInterface $io, Config $config) |
14
|
|
|
{ |
15
|
|
|
$reflectedRepo = new ReflectionObject($repo); |
16
|
|
|
$drivers = static::getRepoField($repo, $reflectedRepo, 'drivers'); |
17
|
|
|
|
18
|
|
|
if (isset($drivers[static::getRepoField($repo, $reflectedRepo, 'type')])) { |
19
|
|
|
$class = $drivers[static::getRepoField($repo, $reflectedRepo, 'type')]; |
20
|
|
|
$driver = new $class($repo->getRepoConfig(), $io, $config); |
21
|
|
|
try { |
22
|
|
|
$driver->initialize(); |
23
|
|
|
} catch (RuntimeException $e) { |
24
|
|
|
// no-op - this is probably caused due to insufficient permissions when trying to create /var/www/.ssh |
25
|
|
|
// but since we're just getting the driver as a shortcut to getting the repository name, we can ignore this for now. |
26
|
|
|
} |
27
|
|
|
return $driver; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
foreach ($drivers as $driver) { |
31
|
|
|
if ($driver::supports($io, $config, static::getRepoField($repo, $reflectedRepo, 'url'))) { |
32
|
|
|
$driver = new $driver($repo->getRepoConfig(), $io, $config); |
33
|
|
|
try { |
34
|
|
|
$driver->initialize(); |
35
|
|
|
} catch (RuntimeException $e) { |
36
|
|
|
// no-op - this is probably caused due to insufficient permissions when trying to create /var/www/.ssh |
37
|
|
|
// but since we're just getting the driver as a shortcut to getting the repository name, we can ignore this for now. |
38
|
|
|
} |
39
|
|
|
return $driver; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
foreach ($drivers as $driver) { |
44
|
|
|
if ($driver::supports($io, $config, static::getRepoField($repo, $reflectedRepo, 'url'), true)) { |
45
|
|
|
$driver = new $driver($repo->getRepoConfig(), $io, $config); |
46
|
|
|
try { |
47
|
|
|
$driver->initialize(); |
48
|
|
|
} catch (RuntimeException $e) { |
49
|
|
|
// no-op - this is probably caused due to insufficient permissions when trying to create /var/www/.ssh |
50
|
|
|
// but since we're just getting the driver as a shortcut to getting the repository name, we can ignore this for now. |
51
|
|
|
} |
52
|
|
|
return $driver; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function getSshUrl($driver) |
58
|
|
|
{ |
59
|
|
|
$reflectedDriver = new ReflectionObject($driver); |
60
|
|
|
if ($reflectedDriver->hasMethod('generateSshUrl')) { |
61
|
|
|
$reflectedMethod = $reflectedDriver->getMethod('generateSshUrl'); |
62
|
|
|
$reflectedMethod->setAccessible(true); |
63
|
|
|
return $reflectedMethod->invoke($driver); |
64
|
|
|
} |
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected static function getRepoField(VcsRepository $repo, ReflectionObject $reflectedRepo, string $field) |
69
|
|
|
{ |
70
|
|
|
$reflectedUrl = $reflectedRepo->getProperty($field); |
71
|
|
|
$reflectedUrl->setAccessible(true); |
72
|
|
|
return $reflectedUrl->getValue($repo); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|