Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
326 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\CsvBlueprint\Tools; | ||
|
||
class Exception extends \RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\CsvBlueprint\Tools; | ||
|
||
use JBZoo\Utils\Cli; | ||
|
||
final class PreloadBuilder | ||
{ | ||
private array $files = []; | ||
private array $excludes = []; | ||
|
||
public function __construct( | ||
private bool $enableOpcacheCompiler = false, | ||
) { | ||
} | ||
|
||
public function saveToFile(string $filename, bool $showInfo = false): void | ||
{ | ||
$files = $this->buildFilelist(); | ||
$lines = \array_merge($this->buildHeader(), $files); | ||
|
||
\file_put_contents($filename, \implode(\PHP_EOL, $lines) . \PHP_EOL); | ||
|
||
if ($showInfo) { | ||
Cli::out('Included files: ' . \count($files)); | ||
} | ||
} | ||
|
||
public function setExcludes(array $excludes): self | ||
{ | ||
$this->excludes = $excludes; | ||
return $this; | ||
} | ||
|
||
public function setFiles(array $files): self | ||
{ | ||
$this->files = $files; | ||
return $this; | ||
} | ||
|
||
private function buildFilelist(): array | ||
{ | ||
$files = []; | ||
$fillList = \array_merge([ | ||
\dirname(__DIR__, 2) . '/vendor/autoload.php', | ||
], $this->files); | ||
|
||
foreach ($fillList as $path) { | ||
if ($this->isExcluded($path)) { | ||
continue; | ||
} | ||
|
||
$files[] = $this->enableOpcacheCompiler | ||
? "\\opcache_compile_file('{$path}');" | ||
: "require_once '{$path}';"; | ||
} | ||
|
||
return \array_unique($files); | ||
} | ||
|
||
private function isExcluded(string $path): bool | ||
{ | ||
return \in_array($path, $this->excludes, true); | ||
} | ||
|
||
private function buildHeader(): array | ||
{ | ||
$header = [ | ||
'<?php declare(strict_types=1);', | ||
'', | ||
]; | ||
|
||
if ($this->enableOpcacheCompiler) { | ||
$header = \array_merge($header, [ | ||
"if (!\\function_exists('opcache_compile_file') ||", | ||
" !\\ini_get('opcache.enable') ||", | ||
" !\\ini_get('opcache.enable_cli')", | ||
') {', | ||
" echo 'Opcache is not available.';", | ||
' die(1);', | ||
'}', | ||
'', | ||
]); | ||
} | ||
|
||
return $header; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\CsvBlueprint\Tools; | ||
|
||
final class RandomCsvGenerator | ||
{ | ||
private const MIN = 1; | ||
private const MAX = 10_000; | ||
|
||
public function __construct( | ||
private int $rows, | ||
private string $filePath, | ||
private array $columns, | ||
) { | ||
} | ||
|
||
public function generateCsv(): void | ||
{ | ||
$fileHandle = \fopen($this->filePath, 'w'); | ||
if ($fileHandle === false) { | ||
throw new Exception("Can't open file: {$this->filePath}"); | ||
} | ||
|
||
\fputcsv($fileHandle, $this->columns); | ||
|
||
for ($i = 0; $i < $this->rows; $i++) { | ||
$rowData = []; | ||
|
||
foreach (\array_keys($this->columns) as $columnIndex) { | ||
$rowData[$columnIndex] = \random_int(self::MIN, self::MAX); | ||
} | ||
|
||
\fputcsv($fileHandle, $rowData); | ||
} | ||
|
||
\fclose($fileHandle); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.