mingyoung /
easywechat-composer
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | /* |
||||
| 6 | * This file is part of the EasyWeChatComposer. |
||||
| 7 | * |
||||
| 8 | * (c) 张铭阳 <[email protected]> |
||||
| 9 | * |
||||
| 10 | * This source file is subject to the MIT license that is bundled |
||||
| 11 | * with this source code in the file LICENSE. |
||||
| 12 | */ |
||||
| 13 | |||||
| 14 | namespace EasyWeChatComposer; |
||||
| 15 | |||||
| 16 | use Composer\Plugin\PluginInterface; |
||||
| 17 | |||||
| 18 | class ManifestManager |
||||
| 19 | { |
||||
| 20 | const PACKAGE_TYPE = 'easywechat-extension'; |
||||
| 21 | |||||
| 22 | const EXTRA_OBSERVER = 'observers'; |
||||
| 23 | |||||
| 24 | /** |
||||
| 25 | * The vendor path. |
||||
| 26 | * |
||||
| 27 | * @var string |
||||
| 28 | */ |
||||
| 29 | protected $vendorPath; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * The manifest path. |
||||
| 33 | * |
||||
| 34 | * @var string |
||||
| 35 | */ |
||||
| 36 | protected $manifestPath; |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * @param string $vendorPath |
||||
| 40 | * @param string|null $manifestPath |
||||
| 41 | */ |
||||
| 42 | public function __construct(string $vendorPath, string $manifestPath = null) |
||||
| 43 | { |
||||
| 44 | $this->vendorPath = $vendorPath; |
||||
| 45 | $this->manifestPath = $manifestPath ?: $vendorPath.'/easywechat-composer/easywechat-composer/extensions.php'; |
||||
| 46 | } |
||||
| 47 | |||||
| 48 | /** |
||||
| 49 | * Remove manifest file. |
||||
| 50 | * |
||||
| 51 | * @return $this |
||||
| 52 | */ |
||||
| 53 | public function unlink() |
||||
| 54 | { |
||||
| 55 | if (file_exists($this->manifestPath)) { |
||||
| 56 | @unlink($this->manifestPath); |
||||
|
0 ignored issues
–
show
|
|||||
| 57 | } |
||||
| 58 | |||||
| 59 | return $this; |
||||
| 60 | } |
||||
| 61 | |||||
| 62 | /** |
||||
| 63 | * Build the manifest file. |
||||
| 64 | */ |
||||
| 65 | public function build() |
||||
| 66 | { |
||||
| 67 | $packages = []; |
||||
| 68 | |||||
| 69 | if (file_exists($installed = $this->vendorPath.'/composer/installed.json')) { |
||||
| 70 | $packages = json_decode(file_get_contents($installed), true); |
||||
| 71 | if (version_compare(PluginInterface::PLUGIN_API_VERSION, '2.0.0', 'ge')) { |
||||
| 72 | $packages = $packages['packages']; |
||||
| 73 | } |
||||
| 74 | } |
||||
| 75 | |||||
| 76 | $this->write($this->map($packages)); |
||||
| 77 | } |
||||
| 78 | |||||
| 79 | /** |
||||
| 80 | * @param array $packages |
||||
| 81 | * |
||||
| 82 | * @return array |
||||
| 83 | */ |
||||
| 84 | protected function map(array $packages): array |
||||
| 85 | { |
||||
| 86 | $manifest = []; |
||||
| 87 | |||||
| 88 | $packages = array_filter($packages, function ($package) { |
||||
| 89 | if(isset($package['type'])){ |
||||
| 90 | return $package['type'] === self::PACKAGE_TYPE; |
||||
| 91 | } |
||||
| 92 | }); |
||||
| 93 | |||||
| 94 | foreach ($packages as $package) { |
||||
| 95 | $manifest[$package['name']] = [self::EXTRA_OBSERVER => $package['extra'][self::EXTRA_OBSERVER] ?? []]; |
||||
| 96 | } |
||||
| 97 | |||||
| 98 | return $manifest; |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | /** |
||||
| 102 | * Write the manifest array to a file. |
||||
| 103 | * |
||||
| 104 | * @param array $manifest |
||||
| 105 | */ |
||||
| 106 | protected function write(array $manifest) |
||||
| 107 | { |
||||
| 108 | file_put_contents( |
||||
| 109 | $this->manifestPath, |
||||
| 110 | '<?php return '.var_export($manifest, true).';' |
||||
| 111 | ); |
||||
| 112 | |||||
| 113 | $this->invalidate($this->manifestPath); |
||||
| 114 | } |
||||
| 115 | |||||
| 116 | /** |
||||
| 117 | * Invalidate the given file. |
||||
| 118 | * |
||||
| 119 | * @param string $file |
||||
| 120 | */ |
||||
| 121 | protected function invalidate($file) |
||||
| 122 | { |
||||
| 123 | if (function_exists('opcache_invalidate')) { |
||||
| 124 | @opcache_invalidate($file, true); |
||||
|
0 ignored issues
–
show
It seems like you do not handle an error condition for
opcache_invalidate(). This can introduce security issues, and is generally not recommended.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
|
|||||
| 125 | } |
||||
| 126 | } |
||||
| 127 | } |
||||
| 128 |
If you suppress an error, we recommend checking for the error condition explicitly: