Phar::webPhar
(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL phar >= 2.0.0)
Phar::webPhar — 웹 브라우저의 요청을 phar 아카이브 내의 내부 파일로 라우팅합니다.
설명
final public static Phar::webPhar( ?string $alias = null, ?string $index = null, ?string $fileNotFoundScript = null, array $mimeTypes = [], ?callable $rewrite = null ): void
Phar::webPhar()는 웹 기반 Phar의 Phar::mapPhar() 역할을 합니다. 이 메서드는 $_SERVER['REQUEST_URI']를 구문 분석하고 웹 브라우저의 요청을 phar 아카이브 내의 내부 파일로 라우팅합니다. 웹 서버를 시뮬레이션하여 요청을 올바른 파일로 라우팅하고 올바른 헤더를 에코하고 필요에 따라 PHP 파일을 구문 분석합니다. Phar::mungServer() 및 Phar::interceptFileFuncs()와 결합하면 모든 웹 애플리케이션을 phar 아카이브에서 수정하지 않고 사용할 수 있습니다.
Phar::webPhar()는 phar 아카이브의 스텁에서만 호출해야 합니다(스텁이 무엇인지에 대한 자세한 내용은 여기 참조).
매개변수
alias
- 전체 경로가 아니라 이 아카이브를 참조하기 위해 phar:// URL에서 사용할 수 있는 별칭입니다.
index
- 디렉토리 인덱스의 phar 내의 위치입니다.
fileNotFoundScript
- 파일을 찾을 수 없을 때 실행할 스크립트의 위치입니다. 이 스크립트는 적절한 HTTP 404 헤더를 출력해야 합니다.
mimeTypes
- 추가 파일 확장자를 MIME 유형에 매핑하는 배열입니다. 기본 매핑으로 충분하면 빈 배열을 전달합니다. 기본적으로 이러한 확장은 다음 MIME 유형에 매핑됩니다.
<?php $mimes = array( 'phps' => Phar::PHPS, // pass to highlight_file() 'c' => 'text/plain', 'cc' => 'text/plain', 'cpp' => 'text/plain', 'c++' => 'text/plain', 'dtd' => 'text/plain', 'h' => 'text/plain', 'log' => 'text/plain', 'rng' => 'text/plain', 'txt' => 'text/plain', 'xsd' => 'text/plain', 'php' => Phar::PHP, // parse as PHP 'inc' => Phar::PHP, // parse as PHP 'avi' => 'video/avi', 'bmp' => 'image/bmp', 'css' => 'text/css', 'gif' => 'image/gif', 'htm' => 'text/html', 'html' => 'text/html', 'htmls' => 'text/html', 'ico' => 'image/x-ico', 'jpe' => 'image/jpeg', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'js' => 'application/x-javascript', 'midi' => 'audio/midi', 'mid' => 'audio/midi', 'mod' => 'audio/mod', 'mov' => 'movie/quicktime', 'mp3' => 'audio/mp3', 'mpg' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'pdf' => 'application/pdf', 'png' => 'image/png', 'swf' => 'application/shockwave-flash', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'wav' => 'audio/wav', 'xbm' => 'image/xbm', 'xml' => 'text/xml', ); ?>
rewrite
- rewrites 함수는 문자열이 유일한 매개변수로 전달되며 문자열 또는
false
를 반환해야 합니다.fast-cgi 또는 cgi를 사용하는 경우 함수에 전달되는 매개변수는 $_SERVER['PATH_INFO'] 변수의 값입니다. 그렇지 않으면 함수에 전달된 매개변수는 $_SERVER['REQUEST_URI'] 변수의 값입니다.
문자열이 반환되면 내부 파일 경로로 사용됩니다.
false
가 반환되면 webPhar()는 HTTP 403 거부 코드를 보냅니다.
반환 값
값이 반환되지 않습니다.
오류/예외
출력할 내부 파일을 열 수 없거나 비 스텁에서 호출된 경우 PharException이 발생합니다. 잘못된 배열 값이 mimeTypes
에 전달되거나 잘못된 콜백이 rewrite
에 전달되면 UnexpectedValueException이 발생합니다.
변경 로그
버전 | 설명 |
---|---|
8.0.0 | fileNotFoundScript ,mimeTypes 및 rewrite 는 이제 null을 허용합니다. |
Examples
예제 #1 Phar::webPhar() 예제
아래 예에서 생성된 phar는 /myphar.phar/index.php
또는 /myphar.phar
를 탐색하는 경우 Hello World
를 표시하고 /myphar.phar/index.phps
를 탐색하는 경우 index.phps
의 소스를 표시합니다.
<?php
// creating the phar archive:
try {
$phar = new Phar('myphar.phar');
$phar['index.php'] = '<?php echo "Hello World"; ?>';
$phar['index.phps'] = '<?php echo "Hello World"; ?>';
$phar->setStub('<?php
Phar::webPhar();
__HALT_COMPILER(); ?>');
} catch (Exception $e) {
// handle error here
}
?>
기타
- Phar::mungServer() - 실행을 위해 수정해야 하는 최대 4개의 $_SERVER 변수 목록을 정의합니다.
- Phar::interceptFileFuncs() - Instructs phar to intercept fopen, file_get_contents, opendir, and all of the stat-related functions