예제 #1
0
 /**
  * Initializes a new instance of that class.
  *
  * @param string $path The path of the file.
  * @param string $mode The mode
  *
  * @throws IOException File could not be opened.
  */
 public function __construct($path, $mode = 'r+')
 {
     $path = ClrString::asString($path);
     $res = \fopen((string) $path, ClrString::valueToString($mode, false));
     if (false === $res) {
         $this->throwIOException('Could not open file!');
     }
     $this->_file = $path;
     parent::__construct($res, true);
 }
예제 #2
0
 /**
  * Initializes a new instance of that class.
  *
  * @param string $prefix The file prefix.
  * @param callable $fileFactory The custom file factory to use.
  * @param bool $deleteOnDispose Delete the file when TempFileStream::dispose() method is called or not.
  *
  * @throws ArgumentException $fileFactory is no valid callable / lambda expression.
  * @throws IOException File could not be opened.
  */
 public function __construct($prefix = null, $fileFactory = null, bool $deleteOnDispose = true)
 {
     $prefix = ClrString::valueToString($prefix);
     $fileFactory = static::asCallable($fileFactory);
     if (null === $fileFactory) {
         $fileFactory = function ($prefix, $sysTempDir) : string {
             return \tempnam($sysTempDir, $prefix);
         };
     }
     $this->_deleteOnDispose = $deleteOnDispose;
     parent::__construct($fileFactory($prefix, \sys_get_temp_dir()), 'c+');
 }
예제 #3
0
 /**
  * Normalizes a path.
  *
  * @param string $path The input value.
  *
  * @return string The normalized value.
  */
 protected static function normalizePath($path)
 {
     $path = ClrString::valueToString($path);
     if (!static::isPathRooted($path)) {
         $path = \getcwd() . \DIRECTORY_SEPARATOR . $path;
     }
     $path = \str_ireplace(\DIRECTORY_SEPARATOR, '/', $path);
     $segments = \explode('/', \trim($path, '/'));
     $result = [];
     foreach ($segments as $segment) {
         if ('.' === $segment || '' === $segment) {
             continue;
         }
         if ('..' === $segment) {
             \array_pop($result);
         } else {
             \array_push($result, $segment);
         }
     }
     return \str_ireplace('/', \DIRECTORY_SEPARATOR, \implode('/', $result));
 }
예제 #4
0
 /**
  * {@inheritDoc}
  */
 public function refresh()
 {
     // reset first
     $this->_directory = null;
     $this->_exists = false;
     $me = $this;
     $path = (string) $this->_fullName;
     if (@\is_link($path)) {
         $link = @\readlink($path);
         if (false !== $link) {
             $path = $link;
         }
     }
     // parent directory
     $this->_exists = \file_exists($path) && \is_file($path);
     // parent directory
     $this->_directory = new Lazy(function () use($me) {
         $result = null;
         $dirPath = \dirname((string) $me->fullName());
         if (!ClrString::isNullOrWhitespace($dirPath)) {
             $result = new DirectoryInfo($dirPath);
         }
         return $result;
     });
 }
 /**
  * Initializes a new instance of that class.
  *
  * @param string $path The path of the directory.
  * @param string $message The message.
  * @param \Exception $innerException The inner exception.
  * @param int $code The code.
  */
 public function __construct($path = null, $message = null, \Exception $innerException = null, int $code = 0)
 {
     $this->_path = ClrString::valueToString($path, false);
     parent::__construct($message, $innerException, $code);
 }
