/**
  * @inheritDoc
  */
 public function readLine()
 {
     if (!$this->file->eof()) {
         $line = $this->file->current();
         $this->file->next();
         return json_decode($line);
     }
     return false;
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function read()
 {
     if ($this->file->valid()) {
         $this->file->seek($this->getContext()->getReadCount());
         $line = rtrim($this->file->current(), PHP_EOL);
         $this->getContext()->incrementReadCount();
         return json_decode($line, true);
     }
     return null;
 }
Ejemplo n.º 3
0
 public function testCheckAnswerTask3()
 {
     $this->assertFileExists('data/export.csv');
     $filecsv = new \SplFileObject('data/export.csv');
     $filecsv->setFlags(\SplFileObject::READ_CSV);
     $header = $filecsv->current();
     $processor = new YieldProcessor(ReaderFactory::create('Db\\Product', include 'config/database.php'), WriterFactory::create('Stub'), function () {
     });
     foreach ($processor->processing() as $item) {
         $filecsv->next();
         $this->assertEquals($item, array_combine($header, $filecsv->current()));
     }
 }
Ejemplo n.º 4
0
 protected function export($var, $return = false)
 {
     if ($var instanceof Closure) {
         /* dump anonymous function in to plain code.*/
         $ref = new ReflectionFunction($var);
         $file = new SplFileObject($ref->getFileName());
         $file->seek($ref->getStartLine() - 1);
         $result = '';
         while ($file->key() < $ref->getEndLine()) {
             $result .= $file->current();
             $file->next();
         }
         $begin = strpos($result, 'function');
         $end = strrpos($result, '}');
         $result = substr($result, $begin, $end - $begin + 1);
     } elseif (is_object($var)) {
         /* dump object with construct function. */
         $result = 'new ' . get_class($var) . '(' . $this->export(get_object_vars($var), true) . ')';
     } elseif (is_array($var)) {
         /* dump array in plain array.*/
         $array = array();
         foreach ($var as $k => $v) {
             $array[] = var_export($k, true) . ' => ' . $this->export($v, true);
         }
         $result = 'array(' . implode(', ', $array) . ')';
     } else {
         $result = var_export($var, true);
     }
     if (!$return) {
         print $result;
     }
     return $result;
 }
Ejemplo n.º 5
0
/**
 * 返回文件从X行到Y行的内容(支持php5、php4)
 * @param  String  $filename  文件名
 * @param  integer $startLine 开始行
 * @param  integer $endLine   结束行
 * @param  string  $method    方法
 * @return array() 返回数组
 */
function readFileByLines($filename, $startLine = 1, $endLine = 50, $method = 'rb')
{
    $content = array();
    $count = $endLine - $startLine;
    // 判断php版本(因为要用到SplFileObject,PHP>=5.1.0)
    if (version_compare(PHP_VERSION, '5.1.0', '>=')) {
        $fp = new SplFileObject($filename, $method);
        // 转到第N行, seek方法参数从0开始计数
        $fp->seek($startLine - 1);
        for ($i = 0; $i <= $count; ++$i) {
            // current()获取当前行内容
            $content[] = $fp->current();
            // 下一行
            $fp->next();
        }
    } else {
        //PHP<5.1
        $fp = fopen($filename, $method);
        if (!$fp) {
            return 'error:can not read file';
        }
        // 跳过前$startLine行
        for ($i = 1; $i < $startLine; ++$i) {
            fgets($fp);
        }
        // 读取文件行内容
        for ($i; $i <= $endLine; ++$i) {
            $content[] = fgets($fp);
        }
        fclose($fp);
    }
    // array_filter过滤:false,null,''
    return array_filter($content);
}
Ejemplo n.º 6
0
 /**
  * Get Code from file
  * @param $path
  * @param $line
  * @param $numLines
  * @return array|null
  */
 private function getCode($path, $line, $numLines)
 {
     if (empty($path) || empty($line) || !file_exists($path)) {
         return NULL;
     }
     try {
         // Get the number of lines in the file
         $file = new \SplFileObject($path);
         $file->seek(PHP_INT_MAX);
         $totalLines = $file->key() + 1;
         // Work out which lines we should fetch
         $start = max($line - floor($numLines / 2), 1);
         $end = $start + ($numLines - 1);
         if ($end > $totalLines) {
             $end = $totalLines;
             $start = max($end - ($numLines - 1), 1);
         }
         // Get the code for this range
         $code = array();
         $file->seek($start - 1);
         while ($file->key() < $end) {
             $code[$file->key() + 1] = rtrim($file->current());
             $file->next();
         }
         return $code;
     } catch (RuntimeException $ex) {
         return null;
     }
 }
Ejemplo n.º 7
0
function modifyFileLine($filename, $linenum, $lineText)
{
    $fp = new SplFileObject($filename);
    $fp->seek($linenum);
    $line = $fp->current();
    $fp->fseek($linenum, SEEK_CUR);
    $fp->fwrite($lineText);
}
Ejemplo n.º 8
0
 /**
  * Write the compiled csv to disk and return the file name
  *
  * @param array $sortedHeaders An array of sorted headers
  *
  * @return string The csv filename where the data was written
  */
 public function prepare($cacheFile, $sortedHeaders)
 {
     $csvFile = $cacheFile . '.csv';
     $handle = fopen($csvFile, 'w');
     // Generate a csv version of the multi-row headers to write to disk
     $headerRows = [[], []];
     foreach ($sortedHeaders as $idx => $header) {
         if (!is_array($header)) {
             $headerRows[0][] = $header;
             $headerRows[1][] = '';
         } else {
             foreach ($header as $headerName => $subHeaders) {
                 $headerRows[0][] = $headerName;
                 $headerRows[1] = array_merge($headerRows[1], $subHeaders);
                 if (count($subHeaders) > 1) {
                     /**
                      * We need to insert empty cells for the first row of headers to account for the second row
                      * this acts as a faux horizontal cell merge in a csv file
                      * | Header 1 | <---- 2 extra cells ----> |
                      * | Sub 1    | Subheader 2 | Subheader 3 |
                      */
                     $headerRows[0] = array_merge($headerRows[0], array_fill(0, count($subHeaders) - 1, ''));
                 }
             }
         }
     }
     fputcsv($handle, $headerRows[0]);
     fputcsv($handle, $headerRows[1]);
     // TODO: Track memory usage
     $file = new \SplFileObject($cacheFile);
     while (!$file->eof()) {
         $csvRow = [];
         $row = json_decode($file->current(), true);
         if (!is_array($row)) {
             // Invalid json data -- don't process this row
             continue;
         }
         foreach ($sortedHeaders as $idx => $header) {
             if (!is_array($header)) {
                 $csvRow[] = isset($row[$header]) ? $row[$header] : '';
             } else {
                 // Multi-row header, so we need to set all values
                 $nestedHeaderName = array_keys($header)[0];
                 $nestedHeaders = $header[$nestedHeaderName];
                 foreach ($nestedHeaders as $nestedHeader) {
                     $csvRow[] = isset($row[$nestedHeaderName][$nestedHeader]) ? $row[$nestedHeaderName][$nestedHeader] : '';
                 }
             }
         }
         fputcsv($handle, $csvRow);
         $file->next();
     }
     $file = null;
     // Get rid of the file handle that SplFileObject has on cache file
     unlink($cacheFile);
     fclose($handle);
     return $csvFile;
 }
Ejemplo n.º 9
0
 /**
  * valid SplFileObjectでCSVフィルを読ませると最終行の空配列で返るのでそれの抑止
  *
  * @return bool
  */
 public function valid()
 {
     $parentValid = parent::valid();
     $var = parent::current();
     if ($var === array(null)) {
         return false;
     }
     return $parentValid;
 }
Ejemplo n.º 10
0
 /**
  * @param string $expectedContent
  * @param integer $zeroBasedLineNumber
  * @param boolean $trimLine
  * @return void
  */
 private function assertSameContentAtLine($expectedContent, $zeroBasedLineNumber, $trimLine = true)
 {
     $this->fileObject->seek($zeroBasedLineNumber);
     $actualContent = $this->fileObject->current();
     if ($trimLine) {
         $actualContent = trim($actualContent);
     }
     $this->assertSame($expectedContent, $actualContent);
 }
Ejemplo n.º 11
0
 /**
  * @param \SplFileObject $stream
  *
  * Reads the file to parse its lines
  */
 public function readFile(\SplFileObject $stream)
 {
     $stream->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);
     while (!$stream->eof()) {
         $this->parseLine($stream->current());
         $stream->next();
     }
     $stream = null;
 }
