Skip to content

Commit

Permalink
Add the possibility to use the native IDE autocompletion to list all …
Browse files Browse the repository at this point in the history
…helpers at once and call them as a simple function.

To activate this feature in your favorite IDE:
add in each view file: /** @var $this PhpEcho */ as the first line of code and normally the magic works.

So this is now equivalent:
<?= $this('$hsc', 'any value to escape') ?>
<?= $this->hsc('any value to escape') ?>

For the second version, you get the direct access through autocompletion.
  • Loading branch information
rawsrc committed Apr 12, 2020
1 parent 1c61f94 commit 89a5d41
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
32 changes: 28 additions & 4 deletions PhpEcho.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
* @method mixed raw($p) Return the raw value from a PhpEcho block
* @method mixed hsc($p) Escape the value in parameter (scalar, array, stringifyable)
* @method bool isScalar($p)
* @method string selected($p, $ref) Return " selected " if $p == $ref
* @method string checked($p, $ref) Return " checked " if $p == $ref
*/
class PhpEcho
implements ArrayAccess
Expand Down Expand Up @@ -220,9 +227,6 @@ public function setCode(string $code)
public function __invoke(string $helper, ...$args)
{
if ($helper !== '') {
if ( ! empty(self::$helpers_file_to_inject)) {
self::injectHelpers();
}
if (self::isHelper($helper)) {
if (empty($this->bound_helpers)) {
$this->bound_helpers = self::bindHelpersTo($this);
Expand All @@ -243,6 +247,21 @@ public function __invoke(string $helper, ...$args)
return null;
}

/**
* @param $name
* @param $arguments
*/
public function __call($name, $arguments)
{
if (self::isHelper($name)) {
return $this->__invoke($name, ...$arguments);
} elseif (self::isHelper('$'.$name)) {
return $this->__invoke('$'.$name, ...$arguments);
} else {
return null;
}
}

/**
* Magic method that returns a string instead of current instance of the class in a string context
*/
Expand Down Expand Up @@ -354,7 +373,12 @@ public static function getHelperTypes(string $helper_name): array
*/
public static function isHelper(string $helper_name): bool
{
return isset(self::$helpers[$helper_name]);
if (isset(self::$helpers[$helper_name])) {
return true;
} else {
self::injectHelpers();
return isset(self::$helpers[$helper_name]);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions stdHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
return is_scalar($p) || (is_object($p) && method_exists($p, '__toString'));
};
$helpers['$is_scalar'] = [$is_scalar, HELPER_RETURN_ESCAPED_DATA];
$helpers['isScalar'] = $helpers['$is_scalar']; // alias for method call


/**
Expand Down

0 comments on commit 89a5d41

Please sign in to comment.