예제 #6
0
 /**
  * Creates a random list of chars.
  *
  * @param int $count The number of chars to return.
  * @param string $allowedChars The allowed chars to use.
  * @param callable $seeder The custom seeder for the random values to use.
  *
  * @return IString The random char list.
  *
  * @throws ArgumentOutOfRangeException $count is less than 0.
  */
 public static final function randChars(int $count, $allowedChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', $seeder = null) : IString
 {
     if ($count < 0) {
         throw new ArgumentOutOfRangeException($count, 'count');
     }
     $allowedChars = ClrString::valueToString($allowedChars);
     $result = '';
     foreach (static::buildRandom($count, \strlen($allowedChars), 0, $seeder) as $i) {
         $result .= $allowedChars[$i];
     }
     return new ClrString($result);
 }
예제 #7
0
 /**
  * {@inheritDoc}
  */
 public final function write($data, $count = null, int $offset = 0) : int
 {
     $this->throwIfDisposed();
     $this->throwIfClosed();
     $this->throwIfNotWritable();
     if ($offset < 0) {
         throw new ArgumentOutOfRangeException($offset, 'offset');
     }
     $data = ClrString::valueToString($data, false);
     if (null === $count) {
         $count = \strlen($data) - $offset;
     } else {
         $count = (int) $count;
     }
     if ($count < 0) {
         throw new ArgumentOutOfRangeException($count, 'count');
     }
     if (\strlen($data) - $offset < $count) {
         throw new ArgumentException('offset+count');
     }
     if ($count < 1) {
         return 0;
     }
     return $this->writeInner(\substr($data, $offset, $count));
 }
예제 #8
0
 /**
  * {@inheritDoc}
  */
 public final function joinToStringCallback($separatorFactory = null, $defValue = '')
 {
     if (null === $separatorFactory) {
         $separatorFactory = function () {
             return '';
         };
     }
     $separatorFactory = static::asCallable($separatorFactory);
     $result = $this->iterateWithItemContext(function ($x, IEachItemContext $ctx) use(&$hasItems, $separatorFactory) {
         $hasItems = true;
         $str = $ctx->result();
         if (!$ctx->isFirst()) {
             $str .= $separatorFactory($x, $ctx);
         } else {
             $str = '';
         }
         $str .= ClrString::valueToString($x);
         $ctx->result($str);
     }, $defValue);
     return ClrString::asString($result, false);
 }
예제 #9
0
 /**
  * Creates a new instance of that class from a file path.
  *
  * @param string $path The path of the file to open.
  * @param int $bufferSize The buffer size to use.
  * @param string $mode The mode to open the file with.
  *
  * @return static
  *
  * @throws ArgumentOutOfRangeException $bufferSize is less than 1.
  * @throws FileNotFoundException File was not found.
  * @throws IOException File could not be open.
  */
 public static function forFile($path, int $bufferSize = 1024, $mode = 'r')
 {
     if ($bufferSize < 1) {
         throw new ArgumentOutOfRangeException('bufferSize');
     }
     $path = ClrString::valueToString($path);
     $fullPath = \realpath($path);
     if (false === $fullPath) {
         throw new FileNotFoundException($path);
     }
     $res = \fopen($fullPath, ClrString::valueToString($mode, false));
     if (!\is_resource($res)) {
         throw new IOException(ClrString::format("File '{0}' could not be opened!", $fullPath));
     }
     return new static($res, $bufferSize);
 }
예제 #10
0
 /**
  * {@inheritDoc}
  */
 public function refresh()
 {
     // reset first
     $this->_directory = null;
     $this->_exists = false;
     $me = $this;
     $path = (string) $this->_fullName;
     if (@\is_link($path)) {
         $link = @\readlink($path);
         if (false !== $link) {
             $path = $link;
         }
     }
     // does exist?
     $this->_exists = \file_exists($path) && \is_dir($path);
     // parent directory
     $this->_directory = new Lazy(function () use($me) {
         $result = null;
         $np = $me->getType()->getMethod('normalizePath')->getClosure(null);
         $dirPath = $me->fullName()->asImmutable();
         if (!$dirPath->endsWith(\DIRECTORY_SEPARATOR)) {
             $dirPath = $dirPath->append(\DIRECTORY_SEPARATOR);
         }
         $dirPath = $dirPath->append('..');
         $dirPath = $np($dirPath);
         if (!ClrString::isNullOrWhitespace($dirPath)) {
             $result = $me->getType()->newInstance($dirPath);
         }
         return $result;
     });
 }
예제 #11
0
 /**
  * Formats a string.
  *
  * @param string $format The format string.
  * @param mixed ...$args One or more argument lists for $format.
  *
  * @return string The formatted string.
  */
 public static function formatArray($format, $args = null) : IString
 {
     if (!\is_array($args)) {
         $args = Enumerable::create($args)->toArray();
     }
     $argCount = \func_num_args();
     for ($i = 2; $i < $argCount; $i++) {
         Enumerable::create(\func_get_arg($i))->appendToArray($args);
     }
     return static::asString(\preg_replace_callback('/{(\\d+)(\\:[^}]*)?}/i', function ($match) use($args) {
         $i = (int) $match[1];
         $format = null;
         if (isset($match[2])) {
             $format = \substr($match[2], 1);
         }
         return \array_key_exists($i, $args) ? ClrString::parseFormatStringValue($format, $args[$i]) : $match[0];
     }, static::valueToString($format)));
 }
예제 #12
0
 /**
  * Creates a closure from a lambda expression.
  *
  * @param string $expr The expression.
  * @param bool $throwException Throw exception or return (false) instead.
  *
  * @return \Closure|bool The closure or (false) on error.
  *
  * @throws ArgumentException $expr is no valid expression.
  */
 public static function toLambda($expr, bool $throwException = true)
 {
     $throwOrReturn = function () use($throwException) {
         if ($throwException) {
             throw new System\ArgumentException('expr', 'No lambda expression!', null, 0);
         }
         return false;
     };
     if (!ClrString::canBeString($expr)) {
         return $throwOrReturn();
     }
     $expr = trim(ClrString::valueToString($expr));
     // check for lambda
     if (1 === preg_match("/^(\\s*)([\\(]?)([^\\)]*)([\\)]?)(\\s*)(=>)/m", $expr, $lambdaMatches)) {
         if (empty($lambdaMatches[2]) && !empty($lambdaMatches[4]) || !empty($lambdaMatches[2]) && empty($lambdaMatches[4])) {
             if ($throwException) {
                 throw new System\ArgumentException('expr', 'Syntax error in lambda expression!', null, 1);
             }
             return false;
         }
         $lambdaBody = trim(substr($expr, strlen($lambdaMatches[0])), '{}' . " \t\n\r\v");
         // remove surrounding {}
         if ('' !== $lambdaBody) {
             if (';' !== \substr($lambdaBody, -1)) {
                 // auto add return statement
                 $lambdaBody = 'return ' . $lambdaBody . ';';
             }
         }
         return self::execGlobal('return function(' . $lambdaMatches[3] . ') { ' . $lambdaBody . ' };');
     }
     return $throwOrReturn();
 }
예제 #13
0
 /**
  * Executes code globally.
  *
  * @param string $code The code to execute.
  *
  * @return mixed The result of the execution.
  *
  * @throws \System\InvalidCastException $code cannot be a string.
  */
 public static final function execGlobal($code)
 {
     return eval(System\ClrString::valueToString($code, false));
 }
예제 #14
0
 /**
  * Initializes a new instance of that class.
  *
  * @param string $buffer The initial data.
  * @param bool $writable New instance is writable or read only.
  */
 public function __construct($buffer = null, bool $writable = true)
 {
     $this->_buffer = ClrString::valueToString($buffer);
     $this->_writable = $writable;
 }