1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
|
//! ref: composer/src/Composer/Repository/FilesystemRepository.php
use std::any::Any;
use crate::util::Silencer;
use anyhow::Result;
use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::Preg;
use shirabe_php_shim::{
Exception, InvalidArgumentException, LogicException, PhpMixed, SORT_NATURAL,
UnexpectedValueException, array_flip, dirname, r#eval, file_get_contents, get_class,
get_class_err, get_debug_type, in_array, is_array, is_int, is_null, is_string, ksort, php_dir,
realpath, sort, sort_with_flags, str_repeat, strtr, trim, usort, var_export,
};
use crate::installed_versions::InstalledVersions;
use crate::installer::InstallationManager;
use crate::json::JsonFile;
use crate::package::PackageInterface;
use crate::package::PackageInterfaceHandle;
use crate::package::RootPackageInterface;
use crate::package::RootPackageInterfaceHandle;
use crate::package::dumper::ArrayDumper;
use crate::package::loader::ArrayLoader;
use crate::package::loader::LoaderInterface;
use crate::repository::InvalidRepositoryException;
use crate::repository::PlatformRepository;
use crate::repository::WritableArrayRepository;
use crate::util::Filesystem;
use crate::util::Platform;
/// Filesystem repository.
#[derive(Debug)]
pub struct FilesystemRepository {
pub(crate) inner: WritableArrayRepository,
/// @var JsonFile
pub(crate) file: JsonFile,
/// @var bool
dump_versions: bool,
/// @var ?RootPackageInterface
root_package: Option<RootPackageInterfaceHandle>,
/// @var Filesystem
filesystem: std::rc::Rc<std::cell::RefCell<Filesystem>>,
/// @var bool|null
dev_mode: Option<bool>,
}
impl FilesystemRepository {
/// Initializes filesystem repository.
///
/// @param JsonFile $repositoryFile repository json file
/// @param ?RootPackageInterface $rootPackage Must be provided if $dumpVersions is true
pub fn new(
repository_file: JsonFile,
dump_versions: bool,
root_package: Option<RootPackageInterfaceHandle>,
filesystem: Option<std::rc::Rc<std::cell::RefCell<Filesystem>>>,
) -> Result<Self> {
let filesystem = filesystem
.unwrap_or_else(|| std::rc::Rc::new(std::cell::RefCell::new(Filesystem::new(None))));
if dump_versions && root_package.is_none() {
return Err(InvalidArgumentException {
message: "Expected a root package instance if $dumpVersions is true".to_string(),
code: 0,
}
.into());
}
Ok(Self {
inner: WritableArrayRepository::new(vec![])?,
file: repository_file,
dump_versions,
root_package,
filesystem,
dev_mode: None,
})
}
/// @return bool|null true if dev requirements were installed, false if --no-dev was used, null if yet unknown
pub fn get_dev_mode(&self) -> Option<bool> {
self.dev_mode
}
pub fn get_repo_name(&self) -> String {
format!("file ({})", self.file.get_path())
}
/// Initializes repository (reads file, or remote address).
pub(crate) fn initialize(&mut self) -> Result<()> {
self.inner.initialize();
if !self.file.exists() {
return Ok(());
}
// TODO(phase-b): use anyhow::Result<Result<T, E>> to model PHP try/catch
let packages: PhpMixed = match (|| -> Result<PhpMixed> {
let data = self.file.read()?;
let packages_value = if let PhpMixed::Array(ref m) = data {
if m.contains_key("packages") {
(*m.get("packages").unwrap().clone()).clone()
} else {
data.clone()
}
} else {
data.clone()
};
if let PhpMixed::Array(ref m) = data {
if let Some(names) = m.get("dev-package-names") {
let dev_names: Vec<String> = names
.as_list()
.map(|l| {
l.iter()
.filter_map(|v| v.as_string().map(|s| s.to_string()))
.collect()
})
.unwrap_or_default();
self.inner.set_dev_package_names(dev_names);
}
if let Some(dev) = m.get("dev") {
self.dev_mode = dev.as_bool();
}
}
if !is_array(&packages_value) {
return Err(UnexpectedValueException {
message: "Could not parse package list from the repository".to_string(),
code: 0,
}
.into());
}
Ok(packages_value)
})() {
Ok(p) => p,
Err(e) => {
return Err(InvalidRepositoryException(Exception {
message: format!(
"Invalid repository data in {}, packages could not be loaded: [{}] {}",
self.file.get_path(),
get_class_err(&e),
e,
),
code: 0,
})
.into());
}
};
let mut loader = ArrayLoader::new(None, true);
if let Some(packages_list) = packages.as_list() {
for package_data in packages_list.iter() {
// TODO(phase-b): expected IndexMap<String, PhpMixed> but package_data is PhpMixed.
let cfg = (**package_data)
.as_array()
.cloned()
.map(|m| {
m.into_iter()
.map(|(k, v)| (k, *v))
.collect::<IndexMap<String, PhpMixed>>()
})
.unwrap_or_default();
let package =
loader.load(cfg, Some("Composer\\Package\\CompletePackage".to_string()))?;
self.inner.add_package(package)?;
}
} else if let Some(packages_array) = packages.as_array() {
for (_, package_data) in packages_array.iter() {
// TODO(phase-b): expected IndexMap<String, PhpMixed> but package_data is PhpMixed.
let cfg = (**package_data)
.as_array()
.cloned()
.map(|m| {
m.into_iter()
.map(|(k, v)| (k, *v))
.collect::<IndexMap<String, PhpMixed>>()
})
.unwrap_or_default();
let package =
loader.load(cfg, Some("Composer\\Package\\CompletePackage".to_string()))?;
self.inner.add_package(package)?;
}
}
Ok(())
}
pub fn reload(&mut self) -> Result<()> {
// TODO(phase-b): clear inner packages cache (PHP: $this->packages = null)
self.inner.reload();
self.initialize()
}
/// Writes writable repository.
pub fn write(
&mut self,
dev_mode: bool,
installation_manager: &mut InstallationManager,
) -> Result<()> {
let mut data: IndexMap<String, PhpMixed> = IndexMap::new();
data.insert("packages".to_string(), PhpMixed::List(vec![]));
data.insert("dev".to_string(), PhpMixed::Bool(dev_mode));
data.insert("dev-package-names".to_string(), PhpMixed::List(vec![]));
let dumper = ArrayDumper::new();
// make sure the directory is created so we can realpath it
// as realpath() does some additional normalizations with network paths that normalizePath does not
// and we need to find shortest path correctly
let repo_dir = dirname(self.file.get_path());
self.filesystem
.borrow_mut()
.ensure_directory_exists(&repo_dir);
let repo_dir = self
.filesystem
.borrow()
.normalize_path(&realpath(&repo_dir).unwrap_or_default());
let mut install_paths: IndexMap<String, Option<String>> = IndexMap::new();
for package in self.inner.get_canonical_packages() {
let mut pkg_array = dumper.dump(package.as_rc().borrow().as_package_interface());
let path = installation_manager
.get_install_path(package.as_rc().borrow().as_package_interface());
let mut install_path: Option<String> = None;
if let Some(path_str) = &path {
if !path_str.is_empty() {
let normalized_path = self.filesystem.borrow_mut().normalize_path(&if self
.filesystem
.borrow()
.is_absolute_path(path_str)
{
path_str.clone()
} else {
format!(
"{}/{}",
Platform::get_cwd(false).unwrap_or_default(),
path_str
)
});
install_path = Some(self.filesystem.borrow_mut().find_shortest_path(
&repo_dir,
&normalized_path,
true,
false,
));
}
}
install_paths.insert(package.get_name().to_string(), install_path.clone());
pkg_array.insert(
"install-path".to_string(),
match install_path {
Some(s) => PhpMixed::String(s),
None => PhpMixed::Null,
},
);
if let Some(PhpMixed::List(list)) = data.get_mut("packages") {
list.push(Box::new(PhpMixed::Array(
pkg_array
.into_iter()
.map(|(k, v)| (k, Box::new(v)))
.collect(),
)));
}
// only write to the files the names which are really installed, as we receive the full list
// of dev package names before they get installed during composer install
if in_array(
PhpMixed::String(package.get_name().to_string()),
&PhpMixed::List(
self.inner
.dev_package_names
.iter()
.map(|s| Box::new(PhpMixed::String(s.clone())))
.collect(),
),
true,
) {
if let Some(PhpMixed::List(list)) = data.get_mut("dev-package-names") {
list.push(Box::new(PhpMixed::String(package.get_name().to_string())));
}
}
}
// PHP: sort($data['dev-package-names']);
if let Some(PhpMixed::List(_list)) = data.get_mut("dev-package-names") {
// TODO(phase-b): sort PhpMixed::List in-place using string comparison; PhpMixed: !Ord.
}
// PHP: usort($data['packages'], static function ($a, $b): int { return strcmp($a['name'], $b['name']); });
if let Some(PhpMixed::List(list)) = data.get_mut("packages") {
usort(list, |a: &Box<PhpMixed>, b: &Box<PhpMixed>| -> i64 {
let a_name = a
.as_array()
.and_then(|m| m.get("name"))
.and_then(|v| v.as_string())
.unwrap_or("");
let b_name = b
.as_array()
.and_then(|m| m.get("name"))
.and_then(|v| v.as_string())
.unwrap_or("");
shirabe_php_shim::strcmp(a_name, b_name)
});
}
self.file.write(PhpMixed::Array(
data.clone()
.into_iter()
.map(|(k, v)| (k, Box::new(v)))
.collect(),
))?;
if self.dump_versions {
let versions = self.generate_installed_versions(
installation_manager,
&install_paths,
dev_mode,
&repo_dir,
)?;
self.filesystem.borrow_mut().file_put_contents_if_modified(
&format!("{}/installed.php", repo_dir),
&format!("<?php return {};\n", self.dump_to_php_code(&versions, 0),),
);
let installed_versions_class =
file_get_contents(&format!("{}/../InstalledVersions.php", php_dir(),));
// this normally should not happen but during upgrades of Composer when it is installed in the project it is a possibility
if let Some(class_content) = installed_versions_class {
self.filesystem.borrow_mut().file_put_contents_if_modified(
&format!("{}/InstalledVersions.php", repo_dir),
&class_content,
);
// make sure the in memory state is up to date with on disk
InstalledVersions::reload(versions);
// make sure the selfDir matches the expected data at runtime if the class was loaded from the vendor dir, as it may have been
// loaded from the Composer sources, causing packages to appear twice in that case if the installed.php is loaded in addition to the
// in memory loaded data from above
// TODO(phase-b): Reflection API on static properties — confirm porting approach with user
let _attempt: Result<()> = (|| -> Result<()> {
todo!(
"ReflectionProperty(Composer\\InstalledVersions::class, 'selfDir')->setValue(null, strtr($repoDir, '\\\\', '/'))"
);
// (the second reflection block sets installedIsLocalDir = true)
})();
// PHP: catches \ReflectionException and rethrows if not "Property does not exist"
}
}
Ok(())
}
/// As we load the file from vendor dir during bootstrap, we need to make sure it contains only expected code before executing it
///
/// @internal
pub fn safely_load_installed_versions(path: &str) -> bool {
// PHP: @file_get_contents($path)
let installed_versions_data = Silencer::call(|| Ok(file_get_contents(path)))
.ok()
.flatten();
let pattern = "{(?(DEFINE)\n (?<number> -? \\s*+ \\d++ (?:\\.\\d++)? )\n (?<boolean> true | false | null )\n (?<strings> (?&string) (?: \\s*+ \\. \\s*+ (?&string))*+ )\n (?<string> (?: \" (?:[^\"\\\\$]*+ | \\\\ [\"\\\\0] )* \" | ' (?:[^'\\\\]*+ | \\\\ ['\\\\] )* ' ) )\n (?<array> array\\( \\s*+ (?: (?:(?&number)|(?&strings)) \\s*+ => \\s*+ (?: (?:__DIR__ \\s*+ \\. \\s*+)? (?&strings) | (?&value) ) \\s*+, \\s*+ )*+ \\s*+ \\) )\n (?<value> (?: (?&number) | (?&boolean) | (?&strings) | (?&array) ) )\n)\n^<\\?php\\s++return\\s++(?&array)\\s*+;$}ix";
if let Some(data) = installed_versions_data {
let mixed = PhpMixed::String(data.clone());
if is_string(&mixed) && Preg::is_match(pattern, &trim(&data, None)).unwrap_or(false) {
let replaced = Preg::replace(
r#"{=>\s*+__DIR__\s*+\.\s*+(['\"])}"#,
&format!(
"=> {} . $1",
var_export(&PhpMixed::String(dirname(path)), true),
),
&data,
);
let replaced = match replaced {
Ok(s) => s,
Err(_) => return false,
};
let evaluated = r#eval(&format!("?>{}", replaced));
InstalledVersions::reload(
evaluated
.as_array()
.cloned()
.map(|m| m.into_iter().map(|(k, v)| (k, *v)).collect())
.unwrap_or_default(),
);
return true;
}
}
false
}
/// @param array<mixed> $array
fn dump_to_php_code(&self, array: &IndexMap<String, PhpMixed>, level: i64) -> String {
let mut lines = String::from("array(\n");
let level = level + 1;
for (key, value) in array {
lines.push_str(&str_repeat(" ", level as usize));
lines.push_str(&if is_int(&PhpMixed::String(key.clone())) {
// TODO(phase-b): PHP integer-keyed array entries — IndexMap keys are strings
format!("{} => ", key)
} else {
format!("{} => ", var_export(&PhpMixed::String(key.clone()), true))
});
if is_array(value) {
if let Some(inner_arr) = value.as_array() {
if !inner_arr.is_empty() {
let inner_map: IndexMap<String, PhpMixed> = inner_arr
.iter()
.map(|(k, v)| (k.clone(), (**v).clone()))
.collect();
lines.push_str(&self.dump_to_php_code(&inner_map, level));
} else {
lines.push_str("array(),\n");
}
} else if let Some(list) = value.as_list() {
if !list.is_empty() {
let inner_map: IndexMap<String, PhpMixed> = list
.iter()
.enumerate()
.map(|(i, v)| (i.to_string(), (**v).clone()))
.collect();
lines.push_str(&self.dump_to_php_code(&inner_map, level));
} else {
lines.push_str("array(),\n");
}
}
} else if key == "install_path" && is_string(value) {
let s = value.as_string().unwrap_or("").to_string();
if self.filesystem.borrow_mut().is_absolute_path(&s) {
lines.push_str(&format!("{},\n", var_export(&PhpMixed::String(s), true),));
} else {
lines.push_str(&format!(
"__DIR__ . {},\n",
var_export(&PhpMixed::String(format!("/{}", s)), true),
));
}
} else if is_string(value) {
lines.push_str(&format!("{},\n", var_export(value, true)));
} else if let PhpMixed::Bool(b) = value {
lines.push_str(&format!("{},\n", if *b { "true" } else { "false" }));
} else if is_null(value) {
lines.push_str("null,\n");
} else {
// PHP: throw new \UnexpectedValueException('Unexpected type '.get_debug_type($value));
panic!("{}", format!("Unexpected type {}", get_debug_type(value)));
}
}
lines.push_str(&format!(
"{}){}",
str_repeat(" ", (level - 1) as usize),
if (level - 1) == 0 { "" } else { ",\n" },
));
lines
}
/// @param array<string, string> $installPaths
fn generate_installed_versions(
&self,
installation_manager: &InstallationManager,
install_paths: &IndexMap<String, Option<String>>,
dev_mode: bool,
repo_dir: &str,
) -> Result<IndexMap<String, PhpMixed>> {
let dev_packages = array_flip(&PhpMixed::List(
self.inner
.dev_package_names
.iter()
.map(|s| Box::new(PhpMixed::String(s.clone())))
.collect(),
));
let mut packages: Vec<PackageInterfaceHandle> = self
.inner
.get_packages()
.into_iter()
.map(|p| p.into())
.collect();
let mut current_root: RootPackageInterfaceHandle = match &self.root_package {
None => {
return Err(LogicException {
message:
"It should not be possible to dump packages if no root package is given"
.to_string(),
code: 0,
}
.into());
}
Some(r) => r.clone(),
};
// packages[] = $rootPackage = $this->rootPackage;
packages.push(current_root.clone().into());
while let Some(root_alias) =
PackageInterfaceHandle::from(current_root.clone()).as_root_alias_package()
{
current_root = root_alias.get_alias_of().into();
packages.push(current_root.clone().into());
}
let mut versions: IndexMap<String, PhpMixed> = IndexMap::new();
versions.insert(
"root".to_string(),
PhpMixed::Array(
self.dump_root_package(
current_root
.as_rc()
.borrow()
.as_root_package_interface()
.expect("current_root is a RootPackageInterface"),
install_paths,
dev_mode,
repo_dir,
&dev_packages,
)
.into_iter()
.map(|(k, v)| (k, Box::new(v)))
.collect(),
),
);
versions.insert("versions".to_string(), PhpMixed::Array(IndexMap::new()));
// add real installed packages
for package in &packages {
if package.as_alias().is_some() {
continue;
}
let dumped = self.dump_installed_package(
package.as_rc().borrow().as_package_interface(),
install_paths,
repo_dir,
&dev_packages,
);
if let Some(PhpMixed::Array(versions_map)) = versions.get_mut("versions") {
versions_map.insert(
package.get_name().to_string(),
Box::new(PhpMixed::Array(
dumped.into_iter().map(|(k, v)| (k, Box::new(v))).collect(),
)),
);
}
}
// add provided/replaced packages
for package in &packages {
let is_dev_package = dev_packages
.as_array()
.map(|m| m.contains_key(&package.get_name()))
.unwrap_or(false);
for (_, replace) in package.get_replaces() {
// exclude platform replaces as when they are really there we can not check for their presence
if PlatformRepository::is_platform_package(replace.get_target()) {
continue;
}
// PHP: dev_requirement handling
// TODO(phase-b): mutate nested versions['versions'][$replace->getTarget()]['dev_requirement']
todo!("mutate nested versions['versions'][target]['dev_requirement']");
#[allow(unreachable_code)]
{
let mut replaced = replace.get_pretty_constraint().unwrap_or("").to_string();
if replaced == "self.version" {
replaced = package.get_pretty_version().to_string();
}
// TODO(phase-b): mutate nested versions['versions'][$replace->getTarget()]['replaced']
todo!("append replaced to versions['versions'][target]['replaced']");
}
}
for (_, provide) in package.get_provides() {
// exclude platform provides as when they are really there we can not check for their presence
if PlatformRepository::is_platform_package(provide.get_target()) {
continue;
}
// TODO(phase-b): mutate nested versions['versions'][$provide->getTarget()]['dev_requirement']
todo!("mutate nested versions['versions'][target]['dev_requirement']");
#[allow(unreachable_code)]
{
let mut provided = provide.get_pretty_constraint().unwrap_or("").to_string();
if provided == "self.version" {
provided = package.get_pretty_version().to_string();
}
// TODO(phase-b): mutate nested versions['versions'][$provide->getTarget()]['provided']
todo!("append provided to versions['versions'][target]['provided']");
}
}
}
// add aliases
for package in &packages {
let Some(_alias) = package.as_alias() else {
continue;
};
// TODO(phase-b): mutate nested versions['versions'][name]['aliases']
todo!("append alias->getPrettyVersion() to versions['versions'][name]['aliases']");
#[allow(unreachable_code)]
if package.as_root().is_some() {
// TODO(phase-b): same mutation on versions['root']['aliases']
todo!("append alias->getPrettyVersion() to versions['root']['aliases']");
}
}
if let Some(PhpMixed::Array(versions_map)) = versions.get_mut("versions") {
// TODO(phase-b): ksort signature mismatch on nested IndexMap; cast appropriately
ksort(versions_map);
}
ksort(&mut versions);
if let Some(PhpMixed::Array(versions_map)) = versions.get_mut("versions") {
for (_name, version) in versions_map.iter_mut() {
if let PhpMixed::Array(version_map) = version.as_mut() {
for key in ["aliases", "replaced", "provided"] {
if let Some(boxed) = version_map.get_mut(key) {
if let PhpMixed::List(_list) = boxed.as_mut() {
// PHP: sort($versions['versions'][$name][$key], SORT_NATURAL);
// TODO(phase-b): PhpMixed lacks Ord; needs custom comparator.
}
}
}
}
}
}
Ok(versions)
}
/// @param array<string, string> $installPaths
/// @param array<string, int> $devPackages
/// @return array{pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev_requirement: bool}
fn dump_installed_package(
&self,
package: &dyn PackageInterface,
install_paths: &IndexMap<String, Option<String>>,
repo_dir: &str,
dev_packages: &PhpMixed,
) -> IndexMap<String, PhpMixed> {
let mut reference: Option<String> = None;
if let Some(install_src) = package.get_installation_source() {
reference = if install_src == "source" {
package.get_source_reference().map(String::from)
} else {
package.get_dist_reference().map(String::from)
};
}
if reference.is_none() {
// PHP: ($package->getSourceReference() ?: $package->getDistReference()) ?: null;
let source = package.get_source_reference().unwrap_or("");
let dist = package.get_dist_reference().unwrap_or("");
let combined = if !source.is_empty() {
source.to_string()
} else {
dist.to_string()
};
reference = if combined.is_empty() {
None
} else {
Some(combined)
};
}
let install_path = if package.as_root_package_interface().is_some() {
let to = self.filesystem.borrow_mut().normalize_path(
&realpath(&Platform::get_cwd(false).unwrap_or_default()).unwrap_or_default(),
);
Some(
self.filesystem
.borrow_mut()
.find_shortest_path(repo_dir, &to, true, false),
)
} else {
install_paths.get(package.get_name()).cloned().flatten()
};
let mut data: IndexMap<String, PhpMixed> = IndexMap::new();
data.insert(
"pretty_version".to_string(),
PhpMixed::String(package.get_pretty_version().to_string()),
);
data.insert(
"version".to_string(),
PhpMixed::String(package.get_version().to_string()),
);
data.insert(
"reference".to_string(),
match reference {
Some(s) => PhpMixed::String(s),
None => PhpMixed::Null,
},
);
data.insert(
"type".to_string(),
PhpMixed::String(package.get_type().to_string()),
);
data.insert(
"install_path".to_string(),
match install_path {
Some(s) => PhpMixed::String(s),
None => PhpMixed::Null,
},
);
data.insert("aliases".to_string(), PhpMixed::List(vec![]));
data.insert(
"dev_requirement".to_string(),
PhpMixed::Bool(
dev_packages
.as_array()
.map(|m| m.contains_key(package.get_name()))
.unwrap_or(false),
),
);
data
}
/// @param array<string, string> $installPaths
/// @param array<string, int> $devPackages
/// @return array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}
fn dump_root_package(
&self,
package: &dyn RootPackageInterface,
install_paths: &IndexMap<String, Option<String>>,
dev_mode: bool,
repo_dir: &str,
dev_packages: &PhpMixed,
) -> IndexMap<String, PhpMixed> {
let data = self.dump_installed_package(package, install_paths, repo_dir, dev_packages);
let mut result: IndexMap<String, PhpMixed> = IndexMap::new();
result.insert(
"name".to_string(),
PhpMixed::String(package.get_name().to_string()),
);
result.insert(
"pretty_version".to_string(),
data.get("pretty_version")
.cloned()
.unwrap_or(PhpMixed::Null),
);
result.insert(
"version".to_string(),
data.get("version").cloned().unwrap_or(PhpMixed::Null),
);
result.insert(
"reference".to_string(),
data.get("reference").cloned().unwrap_or(PhpMixed::Null),
);
result.insert(
"type".to_string(),
data.get("type").cloned().unwrap_or(PhpMixed::Null),
);
result.insert(
"install_path".to_string(),
data.get("install_path").cloned().unwrap_or(PhpMixed::Null),
);
result.insert(
"aliases".to_string(),
data.get("aliases")
.cloned()
.unwrap_or(PhpMixed::List(vec![])),
);
result.insert("dev".to_string(), PhpMixed::Bool(dev_mode));
result
}
}
|