-
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.
Get the right source if extending the Todo class
- Loading branch information
1 parent
18c2940
commit b0042ce
Showing
4 changed files
with
105 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Robertology\TodoOrDie\System; | ||
|
||
class Trace { | ||
|
||
/** | ||
* Get a trace starting from the first call to the given class | ||
* | ||
* Considers parent & child class to be same as class | ||
* Note: this is a backtrace, so "start" means "going backward" | ||
*/ | ||
static public function fromOutside(string $class) : array { | ||
$trace = static::startAt($class); | ||
|
||
foreach ($trace as $key => $entry) { | ||
$check = $entry['class'] ?? ''; | ||
if (empty($check) || ! static::_isA($check, $class)) { | ||
$key--; | ||
break; | ||
} | ||
} | ||
|
||
return isset($key) ? array_splice($trace, $key) : []; | ||
} | ||
|
||
/** | ||
* Get a trace starting at calls involving the given class | ||
* | ||
* Considers parent & child class to be same as class | ||
* Note: this is a backtrace, so "start" means "going backward" | ||
*/ | ||
static public function startAt(string $class) : array { | ||
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); | ||
|
||
$check = $trace[0]; | ||
while (! static::_isA($class, $check['class'] ?? '')) { | ||
array_shift($trace); | ||
$check = reset($trace); | ||
} | ||
|
||
|
||
// renumber the keys with array_values() | ||
return array_values($trace); | ||
} | ||
|
||
/** | ||
* Companre the class names for match or subclass in either direction | ||
*/ | ||
static protected function _isA(string $a, string $b) : bool { | ||
return is_a($a, $b, true) || | ||
is_a($b, $a, true); | ||
} | ||
|
||
} |
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,28 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Robertology\TodoOrDie; | ||
|
||
use Robertology\TodoOrDie\ { | ||
System\Trace, | ||
Todo, | ||
}; | ||
|
||
class TodoIdGenerator { | ||
|
||
private Todo $_todo; | ||
|
||
public function __construct(Todo $todo) { | ||
$this->_todo = $todo; | ||
} | ||
|
||
public function __invoke() : string { | ||
$entry = Trace::fromOutside(get_class($this->_todo))[0] ?? []; | ||
|
||
$file = $entry['file'] ?? ''; | ||
$line = $entry['line'] ?? 0; | ||
|
||
return "{$file}:{$line}"; | ||
} | ||
|
||
} |
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