Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
jgniecki committed Mar 22, 2021
1 parent c92be59 commit 6087fbb
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
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
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
# Lazy-File-loader
# Lazy File Loader [![Packagist](https://img.shields.io/packagist/dt/dev-lancer/lazy-file-loader.svg)](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.
//)
```
26 changes: 26 additions & 0 deletions composer.json
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/"
}
}
}
18 changes: 18 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions src/FileLoader.php
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);
}
}
51 changes: 51 additions & 0 deletions src/LazyCharsLoader.php
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();
}
}
25 changes: 25 additions & 0 deletions src/LazyFileLoaderInterface.php
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);
}
Loading

0 comments on commit 6087fbb

Please sign in to comment.