Ejemplo n.º 1
0
 public function execute()
 {
     $writer = new CsvWriter($this->getCsvWriterOptions());
     $writer->createTempStream();
     if ($this->getHeader()) {
         $writer->writeRow($this->getHeader());
     }
     $results = $this->query->find();
     foreach ($results as $result) {
         $writer->writeRow($this->getRow($result));
     }
     $this->content = $writer->getFileContent();
     $writer->close();
     return $this;
 }
Ejemplo n.º 2
0
 public function finalize()
 {
     $this->csvWriter->close();
     return $this->filename;
 }
Ejemplo n.º 3
0
 public function testWritingTempStream()
 {
     $csvArray = array(array('nom', 'prénom', 'age'), array('Martin', 'Durand', '28'), array('Alain', 'Richard', '36'));
     $expected = 'nom,prénom,age' . "\n" . 'Martin,Durand,28' . "\n" . 'Alain,Richard,36' . "\n";
     $writer = new CsvWriter(array('delimiter' => ',', 'enclosure' => '"', 'encoding' => 'UTF-8', 'eol' => "\n", 'escape' => "\\", 'enclosing_mode' => Dialect::ENCLOSING_MINIMAL, 'escape_double' => true));
     $this->assertFalse($writer->isFileOpened());
     $this->assertInstanceOf('CSanquer\\ColibriCsv\\CsvWriter', $writer->createTempStream());
     $this->assertTrue($writer->isFileOpened());
     $this->assertInternalType('resource', $writer->getFileHandler());
     $this->assertInstanceOf('CSanquer\\ColibriCsv\\CsvWriter', $writer->writeRows($csvArray));
     $this->assertEquals($expected, $writer->getFileContent());
     $this->assertInstanceOf('CSanquer\\ColibriCsv\\CsvWriter', $writer->close());
 }
Ejemplo n.º 4
0
$watch->start('csv_generation');
$header = array('lastname', 'firstname', 'phone number', 'email', 'birthday', 'address', 'zipcode', 'city', 'points');
$csvWriter1->writeRow($header);
for ($i = 0; $i < $maxLines; $i++) {
    $lastname = $faker->lastName;
    $firstname = $faker->firstName;
    $row = array($lastname, $firstname, $faker->phoneNumber, strtolower($firstname . '.' . $lastname) . '@' . $faker->domainName, $faker->dateTimeThisCentury->format('Y-m-d'), $faker->streetAddress, $faker->postcode, $faker->city, $faker->randomNumber());
    $csvWriter1->writeRow($row);
}
$csvWriter1->close();
$eventGeneration = $watch->stop('csv_generation');
echo 'Converting the CSV' . "\n\n";
$file2 = $dir . '/bench_result' . $maxLines . '.csv';
$watch->start('csv_convert');
$csvReader = new CsvReader(array('delimiter' => ';', 'enclosure' => '"', 'encoding' => 'CP1252', 'eol' => "\r\n", 'escape' => "\\"));
$csvReader->open($file1);
$csvWriter2 = new CsvWriter(array('delimiter' => ',', 'enclosure' => '"', 'encoding' => 'UTF-8', 'eol' => "\n", 'escape' => "\\"));
$csvWriter2->open($file2);
foreach ($csvReader as $row) {
    $csvWriter2->writeRow($row);
}
$csvWriter2->close();
$csvReader->close();
$eventConverting = $watch->stop('csv_convert');
echo 'Results' . "\n";
echo 'CSV Generation' . "\n\n";
echo 'duration     = ' . $eventGeneration->getDuration() . ' ms' . "\n";
echo 'memory usage = ' . $eventGeneration->getMemory() / (1024 * 1024) . ' Mb (' . $eventConverting->getMemory() . ' b)' . "\n\n";
echo 'CSV Parsing/Converting' . "\n\n";
echo 'duration     = ' . $eventConverting->getDuration() . ' ms' . "\n";
echo 'memory usage = ' . $eventConverting->getMemory() / (1024 * 1024) . ' Mb (' . $eventConverting->getMemory() . ' b)' . "\n";
Ejemplo n.º 5
0
 /**
  * @param $datas
  */
 public function getCsv($data)
 {
     $filePath = "../" . $this->filename . '.csv';
     $writer = new CsvWriter(array('delimiter' => ';', 'enclosure' => '"', 'encoding' => 'CP1252', 'eol' => "\r\n", 'escape' => "\\", 'bom' => false, 'translit' => null, 'first_row_header' => false, 'trim' => false));
     $stream = fopen($filePath, 'wb');
     $writer->open($stream);
     $array = json_decode(json_encode($data), true);
     $writer->writeRows($array);
     $writer->getFileContent();
     $writer->close();
     $maxRead = 1 * 2048 * 2048;
     $fileSize = filesize($filePath);
     $fh = fopen($filePath, 'r');
     $read = 0;
     header('Content-Type: application/octet-stream');
     header('Content-Disposition: attachment; filename="' . str_replace('../', '', $this->filename) . ".csv" . '"');
     while (!feof($fh)) {
         echo fread($fh, $maxRead);
     }
     exit;
 }
Ejemplo n.º 6
0
 /**
  * Execute the command
  *
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $ep = $this->getService('sugarcrm.entrypoint');
     $bm = new BeanManager($ep);
     $this->module = $input->getOption('module');
     // Get the file as a parameter
     if (empty($this->module)) {
         $moduleList = array_keys($ep->getBeansList());
         $msg = 'You must define the module with --module';
         $msg .= PHP_EOL . PHP_EOL . 'List of Available modules: ' . PHP_EOL;
         $msg .= '    - ' . implode(PHP_EOL . '    - ', $moduleList);
         throw new \InvalidArgumentException($msg);
     }
     ########### FIELDS
     $moduleFields = $bm->getModuleFields($this->module, $input->getOption('lang'), true);
     // Change the lists that are arrays as strings
     foreach ($moduleFields as $key => $moduleProps) {
         if (array_key_exists('options_list', $moduleProps)) {
             $optionsList = '';
             foreach ($moduleFields[$key]['options_list'] as $optK => $optV) {
                 $optionsList .= "{$optK}<=>{$optV}" . PHP_EOL;
             }
             $moduleFields[$key]['options_list'] = $optionsList;
         } else {
             $moduleFields[$key]['options_list'] = 'N/A';
         }
     }
     // create the writer
     $writer = new CsvWriter(array('delimiter' => ';', 'enclosure' => '"', 'encoding' => 'UTF-8', 'bom' => false, 'first_row_header' => true, 'trim' => true));
     $file = $this->module . '-Fields.' . date('Y-m-d') . '.csv';
     //Open the csv file to write
     $writer->open(getcwd() . '/' . $file);
     $writer->writeRows($moduleFields);
     $writer->close();
     $output->writeln("<comment>All fields for {$this->module} written in {$file}</comment>");
     ########### RELATIONSHIPS
     $moduleRelationships = $bm->getModuleRelationships($this->module);
     // create the writer
     $writer = new CsvWriter(array('delimiter' => ';', 'enclosure' => '"', 'encoding' => 'UTF-8', 'bom' => false, 'first_row_header' => true, 'trim' => true));
     $file = $this->module . '-Relationships.' . date('Y-m-d') . '.csv';
     //Open the csv file to write
     $writer->open(getcwd() . '/' . $file);
     $writer->writeRows($moduleRelationships);
     $writer->close();
     $output->writeln("<comment>All relationships for {$this->module} written in {$file}</comment>");
 }