This repository has been archived by the owner on May 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
8 changed files
with
394 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
composer.phar | ||
/vendor/ | ||
|
||
/.idea/ | ||
/example.txt | ||
/test.php | ||
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control | ||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file | ||
# composer.lock |
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 |
---|---|---|
@@ -1 +1,66 @@ | ||
# Lazy-File-loader | ||
# Lazy File Loader [](https://packagist.org/packages/dev-lancer/lazy-file-loader) | ||
|
||
## Installation | ||
This library can installed by issuing the following command: | ||
```bash | ||
composer require dev-lancer/lazy-file-loader | ||
``` | ||
|
||
## Example | ||
|
||
###class LazyCharsLoader | ||
```php | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
use DevLancer\LazyFileLoader\LazyCharsLoader; | ||
|
||
$file = "LICENSE"; //path to file | ||
|
||
$loader = new LazyCharsLoader($file); //offset: 0 | ||
|
||
print_r($loader->load()); //Length: -1, Output: the entire LICENSE file | ||
print_r($loader->load(11)); //Length: 11, Output: "MIT License" | ||
|
||
$loader = new LazyCharsLoader($file, 15);//offset: 15 | ||
print_r($loader->load(35)); //Length: 35, Output: "Copyright (c) 2021 DeveloperLancer" | ||
|
||
$loader = new LazyCharsLoader($file, -1);//offset: -1 | ||
print_r($loader->load(11)); //Length: 11, Output: "SOFTWARE." | ||
``` | ||
|
||
###class LazyLineLoader | ||
```php | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
use DevLancer\LazyFileLoader\LazyLineLoader; | ||
|
||
$file = "LICENSE"; //path to file | ||
|
||
$loader = new LazyLineLoader($file); //offset: 0, separator: \n | ||
print_r($loader->load()); //Length: -1, Output: array[] the entire LICENSE file | ||
print_r($loader->load(1)); //Length: 1, | ||
//Output: Array ( | ||
// [0] => MIT License | ||
//) | ||
|
||
$loader = new LazyLineLoader($file, 2);//offset: 2 | ||
print_r($loader->load(1)); //Length: 1 | ||
//Output: Array ( | ||
// [0] => Copyright (c) 2021 DeveloperLancer | ||
//) | ||
|
||
print_r($loader->load(3)); //Length: 3 | ||
//Output: Array ( | ||
// [0] => Copyright (c) 2021 DeveloperLancer | ||
// [1] => | ||
// [2] => Permission is hereby granted, free of charge, to any person obtaining a copy | ||
//) | ||
|
||
$loader = new LazyLineLoader($file, -2);//offset: -2 | ||
print_r($loader->load(1)); //Length: 10, | ||
//Output: Array ( | ||
// [0] => SOFTWARE. | ||
//) | ||
``` |
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,26 @@ | ||
{ | ||
"name": "devlancer/lazy-file-loader", | ||
"type": "library", | ||
"license": "MIT", | ||
"description": "", | ||
"minimum-stability": "stable", | ||
"authors": [ | ||
{ | ||
"name": "Jakub Gniecki", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.4" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"DevLancer\\LazyFileLoader\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"DevLancer\\LazyFileLoader\\Tests\\": "tests/" | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,100 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* @author Jakub Gniecki | ||
* @copyright Jakub Gniecki <[email protected]> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
|
||
namespace DevLancer\LazyFileLoader; | ||
|
||
|
||
/** | ||
* Class FileLoader | ||
* @package DevLancer\LazyFileLoader | ||
*/ | ||
abstract class FileLoader implements LazyFileLoaderInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected string $path; | ||
|
||
/** | ||
* @var mixed|null | ||
*/ | ||
protected $file = null; | ||
|
||
/** | ||
* @var mixed|null | ||
*/ | ||
protected $data = null; | ||
|
||
/** | ||
* FileLoader constructor. | ||
* @param string $path | ||
* @param null $data | ||
*/ | ||
public function __construct(string $path, $data = null) | ||
{ | ||
if ($path === "") | ||
throw new \InvalidArgumentException('An empty file name is not valid to be located.'); | ||
|
||
if (!file_exists($path)) | ||
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $path)); | ||
|
||
$this->path = $path; | ||
$this->data = $data; | ||
$this->open(); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getFileSize(): int | ||
{ | ||
return filesize($this->getPath()); | ||
} | ||
|
||
/** | ||
* @return mixed|null | ||
*/ | ||
public function getData() | ||
{ | ||
return $this->data; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
protected function open() | ||
{ | ||
$this->file = @fopen($this->getPath(), 'r'); | ||
if (is_bool($this->file) || $this->file === null) | ||
throw new \RuntimeException(sprintf('The file "%s" does not open.', $this->path)); | ||
} | ||
/** | ||
* @return string | ||
*/ | ||
public function getPath(): string | ||
{ | ||
return $this->path; | ||
} | ||
|
||
/** | ||
* @return mixed|null | ||
*/ | ||
public function getFile() | ||
{ | ||
return $this->file; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function __destruct() | ||
{ | ||
fclose($this->file); | ||
} | ||
} |
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,51 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* @author Jakub Gniecki | ||
* @copyright Jakub Gniecki <[email protected]> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
|
||
namespace DevLancer\LazyFileLoader; | ||
|
||
|
||
/** | ||
* Class LazyCharsLoader | ||
* @package DevLancer\LazyFileLoader | ||
*/ | ||
class LazyCharsLoader extends FileLoader | ||
{ | ||
/** | ||
* LazyCharsLoader constructor. | ||
* @param string $path | ||
* @param int $offset | ||
*/ | ||
public function __construct(string $path, int $offset = 0) | ||
{ | ||
parent::__construct($path, $offset); | ||
} | ||
|
||
/** | ||
* @param int $length length | ||
* @param int $buffer_size | ||
* @return false|string | ||
*/ | ||
public function load($length = -1, int $buffer_size = 512) | ||
{ | ||
$offset = $this->getOffset(); | ||
|
||
if ($offset < 0) | ||
$offset = $this->getFileSize() + $this->data - $length + 1; | ||
|
||
return stream_get_contents($this->file, $length, $offset); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getOffset(): int | ||
{ | ||
return $this->getData(); | ||
} | ||
} |
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,25 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* @author Jakub Gniecki | ||
* @copyright Jakub Gniecki <[email protected]> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
|
||
namespace DevLancer\LazyFileLoader; | ||
|
||
|
||
/** | ||
* Interface LazyFileLoaderInterface | ||
* @package DevLancer\LazyFileLoader | ||
*/ | ||
interface LazyFileLoaderInterface | ||
{ | ||
/** | ||
* @param $resource | ||
* @param int $buffer_size | ||
* @return mixed | ||
*/ | ||
public function load($resource, int $buffer_size = 512); | ||
} |
Oops, something went wrong.