Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
Copy directives from #118
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Jan 19, 2023
1 parent 669bcdc commit 136bc17
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/directives/utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

function get_from_context( $expr, $context ) {
$path = explode( '.', $expr );
if ( count( $path ) > 0 && 'context' === $path[0] ) {
array_shift( $path );
$result = $context;
foreach( $path as $key ) {
$result = $result[$key];
}
}
return $result;
}
23 changes: 23 additions & 0 deletions src/directives/wp-bind.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

require_once __DIR__ . '/utils.php';

function process_wp_bind( &$tags, &$context ) {
if ( $tags->is_tag_closer() ) {
return;
}

$prefixed_attributes = $tags->get_attributes_by_prefix( 'wp-bind:' );

foreach( $prefixed_attributes as $name => $expr ) {
$attr_parts = explode( ':', $name );
if ( count( $attr_parts ) < 2 ) {
continue;
}
$bound_attr = $attr_parts[1];

// TODO: Properly parse $value.
$value = get_from_context( $expr, $context->get_context() );
$tags->set_attribute( $bound_attr, $value );
}
}
44 changes: 44 additions & 0 deletions src/directives/wp-context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

class WP_Directive_Context {
protected $stack = array( array() );

function __construct( $context = array() ) {
$this->set_context( $context );
}

public function get_context() {
return end( $this->stack );
}

public function set_context( $context ) {
array_push( $this->stack, array_replace_recursive( $this->get_context(), $context ) );
}

public function rewind_context() {
array_pop( $this->stack );
}
}

function process_wp_context( &$tags, &$context ) {
if ( 'WP-CONTEXT' === $tags->get_tag() ) {
if ( $tags->is_tag_closer() ) {
$context->rewind_context();
return;
}
$value = $tags->get_attribute( 'data' );
} else {
// TODO: Implement rewinding context upon matching closing tag.
$value = $tags->get_attribute( 'wp-context' );
}

if ( null === $value ) {
// No wp-context directive.
return;
}

$new_context = json_decode( $value, true );
// TODO: Error handling.

$context->set_context( $new_context );
}
27 changes: 27 additions & 0 deletions src/directives/wp-show.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

require_once __DIR__ . '/utils.php';

function process_wp_show( &$tags, &$context ) {
if ( $tags->is_tag_closer() ) {
return;
}

if ( 'WP-SHOW' === $tags->get_tag() ) {
$value = $tags->get_attribute( 'when' );
} else {
$value = $tags->get_attribute( 'wp-data' );
}

if ( null === $value ) {
return;
}

// TODO: Properly parse $value.
$show = get_from_context( $value, $context->get_context() );

if( ! $show ) {
// $content = $tags->get_content_inside_balanced_tags()
// $tags->set_content_inside_balanced_tags( '<template>' . $content . '</template>' );
}
}

0 comments on commit 136bc17

Please sign in to comment.