diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-09 06:19:30 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-09 06:19:30 +0900 |
| commit | 880a5eaad9dcdfd31563385f11ba1f63d38cfd14 (patch) | |
| tree | 6f9de7b2b8331628a74287112711f65ae3eddc52 /crates/shirabe/src | |
| parent | f3d60c7836da0d50d59a22cfd6e4692e3dc75581 (diff) | |
| download | php-shirabe-880a5eaad9dcdfd31563385f11ba1f63d38cfd14.tar.gz php-shirabe-880a5eaad9dcdfd31563385f11ba1f63d38cfd14.tar.zst php-shirabe-880a5eaad9dcdfd31563385f11ba1f63d38cfd14.zip | |
refactor(php-shim): use std::path::MAIN_SEPARATOR over a shim constant
The shim's DIRECTORY_SEPARATOR was hardcoded to "/", so every ported
`'\\' === DIRECTORY_SEPARATOR` check compared against a constant that
does not track the target platform. std::path::MAIN_SEPARATOR and
MAIN_SEPARATOR_STR carry the same meaning as PHP's constant and resolve
per platform, so the Windows branches are selected on Windows targets.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src')
| -rw-r--r-- | crates/shirabe/src/autoload/class_loader.rs | 28 | ||||
| -rw-r--r-- | crates/shirabe/src/command/create_project_command.rs | 10 | ||||
| -rw-r--r-- | crates/shirabe/src/downloader/archive_downloader.rs | 15 | ||||
| -rw-r--r-- | crates/shirabe/src/downloader/file_downloader.rs | 17 | ||||
| -rw-r--r-- | crates/shirabe/src/downloader/gzip_downloader.rs | 7 | ||||
| -rw-r--r-- | crates/shirabe/src/downloader/path_downloader.rs | 10 | ||||
| -rw-r--r-- | crates/shirabe/src/downloader/zip_downloader.rs | 12 | ||||
| -rw-r--r-- | crates/shirabe/src/repository/path_repository.rs | 6 | ||||
| -rw-r--r-- | crates/shirabe/src/util/filesystem.rs | 27 |
9 files changed, 71 insertions, 61 deletions
diff --git a/crates/shirabe/src/autoload/class_loader.rs b/crates/shirabe/src/autoload/class_loader.rs index 47b6ee4c..21473fb9 100644 --- a/crates/shirabe/src/autoload/class_loader.rs +++ b/crates/shirabe/src/autoload/class_loader.rs @@ -2,9 +2,8 @@ use indexmap::IndexMap; use shirabe_php_shim::{ - DIRECTORY_SEPARATOR, InvalidArgumentException, PhpMixed, defined, file_exists, include_file, - spl_autoload_register, spl_autoload_unregister, stream_resolve_include_path, strlen, strpos, - strrpos, strtr, substr, + InvalidArgumentException, PhpMixed, defined, file_exists, include_file, spl_autoload_register, + spl_autoload_unregister, stream_resolve_include_path, strlen, strpos, strrpos, strtr, substr, }; use std::sync::{LazyLock, Mutex}; @@ -366,7 +365,11 @@ impl ClassLoader { fn find_file_with_extension(&self, class: &str, ext: &str) -> Option<String> { // PSR-4 lookup - let logical_path_psr4 = format!("{}{}", strtr(class, "\\", DIRECTORY_SEPARATOR), ext); + let logical_path_psr4 = format!( + "{}{}", + strtr(class, "\\", std::path::MAIN_SEPARATOR_STR), + ext + ); let first = class.chars().next().unwrap_or('\0').to_string(); if self.prefix_lengths_psr4.contains_key(&first) { @@ -382,7 +385,7 @@ impl ClassLoader { if let Some(dirs) = self.prefix_dirs_psr4.get(&search) { let path_end = format!( "{}{}", - DIRECTORY_SEPARATOR, + std::path::MAIN_SEPARATOR, substr(&logical_path_psr4, (last_pos + 1) as i64, None) ); for dir in dirs { @@ -397,7 +400,7 @@ impl ClassLoader { // PSR-4 fallback dirs for dir in &self.fallback_dirs_psr4 { - let file = format!("{}{}{}", dir, DIRECTORY_SEPARATOR, logical_path_psr4); + let file = format!("{}{}{}", dir, std::path::MAIN_SEPARATOR, logical_path_psr4); if file_exists(&file) { return Some(file); } @@ -413,19 +416,24 @@ impl ClassLoader { strtr( &substr(&logical_path_psr4, (pos + 1) as i64, None), "_", - DIRECTORY_SEPARATOR + std::path::MAIN_SEPARATOR_STR, ) ); } else { // PEAR-like class name - logical_path_psr0 = format!("{}{}", strtr(class, "_", DIRECTORY_SEPARATOR), ext); + logical_path_psr0 = format!( + "{}{}", + strtr(class, "_", std::path::MAIN_SEPARATOR_STR), + ext + ); } if let Some(prefixes) = self.prefixes_psr0.get(&first) { for (prefix, dirs) in prefixes { if Some(0) == strpos(class, prefix) { for dir in dirs { - let file = format!("{}{}{}", dir, DIRECTORY_SEPARATOR, logical_path_psr0); + let file = + format!("{}{}{}", dir, std::path::MAIN_SEPARATOR, logical_path_psr0); if file_exists(&file) { return Some(file); } @@ -436,7 +444,7 @@ impl ClassLoader { // PSR-0 fallback dirs for dir in &self.fallback_dirs_psr0 { - let file = format!("{}{}{}", dir, DIRECTORY_SEPARATOR, logical_path_psr0); + let file = format!("{}{}{}", dir, std::path::MAIN_SEPARATOR, logical_path_psr0); if file_exists(&file) { return Some(file); } diff --git a/crates/shirabe/src/command/create_project_command.rs b/crates/shirabe/src/command/create_project_command.rs index 267c866e..3849d93d 100644 --- a/crates/shirabe/src/command/create_project_command.rs +++ b/crates/shirabe/src/command/create_project_command.rs @@ -44,9 +44,9 @@ use shirabe_external_packages::symfony::console::output::OutputInterface; use shirabe_external_packages::symfony::finder::Finder; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ - DIRECTORY_SEPARATOR, InvalidArgumentException, PhpMixed, RuntimeException, - UnexpectedValueException, array_pop, chdir, explode_with_limit, file_exists, getcwd, - impl_php_class, implode, is_dir, is_file, mkdir, realpath, rtrim, strtolower, unlink, + InvalidArgumentException, PhpMixed, RuntimeException, UnexpectedValueException, array_pop, + chdir, explode_with_limit, file_exists, getcwd, impl_php_class, implode, is_dir, is_file, + mkdir, realpath, rtrim, strtolower, unlink, }; use std::path::PathBuf; @@ -461,7 +461,7 @@ impl CreateProjectCommand { format!( "{}{}{}", Platform::get_cwd(false)?, - DIRECTORY_SEPARATOR, + std::path::MAIN_SEPARATOR, array_pop(&mut parts).unwrap_or_default() ) } @@ -477,7 +477,7 @@ impl CreateProjectCommand { directory = format!( "{}{}{}", Platform::get_cwd(false)?, - DIRECTORY_SEPARATOR, + std::path::MAIN_SEPARATOR, directory ); } diff --git a/crates/shirabe/src/downloader/archive_downloader.rs b/crates/shirabe/src/downloader/archive_downloader.rs index d2ee4a75..097fc1af 100644 --- a/crates/shirabe/src/downloader/archive_downloader.rs +++ b/crates/shirabe/src/downloader/archive_downloader.rs @@ -11,8 +11,7 @@ use indexmap::IndexMap; use shirabe_external_packages::symfony::finder::Finder; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ - DIRECTORY_SEPARATOR, PhpMixed, RuntimeException, bin2hex, file_exists, is_dir, random_bytes, - realpath, + PhpMixed, RuntimeException, bin2hex, file_exists, is_dir, random_bytes, realpath, }; use std::path::{Path, PathBuf}; @@ -92,13 +91,11 @@ pub trait ArchiveDownloader { .filesystem .borrow() .normalize_path(&vendor_dir) - .contains( - &self - .inner() - .filesystem - .borrow() - .normalize_path(&format!("{}{}", path, DIRECTORY_SEPARATOR)), - ) + .contains(&self.inner().filesystem.borrow().normalize_path(&format!( + "{}{}", + path, + std::path::MAIN_SEPARATOR + ))) { self.inner() .filesystem diff --git a/crates/shirabe/src/downloader/file_downloader.rs b/crates/shirabe/src/downloader/file_downloader.rs index 9b86ee56..ab9c3672 100644 --- a/crates/shirabe/src/downloader/file_downloader.rs +++ b/crates/shirabe/src/downloader/file_downloader.rs @@ -27,10 +27,10 @@ use crate::util::sync_executor; use indexmap::IndexMap; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ - DIRECTORY_SEPARATOR, InvalidArgumentException, PATHINFO_BASENAME, PATHINFO_EXTENSION, - PHP_URL_PATH, PhpMixed, RuntimeException, UnexpectedValueException, array_search, file_exists, - filesize, get_class, hash, hash_file, impl_php_class, is_dir, is_executable, parse_url, - pathinfo, realpath, rtrim, spl_object_hash, strlen, strpos, strtr, trim, umask, usleep, + InvalidArgumentException, PATHINFO_BASENAME, PATHINFO_EXTENSION, PHP_URL_PATH, PhpMixed, + RuntimeException, UnexpectedValueException, array_search, file_exists, filesize, get_class, + hash, hash_file, impl_php_class, is_dir, is_executable, parse_url, pathinfo, realpath, rtrim, + spl_object_hash, strlen, strpos, strtr, trim, umask, usleep, }; use std::sync::{LazyLock, Mutex}; @@ -744,10 +744,11 @@ impl DownloaderInterface for FileDownloader { // but in that case we ensure the directory is empty already in ProjectInstaller so no need to empty it here. if !{ let normalized_vendor = self.filesystem.borrow_mut().normalize_path(&vendor_dir); - let normalized_path = self - .filesystem - .borrow() - .normalize_path(&format!("{}{}", path, DIRECTORY_SEPARATOR)); + let normalized_path = self.filesystem.borrow().normalize_path(&format!( + "{}{}", + path, + std::path::MAIN_SEPARATOR + )); strpos(&normalized_vendor, &normalized_path).is_some() } { self.filesystem.borrow_mut().empty_directory(path, true)?; diff --git a/crates/shirabe/src/downloader/gzip_downloader.rs b/crates/shirabe/src/downloader/gzip_downloader.rs index 9473deff..3ddca2a4 100644 --- a/crates/shirabe/src/downloader/gzip_downloader.rs +++ b/crates/shirabe/src/downloader/gzip_downloader.rs @@ -14,9 +14,8 @@ use crate::util::Platform; use crate::util::ProcessExecutor; use indexmap::IndexMap; use shirabe_php_shim::{ - DIRECTORY_SEPARATOR, PATHINFO_FILENAME, PHP_URL_PATH, PhpMixed, RuntimeException, - extension_loaded, fclose, fopen, fwrite, gzclose, gzopen, gzread, impl_php_class, implode, - parse_url, pathinfo, strtr, + PATHINFO_FILENAME, PHP_URL_PATH, PhpMixed, RuntimeException, extension_loaded, fclose, fopen, + fwrite, gzclose, gzopen, gzread, impl_php_class, implode, parse_url, pathinfo, strtr, }; #[derive(Debug)] @@ -91,7 +90,7 @@ impl ArchiveDownloader for GzipDownloader { .unwrap_or(""), PATHINFO_FILENAME, ); - let target_filepath = format!("{}{}{}", path, DIRECTORY_SEPARATOR, filename); + let target_filepath = format!("{}{}{}", path, std::path::MAIN_SEPARATOR, filename); if !Platform::is_windows() { let command = vec![ diff --git a/crates/shirabe/src/downloader/path_downloader.rs b/crates/shirabe/src/downloader/path_downloader.rs index 9449e74a..4be835cd 100644 --- a/crates/shirabe/src/downloader/path_downloader.rs +++ b/crates/shirabe/src/downloader/path_downloader.rs @@ -23,8 +23,8 @@ use crate::util::ProcessExecutor; use indexmap::IndexMap; use shirabe_external_packages::symfony::filesystem::Filesystem as SymfonyFilesystem; use shirabe_php_shim::{ - DIRECTORY_SEPARATOR, PHP_WINDOWS_VERSION_MAJOR, PHP_WINDOWS_VERSION_MINOR, PhpMixed, - RuntimeException, file_exists, function_exists, impl_php_class, is_dir, realpath, + PHP_WINDOWS_VERSION_MAJOR, PHP_WINDOWS_VERSION_MINOR, PhpMixed, RuntimeException, file_exists, + function_exists, impl_php_class, is_dir, realpath, }; #[derive(Debug)] @@ -261,9 +261,9 @@ impl DownloaderInterface for PathDownloader { if format!( "{}{}", realpath(&path).unwrap_or_default(), - DIRECTORY_SEPARATOR + std::path::MAIN_SEPARATOR ) - .starts_with(&format!("{}{}", real_url, DIRECTORY_SEPARATOR)) + .starts_with(&format!("{}{}", real_url, std::path::MAIN_SEPARATOR)) { // IMPORTANT NOTICE: If you wish to change this, don't. You are wasting your time and ours. // @@ -383,7 +383,7 @@ impl DownloaderInterface for PathDownloader { format!( "{}{}{}", Platform::get_cwd(false)?, - DIRECTORY_SEPARATOR, + std::path::MAIN_SEPARATOR, path ) } else { diff --git a/crates/shirabe/src/downloader/zip_downloader.rs b/crates/shirabe/src/downloader/zip_downloader.rs index c481f13f..bc565ea0 100644 --- a/crates/shirabe/src/downloader/zip_downloader.rs +++ b/crates/shirabe/src/downloader/zip_downloader.rs @@ -12,10 +12,10 @@ use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; use shirabe_external_packages::symfony::process::ExecutableFinder; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ - CmpOp, DIRECTORY_SEPARATOR, ErrorException, PhpMixed, RuntimeException, - UnexpectedValueException, ZipArchive, bin2hex, class_exists, file_exists, file_get_contents, - filesize, function_exists, hash_file, impl_php_class, is_file, json_encode, php_regex, - random_int, str_contains, str_replace, strlen, substr, version_compare, + CmpOp, ErrorException, PhpMixed, RuntimeException, UnexpectedValueException, ZipArchive, + bin2hex, class_exists, file_exists, file_get_contents, filesize, function_exists, hash_file, + impl_php_class, is_file, json_encode, php_regex, random_int, str_contains, str_replace, strlen, + substr, version_compare, }; use std::sync::Mutex; @@ -84,8 +84,8 @@ impl ZipDownloader { let map: IndexMap<&str, String> = [ // normalize separators to backslashes to avoid problems with 7-zip on windows // see https://github.com/composer/composer/issues/10058 - ("%file%", file.replace('/', DIRECTORY_SEPARATOR)), - ("%path%", path.replace('/', DIRECTORY_SEPARATOR)), + ("%file%", file.replace('/', std::path::MAIN_SEPARATOR_STR)), + ("%path%", path.replace('/', std::path::MAIN_SEPARATOR_STR)), ] .into_iter() .collect(); diff --git a/crates/shirabe/src/repository/path_repository.rs b/crates/shirabe/src/repository/path_repository.rs index 46b7f95f..13c512aa 100644 --- a/crates/shirabe/src/repository/path_repository.rs +++ b/crates/shirabe/src/repository/path_repository.rs @@ -25,8 +25,8 @@ use crate::util::Url; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::Preg; use shirabe_php_shim::{ - DIRECTORY_SEPARATOR, GLOB_BRACE, GLOB_MARK, GLOB_ONLYDIR, PhpMixed, RuntimeException, defined, - file_exists, file_get_contents, glob_with_flags, hash, php_regex, realpath, serialize, + GLOB_BRACE, GLOB_MARK, GLOB_ONLYDIR, PhpMixed, RuntimeException, defined, file_exists, + file_get_contents, glob_with_flags, hash, php_regex, realpath, serialize, }; #[derive(Debug)] @@ -378,7 +378,7 @@ impl PathRepository { Ok(glob_with_flags(&self.url, flags) .into_iter() .map(|val| { - val.replace(DIRECTORY_SEPARATOR, "/") + val.replace(std::path::MAIN_SEPARATOR, "/") .trim_end_matches('/') .to_string() }) diff --git a/crates/shirabe/src/util/filesystem.rs b/crates/shirabe/src/util/filesystem.rs index 7010fd86..a12c6d64 100644 --- a/crates/shirabe/src/util/filesystem.rs +++ b/crates/shirabe/src/util/filesystem.rs @@ -7,13 +7,13 @@ use shirabe_external_packages::composer::pcre::Preg; use shirabe_external_packages::symfony::filesystem::exception::IOException; use shirabe_external_packages::symfony::finder::Finder; use shirabe_php_shim::{ - DIRECTORY_SEPARATOR, ErrorException, LogicException, PhpMixed, RuntimeException, array_pop, - basename, chdir, clearstatcache, clearstatcache2, copy, dirname, error_get_last, explode, - fclose, feof, file_exists, file_get_contents, file_put_contents, fileatime, filemtime, - filesize, fopen, fread, function_exists, fwrite, implode, is_dir, is_file, is_link, - is_readable, lstat, mkdir, php_regex, rename, rmdir, rtrim, str_contains, str_repeat, - str_replace, str_starts_with, strlen, strpos, strtoupper, strtr, substr, substr_count, symlink, - touch, unlink, usleep, var_export, + ErrorException, LogicException, PhpMixed, RuntimeException, array_pop, basename, chdir, + clearstatcache, clearstatcache2, copy, dirname, error_get_last, explode, fclose, feof, + file_exists, file_get_contents, file_put_contents, fileatime, filemtime, filesize, fopen, + fread, function_exists, fwrite, implode, is_dir, is_file, is_link, is_readable, lstat, mkdir, + php_regex, rename, rmdir, rtrim, str_contains, str_repeat, str_replace, str_starts_with, + strlen, strpos, strtoupper, strtr, substr, substr_count, symlink, touch, unlink, usleep, + var_export, }; use std::path::Path; @@ -488,7 +488,12 @@ impl Filesystem { let mut result = true; for file in &ri { - let target_path = format!("{}{}{}", target, DIRECTORY_SEPARATOR, ri.get_sub_pathname()); + let target_path = format!( + "{}{}{}", + target, + std::path::MAIN_SEPARATOR, + ri.get_sub_pathname() + ); if file.is_dir() { self.ensure_directory_exists(&target_path)?; } else { @@ -1003,7 +1008,7 @@ impl Filesystem { let cmd = vec![ "mklink".to_string(), "/J".to_string(), - str_replace("/", DIRECTORY_SEPARATOR, junction), + str_replace("/", std::path::MAIN_SEPARATOR_STR, junction), Platform::realpath(target), ]; let mut output = String::new(); @@ -1066,8 +1071,8 @@ impl Filesystem { return Ok(false); } let junction = rtrim( - &str_replace("/", DIRECTORY_SEPARATOR, junction), - Some(DIRECTORY_SEPARATOR), + &str_replace("/", std::path::MAIN_SEPARATOR_STR, junction), + Some(std::path::MAIN_SEPARATOR_STR), ); if !self.is_junction(&junction) { return Err(IOException::new( |
