data = $initial_data; $this->metadata = array( 'created_at' => current_time('mysql'), 'execution_id' => null, 'step_count' => 0 ); } /** * Set a value in the context * * @param string $key * @param mixed $value */ public function set($key, $value) { $this->data[$key] = $value; } /** * Get a value from the context * * @param string $key * @param mixed $default Default value if key doesn't exist * @return mixed */ public function get($key, $default = null) { return isset($this->data[$key]) ? $this->data[$key] : $default; } /** * Check if a key exists in the context * * @param string $key * @return bool */ public function has($key) { return isset($this->data[$key]); } /** * Remove a key from the context * * @param string $key */ public function remove($key) { unset($this->data[$key]); } /** * Get all data from the context * * @return array */ public function get_all() { return $this->data; } /** * Set metadata * * @param string $key * @param mixed $value */ public function set_metadata($key, $value) { $this->metadata[$key] = $value; } /** * Get metadata * * @param string $key * @param mixed $default * @return mixed */ public function get_metadata($key, $default = null) { return isset($this->metadata[$key]) ? $this->metadata[$key] : $default; } /** * Get all metadata * * @return array */ public function get_all_metadata() { return $this->metadata; } /** * Add an error to the context * * @param string $step_name Name of the step that generated the error * @param string $message Error message * @param array $details Additional error details */ public function add_error($step_name, $message, $details = array()) { $this->errors[] = array( 'step' => $step_name, 'message' => $message, 'details' => $details, 'timestamp' => current_time('mysql') ); } /** * Get all errors * * @return array */ public function get_errors() { return $this->errors; } /** * Check if context has errors * * @return bool */ public function has_errors() { return !empty($this->errors); } /** * Clear all errors */ public function clear_errors() { $this->errors = array(); } /** * Increment step counter */ public function increment_step_count() { $this->metadata['step_count']++; } /** * Create a clone of this context for branching (parallel execution) * * @return AIGH_Pipeline_Context */ public function clone_for_branch() { $clone = new self($this->data); $clone->metadata = $this->metadata; // Don't clone errors - each branch should have clean error state return $clone; } /** * Merge data from another context (for parallel execution merge) * * @param AIGH_Pipeline_Context $other_context * @param array $keys_to_merge Specific keys to merge, or empty for all */ public function merge_from($other_context, $keys_to_merge = array()) { if (empty($keys_to_merge)) { // Merge all data $this->data = array_merge($this->data, $other_context->get_all()); } else { // Merge specific keys only foreach ($keys_to_merge as $key) { if ($other_context->has($key)) { $this->set($key, $other_context->get($key)); } } } // Merge errors $this->errors = array_merge($this->errors, $other_context->get_errors()); } /** * Serialize context to JSON for storage * * @return string */ public function to_json() { return json_encode(array( 'data' => $this->data, 'metadata' => $this->metadata, 'errors' => $this->errors )); } /** * Create context from JSON * * @param string $json * @return AIGH_Pipeline_Context|WP_Error */ public static function from_json($json) { $decoded = json_decode($json, true); if (json_last_error() !== JSON_ERROR_NONE) { return new WP_Error('invalid_json', 'Failed to decode context JSON'); } $context = new self($decoded['data'] ?? array()); $context->metadata = $decoded['metadata'] ?? array(); $context->errors = $decoded['errors'] ?? array(); return $context; } /** * Get a summary of the context for debugging * * @return array */ public function get_summary() { return array( 'data_keys' => array_keys($this->data), 'metadata' => $this->metadata, 'error_count' => count($this->errors), 'has_errors' => $this->has_errors() ); } }