public function initialize(Config $config, $directory, $filenameWithDate = false) { $this->setFilename($config, $directory, $filenameWithDate); $this->csvWriter = new CsvWriter($config->getCsvDialect() ?: Dialect::createExcelDialect()); $this->csvWriter->open($this->filename); $this->hasHeader = false; }
public function testWritingExistingFileHandler() { $filename = __DIR__ . '/Fixtures/testWriteStream1.csv'; if (file_exists($filename)) { unlink($filename); } $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"; $stream = fopen($filename, 'wb'); $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->open($stream)); $this->assertTrue($writer->isFileOpened()); $this->assertInternalType('resource', $writer->getFileHandler()); $this->assertInstanceOf('CSanquer\\ColibriCsv\\CsvWriter', $writer->writeRows($csvArray)); $this->assertInstanceOf('CSanquer\\ColibriCsv\\CsvWriter', $writer->close()); $this->assertEquals($expected, file_get_contents($filename)); if (file_exists($filename)) { unlink($filename); } }
/** * @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; }
$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";
/** * 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>"); }