/rhaco/io/Stream.php
https://github.com/tokushima/rhaco1 · PHP · 151 lines · 99 code · 5 blank · 47 comment · 22 complexity · 5a3bc00a8e1ef1dc90a2e6811215d135 MD5 · raw file
- <?php
- Rhaco::import("network.http.Http");
- Rhaco::import("io.FileUtil");
- Rhaco::import("lang.StringUtil");
- Rhaco::import("exception.ExceptionTrigger");
- Rhaco::import("exception.model.PermissionException");
- /**
- * ストリームを操作するクラス
- *
- * @author Kazutaka Tokushima
- * @license New BSD License
- * @copyright Copyright 2005- rhaco project. All rights reserved.
- */
- class Stream{
- /**
- * ストリームから取得する
- * @param string $url
- * @return string
- */
- function read($url){
- /*** #pass */
- $src = "";
- if(preg_match("/[\w]+:\/\/[\w]+/",$url)){
- if(preg_match("/php:\/\/(.+)/i",$url,$type)){
- $type = strtolower($type);
-
- if($type == "stdin"){
- $src = Stream::stdin();
- }else if($type == "input"){
- $src = Stream::input();
- }
- }else{
- $src = Http::get($url);
- }
- }else{
- $src = FileUtil::read($url);
- }
- return StringUtil::encode($src);
- }
- /**
- * ストリームに書き出す
- * @param string $url
- * @param string $value
- */
- function write($url,$value){
- /*** #pass */
- if(preg_match("/[\w]+:\/\/[\w]+/",$url)){
- if(preg_macth("/php:\/\/(.+)/i",$url,$type)){
- $type = strtolower($type);
-
- if($type == "stdout"){
- Stream::stdout($value);
- }else if($type == "stderr"){
- Stream::stderr($value);
- }else if($type == "output"){
- Stream::output($value);
- }
- }
- }else{
- FileUtil::write($url,$value);
- }
- }
- /**
- * 標準入力から取得
- * @return string
- */
- function stdin(){
- /*** #pass */
- $buffer = "";
- $fp = fopen("php://stdin","r");
- if(!$fp){
- ExceptionTrigger::raise(new PermissionException("php://stdin"));
- return false;
- }
- while(substr($buffer,-1) != "\n" && substr($buffer,-1) != "\r\n"){
- $buffer .= fgets($fp,4096);
- }
- fclose($fp);
- return rtrim($buffer);
- }
- /**
- * php://input から取得
- * @return string
- */
- function input(){
- /*** #pass */
- $buffer = "";
- $fp = fopen("php://input","r");
- if(!$fp){
- ExceptionTrigger::raise(new PermissionException("php://input"));
- return false;
- }
- while(!feof($fp)){
- $buffer .= fgets($fp,4096);
- }
- fclose($fp);
- return $buffer;
- }
- /**
- * 標準出力へ出力
- * @param string $value
- * @return boolean
- */
- function stdout($value){
- /*** #pass */
- $fp = fopen("php://stdout","w");
- if(!$fp){
- ExceptionTrigger::raise(new PermissionException("php://stdout"));
- return false;
- }
- fwrite($fp,$value);
- fclose($fp);
- return true;
- }
- /**
- * 標準エラー出力へ出力
- * @param $value
- * @return boolean
- */
- function stderr($value){
- /*** #pass */
- $fp = fopen("php://stderr","w");
- if(!$fp){
- ExceptionTrigger::raise(new PermissionException("php://stderr"));
- return false;
- }
- fwrite($fp,$value);
- fclose($fp);
- return true;
- }
- /**
- * php://output へ出力
- * @param string $value
- * @return boolean
- */
- function output($value){
- /*** #pass */
- $fp = fopen("php://output","w");
- if(!$fp){
- ExceptionTrigger::raise(new PermissionException("php://output"));
- return false;
- }
- fwrite($fp,$value);
- fclose($fp);
- return true;
- }
- }
- ?>