Ejemplo n.º 12
0
 /**
  * Fetch the current line as an associative array
  *
  * @return array
  */
 public function current()
 {
     $data = trim(parent::current());
     $parts = parse_url($data);
     if (isset($parts['query'])) {
         $parts['query'] = $this->_parseQuery($parts['query']);
     }
     return $parts;
 }
Ejemplo n.º 13
0
 /**
  * Evaluates the closure and the code in the reset of the file. This sets
  * the code for the closure, the namespace it is declared in (if any) and
  * any applicable namespace imports
  * @param ReflectionFunction The reflected function of the closure
  * @return String The code the closure runs 
  */
 private function evaluate(\ReflectionFunction $reflection)
 {
     $code = '';
     $full = '';
     $file = new \SplFileObject($reflection->getFileName());
     while (!$file->eof()) {
         if ($file->key() >= $reflection->getStartLine() - 1 && $file->key() < $reflection->getEndLine()) {
             $code .= $file->current();
         }
         $full .= $file->current();
         $file->next();
     }
     //@todo this assumes the function will be the only one on that line
     $begin = strpos($code, 'function');
     //@todo this assumes the } will be the only one on that line
     $end = strrpos($code, '}');
     $this->code = substr($code, $begin, $end - $begin + 1);
     $this->extractDetail($full);
 }
