diff --git a/src/Model/SourceLocation.php b/src/Model/SourceLocation.php index d7a9659..7217212 100644 --- a/src/Model/SourceLocation.php +++ b/src/Model/SourceLocation.php @@ -52,6 +52,21 @@ public function __construct($message, $path, $line, array $context = []) $this->context = $context; } + /** + * Create a source location from your current location. + * + * @param string $message + * @param array $context + * + * @return SourceLocation + */ + public static function createHere($message, array $context = []) + { + $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1); + + return new self($message, $trace[0]['file'], $trace[0]['line'], $context); + } + /** * @return string */ diff --git a/tests/Unit/Model/SourceLocationTest.php b/tests/Unit/Model/SourceLocationTest.php new file mode 100644 index 0000000..9b234bf --- /dev/null +++ b/tests/Unit/Model/SourceLocationTest.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\Extractor\Tests\Unit\Model; + +use Translation\Extractor\Model\SourceLocation; + +class SourceLocationTest extends \PHPUnit_Framework_TestCase +{ + public function testCreateHere() + { + $location = SourceLocation::createHere('foobar', ['foo' => 'bar']); + + $this->assertContains('tests/Unit/Model/SourceLocationTest.php', $location->getPath()); + $this->assertEquals(20, $location->getLine()); + + $this->assertEquals('foobar', $location->getMessage()); + $this->assertEquals(['foo' => 'bar'], $location->getContext()); + } +}