main
Robert 8 months ago
parent ea048ab9e7
commit f40d45d0d3
  1. 4
      BcMath.php
  2. 4
      LazyCollection.php
  3. 22
      LazyObject.php
  4. 6
      ex/Example2.php
  5. 9
      ex/MyClass.php
  6. 4
      libs/Dollars.php
  7. 17
      libs/LazyCollection.php
  8. 32
      libs/LazyObject.php
  9. 11
      libs/autoload.php

@ -6,7 +6,7 @@
use BcMath\Number;
require "libs/Dollars.php";
require "libs/autoload.php";
function init(): Number {
$num1 = new Number('2200');
@ -16,7 +16,7 @@ function init(): Number {
return $money;
}
$d = new Dollars();
$d = new libs\Dollars();
$c = init();
echo $d->formatAsUSD($c);
echo "\n";

@ -1,8 +1,8 @@
<?php
require "libs/LazyCollection.php";
require "libs/autoload.php";
$lazyCollection = LazyCollection::make(function () {
$lazyCollection = libs\LazyCollection::make(function () {
try {
$handle = fopen('huge.txt', 'r');
if (!$handle) throw new Exception("Could not open file");

@ -1,22 +1,12 @@
<?php
require "libs/LazyObject.php";
require "libs/autoload.php";
class MyClass {
public function __construct(public int $num) {}
public function someMethod() {
return $this->num;
}
}
class Example2 {
public function hi() { echo "Hi!"; }
}
$lazy = new LazyObject();
$object = $lazy::proxy(MyClass::class, 301);
$lazyA = new libs\LazyObject(ex\MyClass::class);
$objectA = $lazyA->proxy(301);
// The object is initialized only when accessed
echo $object->someMethod() . "\n";
echo $objectA->someMethod() . "\n";
$objectB = $lazy::proxy(Example2::class);
$lazyB = new libs\LazyObject(ex\Example2::class);
$objectB = $lazyB->proxy();
$objectB->hi();

@ -0,0 +1,6 @@
<?php
namespace ex;
class Example2 {
public function hi() { echo "Hi!"; }
}

@ -0,0 +1,9 @@
<?php
namespace ex;
class MyClass {
public function __construct(public int $num) {}
public function someMethod() {
return $this->num;
}
}

@ -1,4 +1,6 @@
<?php
namespace libs;
use BcMath\Number;
class Dollars {
@ -14,7 +16,7 @@ class Dollars {
public function intlUSD(Number $money): string {
if (class_exists('NumberFormatter')) {
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$formatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY);
return $formatter->formatCurrency($this->moneyAsFloat($money), 'USD');
}
}

@ -1,11 +1,14 @@
<?php
class LazyCollection implements IteratorAggregate
namespace libs;
class LazyCollection implements \IteratorAggregate
{
protected $source;
public function __construct($source = null)
{
if ($source instanceof Closure || $source instanceof self) {
if ($source instanceof \Closure || $source instanceof self) {
$this->source = $source;
} elseif (is_null($source)) {
$this->source = static::empty();
@ -14,19 +17,19 @@ class LazyCollection implements IteratorAggregate
}
}
public function getIterator(): Traversable
public function getIterator(): \Traversable
{
return $this->makeIterator($this->source);
}
protected function makeIterator($source)
{
if ($source instanceof Closure) {
if ($source instanceof \Closure) {
$source = $source();
}
if (is_array($source)) {
return new ArrayIterator($source);
return new \ArrayIterator($source);
}
return $source;
@ -34,7 +37,7 @@ class LazyCollection implements IteratorAggregate
public static function make($source = null)
{
return new static($source instanceof Closure ? $source() : $source);
return new static($source instanceof \Closure ? $source() : $source);
}
public function each(callable $callback)
@ -52,7 +55,7 @@ class LazyCollection implements IteratorAggregate
{
if (is_array($items)) {
return $items;
} elseif ($items instanceof Traversable) {
} elseif ($items instanceof \Traversable) {
return iterator_to_array($items);
} elseif (is_object($items) && method_exists($items, 'toArray')) {
return $items->toArray();

@ -1,26 +1,24 @@
<?php
namespace libs;
class LazyObject {
public static function proxy(string $className, mixed $c = null) {
public function __construct(public string $className) {}
public function proxy(mixed ...$args) {
$className = $this->className;
if (!class_exists($className, true)) {
throw new InvalidArgumentException("Class {$className} does not exist");
throw new \InvalidArgumentException("Class {$className} does not exist");
}
$reflector = new ReflectionClass($className);
$reflector = new \ReflectionClass($className);
if (!$reflector->isInstantiable()) {
throw new InvalidArgumentException("Class {$className} cannot be instantiated");
throw new \InvalidArgumentException("Class {$className} cannot be instantiated");
}
$constructor = $reflector->getConstructor();
// If the class has a constructor with required parameters
if ($constructor && $constructor->getNumberOfRequiredParameters() > 0) {
$initializer = static function (mixed $i) use ($className) {
return new $className($i);
};
return $reflector->newLazyProxy(fn() => $initializer($c));
} else {
$initializer = static function () use ($className) {
return new $className();
};
return $reflector->newLazyProxy($initializer);
}
$initializer = static function () use ($className, $args) {
return new $className(...$args);
};
return $reflector->newLazyProxy($initializer);
}
}

@ -0,0 +1,11 @@
<?php
spl_autoload_register(function ($className) {
// Project base directory
$baseDir = dirname(__DIR__,1) . '/';
// Replace namespace separator with directory separator
$file = $baseDir . str_replace('\\', '/', $className) . '.php';
// If the file exists, require it
if (file_exists($file)) {
require_once $file;
}
});
Loading…
Cancel
Save