Ejemplo n.º 14
0
 /**
  * This method returns the element at the the specified index.
  *
  * @access public
  * @param integer $index                                    the index of the element
  * @return mixed                                            the element at the specified index
  * @throws Throwable\InvalidArgument\Exception              indicates that an index must be an integer
  * @throws Throwable\OutOfBounds\Exception                  indicates that the index is out of bounds
  */
 public function getValue($index)
 {
     if (!$this->hasIndex($index)) {
         throw new Throwable\OutOfBounds\Exception('Unable to get element. Undefined index at ":index" specified', array(':index' => $index));
     }
     $this->reader->seek($index);
     $line = $this->reader->current();
     $value = unserialize($line);
     return $value;
 }
Ejemplo n.º 15
0
 public function current()
 {
     if ($this->_columns) {
         if (false === ($combined = @array_combine($this->_columns, parent::current()))) {
             throw new Exception(sprintf("Column count did not match expected on line %d", parent::key()));
         }
         return $combined;
     }
     return parent::current();
 }
Ejemplo n.º 16
0
function generate($nb, $fixture = __DIR__ . '/fixtures/lorem.txt')
{
    $lorems = new SplFileObject($fixture, 'a+');
    $txt = '';
    while ($nb > 0 && $lorems->valid()) {
        $txt .= $lorems->current();
        $lorems->next();
        $nb--;
    }
    return $txt;
}
Ejemplo n.º 17
0
 public function read($id)
 {
     $file = new \SplFileObject($this->filename, "r");
     if ($file->flock(LOCK_EX)) {
         $file->seek($id);
         $data = $file->current();
         $file->flock(LOCK_UN);
     } else {
         return false;
     }
     return $this->mapStringDataToObject($data);
 }
Ejemplo n.º 18
0
 /**
  * @param $fileName
  * @param $page
  * @param $rows
  * @param string $methord
  * @return array
  */
 public static function getContents($fileName, $page, $rows, $methord = 'rb')
 {
     $content = array();
     $start = ($page - 1) * $rows;
     $fp = new \SplFileObject($fileName, $methord);
     $fp->seek($start);
     for ($i = 0; $i < $rows; ++$i) {
         $content[] = $fp->current();
         $fp->next();
     }
     return array_filter($content);
 }
Ejemplo n.º 19
0
 public function current()
 {
     $row = parent::current();
     if ($this->names) {
         if (count($row) != count($this->names)) {
             return null;
         } else {
             $row = array_combine($this->names, $row);
         }
     }
     return $row;
 }
Ejemplo n.º 20
0
 /**
  *  Rewind the \Iterator to the first element
  */
 public function rewind()
 {
     $this->_filepos = null;
     $this->_key = 0;
     if (null === $this->_file) {
         $this->_openFile();
     } elseif ($this->_file) {
         $this->_file->rewind();
         $this->_file->current();
         // Reading first line is necessary for correct loading of file.
         $this->next();
     }
 }
Ejemplo n.º 21
0
 public function getData()
 {
     try {
         $file = new SplFileObject($this->tmp_name, 'rb');
         $data = null;
         for ($file; $file->valid(); $file->next()) {
             $data .= $file->current();
         }
         return $data;
     } catch (RuntimeException $e) {
     }
     return null;
 }
Ejemplo n.º 22
0
 /**
  * {@inheritdoc}
  */
 public function current()
 {
     $row = parent::current();
     if ($row && !empty($this->getColumnNames())) {
         // Only use columns specified in the defined CSV columns.
         $row = array_intersect_key($row, $this->getColumnNames());
         // Set meaningful keys for the columns mentioned in $this->csvColumns.
         foreach ($this->getColumnNames() as $int => $values) {
             // Copy value to more descriptive string based key and unset original.
             $row[key($values)] = isset($row[$int]) ? $row[$int] : NULL;
             unset($row[$int]);
         }
     }
     return $row;
 }
 public function testAgainstOriginalSedImplementation()
 {
     $tokenizer = new PennTreeBankTokenizer();
     $tokenized = new \SplFileObject(TEST_DATA_DIR . "/Tokenizers/PennTreeBankTokenizerTest/tokenized");
     $tokenized->setFlags(\SplFileObject::DROP_NEW_LINE);
     $sentences = new \SplFileObject(TEST_DATA_DIR . "/Tokenizers/PennTreeBankTokenizerTest/test.txt");
     $sentences->setFlags(\SplFileObject::DROP_NEW_LINE);
     $tokenized->rewind();
     foreach ($sentences as $sentence) {
         if ($sentence) {
             $this->assertEquals($tokenized->current(), implode(" ", $tokenizer->tokenize($sentence)), "Sentence: '{$sentence}' was not tokenized correctly");
         }
         $tokenized->next();
     }
 }
Ejemplo n.º 24
0
 /**
  * @Route("/", name="import")
  * @param Request $request
  * @return Response
  */
 public function indexAction(Request $request)
 {
     $form = $this->createFormBuilder()->add('attach', FileType::class)->add('save', SubmitType::class, array('label' => 'Create Task'))->getForm();
     $form->handleRequest($request);
     if ($form->isValid()) {
         $data = [];
         $importService = $this->getImportService();
         $importService->uploadFile($form->get('attach')->getData());
         $csv = new \SplFileObject($importService->getUploadedCsvName() . 'csv');
         if ($data['delimiter'] = $importService->getFileDelimiter($csv->current())) {
             $importService->getCsvHeader($csv, $data);
             return $this->forward('AppBundle:Default:step2', ['data' => $data]);
         }
     }
     return $this->render('default/index.html.twig', ['form' => $form->createView()]);
 }
Ejemplo n.º 25
0
 /**
  * {@inheritdoc}
  */
 public function current()
 {
     $row = parent::current();
     if ($row && !empty($this->columnNames)) {
         // Only use columns specified in the defined CSV columns.
         $row = array_intersect_key($row, $this->columnNames);
         // Set meaningful keys for the columns mentioned in $this->csvColumns.
         foreach ($this->columnNames as $key => $value) {
             // Copy value to more descriptive key and unset original.
             $value = key($value);
             $row[$value] = isset($row[$key]) ? $row[$key] : NULL;
             unset($row[$key]);
         }
     }
     return $row;
 }
Ejemplo n.º 26
0
 function getMethodCode($name, $comment = false)
 {
     $fun = $this->getMethod($name);
     $start = $fun->getStartLine();
     $end = $fun->getEndLine();
     $fileName = $fun->getFileName();
     $file = new SplFileObject($fileName);
     $file->seek($start - 1);
     $i = 0;
     $str = $comment ? $fun->getDocComment() : '';
     while ($i++ < $end + 1 - $start) {
         $str .= $file->current();
         $file->next();
     }
     return $str;
 }
Ejemplo n.º 27
0
 public static function setUpBeforeClass()
 {
     $reflection = new ReflectionClass(__NAMESPACE__ . '\\TestClass');
     self::$startLine = $reflection->getStartLine();
     self::$endLine = $reflection->getEndLine();
     self::$testClassCode = NULL;
     /* GetCode as String */
     $file = new \SplFileObject($reflection->getFileName());
     $file->seek(self::$startLine - 1);
     while ($file->key() < $reflection->getEndLine()) {
         self::$testClassCode .= $file->current();
         $file->next();
     }
     self::$testClassCode = rtrim(self::$testClassCode, "\n");
     unset($file);
 }
Ejemplo n.º 28
0
 private function getClosureCode(\Closure $closure)
 {
     $reflection = new \ReflectionFunction($closure);
     // Open file and seek to the first line of the closure
     $file = new \SplFileObject($reflection->getFileName());
     $file->seek($reflection->getStartLine() - 1);
     // Retrieve all of the lines that contain code for the closure
     $code = '';
     while ($file->key() < $reflection->getEndLine()) {
         $line = $file->current();
         $line = ltrim($line);
         $code .= $line;
         $file->next();
     }
     return $code;
 }
Ejemplo n.º 29
0
function word_gen($word_len)
{
    global $word_len_arr;
    if ($word_len > WORD_LEN_ARR_SIZE) {
        return "";
    }
    $wordlist_file = new SplFileObject(WORD_LIST_FILE_NAME);
    $first_word_pos = $word_len_arr[$word_len - 1];
    //Get the position of the first word in word list file
    $num_of_word = $word_len_arr[$word_len] - $word_len_arr[$word_len - 1];
    //Get number of word which have len = $word_len
    $wordlist_file->seek($first_word_pos + rand(0, $num_of_word));
    // Seek to line no. 10,000
    return trim($wordlist_file->current());
    // Print contents of that line
}
Ejemplo n.º 30
0
 protected function _fetchCode()
 {
     // Open file and seek to the first line of the closure
     $file = new SplFileObject($this->reflection->getFileName());
     $file->seek($this->reflection->getStartLine() - 1);
     // Retrieve all of the lines that contain code for the closure
     $code = '';
     while ($file->key() < $this->reflection->getEndLine()) {
         $code .= $file->current();
         $file->next();
     }
     // Only keep the code defining that closure
     $begin = strpos($code, 'function');
     $end = strrpos($code, '}');
     $code = substr($code, $begin, $end - $begin + 1);
     return $code;
 }