Generated Artifacts
WPKernel's generate command is a powerful tool that takes your wpk.config.ts file and produces a set of production-ready artifacts. These artifacts are the PHP and TypeScript files that make up the runtime of your WPKernel application. They are stored in the .generated directory in your project root.
This guide provides an overview of the different types of artifacts that WPKernel generates and explains how they work together.
PHP Artifacts
WPKernel generates several types of PHP files that handle the server-side logic of your application.
Plugin Loader
The plugin-loader.php file is the main entry point for your WPKernel plugin. It's responsible for:
- Bootstrapping the WPKernel integration.
- Registering the generated REST controllers with WordPress.
- Enqueuing the generated UI assets.
Here's an example of a generated plugin loader:
<?php
/**
* Plugin Name: Demo Plugin
* Description: Bootstrap loader for the Demo Plugin WPKernel integration.
* Version: 0.1.0
* Requires at least: 6.7
* Requires PHP: 8.1
* Text Domain: demo-plugin
* Author: WPKernel Contributors
* Author URI: https://github.com/wpkernel/wpkernel
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Generated by WPKernel CLI - edits between WPK:BEGIN AUTO and WPK:END AUTO are managed by the generator.
* Source: wpk.config.ts → plugin/loader
* @package DemoPlugin
*/
declare(strict_types=1);
namespace Demo\Plugin;
/**
* AUTO-GENERATED by WPKernel CLI.
* Edits between WPK:BEGIN AUTO and WPK:END AUTO will be overwritten.
* Source: wpk.config.ts → plugin/loader
*/
// WPK:BEGIN AUTO
if (!defined('ABSPATH')) {
exit;
}
/**
* Retrieve WPKernel REST controllers generated for this plugin.
* @return array<int, object>
*/
function get_wpkernel_controllers(): array
{
return new BooksController();
}
/** Register WPKernel REST controllers with WordPress. */
function register_wpkernel_routes(): void
{
$controllers = get_wpkernel_controllers();
foreach ($controllers as $controller) {
if (!method_exists($controller, 'register_routes')) {
continue;
}
$controller->register_routes();
}
}
/** Attach wpk hooks required for REST registration. */
function bootstrap_kernel(): void
{
add_action('rest_api_init', __NAMESPACE__ . '\register_wpkernel_routes');
}
bootstrap_kernel();
// WPK:END AUTOResource Controllers
For each resource you define in your wpk.config.ts file, WPKernel generates a corresponding REST controller. This controller is a PHP class that handles the HTTP requests for your resource's endpoints.
Here's an example of a generated controller for a books resource:
<?php
declare(strict_types=1);
/**
* AUTO-GENERATED by WPKernel CLI.
* Edits between WPK:BEGIN AUTO and WPK:END AUTO will be overwritten.
* Source: wpk.config.ts → resources.books
* Schema: book (manual)
* Route: [GET] /wpk/v1/books
* Route: [GET] /wpk/v1/books/:slug
* Route: [POST] /wpk/v1/books
* Route: [PUT] /wpk/v1/books/:slug
* Route: [DELETE] /wpk/v1/books/:slug
*/
namespace Demo\Plugin\Generated\Rest;
use WP_Error;
use WP_Post;
use WP_Query;
use WP_REST_Request;
use function is_wp_error;
// WPK:BEGIN AUTO
final class BooksController extends BaseController
{
public function get_resource_name(): string
{
return 'books';
}
public function get_schema_key(): string
{
return 'book';
}
public function get_rest_args(): array
{
return [];
}
/**
* Handle [GET] /wpk/v1/books.
* @wp-kernel route-kind list
*/
public function getWpkV1Books(WP_REST_Request $request)
{
// ... implementation ...
}
/**
* Handle [GET] /wpk/v1/books/:slug.
* @wp-kernel route-kind get
*/
public function getWpkV1BooksSlug(WP_REST_Request $request)
{
// ... implementation ...
}
/**
* Handle [POST] /wpk/v1/books.
* @wp-kernel route-kind create
* @wp-kernel resource.wpPost.mutation create
*/
public function postWpkV1Books(WP_REST_Request $request)
{
// ... implementation ...
}
/**
* Handle [PUT] /wpk/v1/books/:slug.
* @wp-kernel route-kind update
* @wpk-kernel resource.wpPost.mutation update
*/
public function putWpkV1BooksSlug(WP_REST_Request $request)
{
// ... implementation ...
}
/**
* Handle [DELETE] /wpk/v1/books/:slug.
* @wp-kernel route-kind remove
* @wp-kernel resource.wpPost.mutation delete
*/
public function deleteWpkV1BooksSlug(WP_REST_Request $request)
{
// ... implementation ...
}
}
// WPK:END AUTOUI Artifacts
In addition to PHP files, WPKernel can also generate TypeScript and .tsx files for your user interface.
DataView Screens
If you configure a ui section in your resource definition in wpk.config.ts, WPKernel will generate a React component for displaying your resource in a ResourceDataView.
Here's an example of a generated JobsAdminScreen.tsx file:
export const jobsadminscreenRoute = '/admin.php?page=wpk-jobs';
import { WPKernelUIProvider, useWPKernelUI } from '@wpkernel/ui';
import { ResourceDataView } from '@wpkernel/ui/dataviews';
import { wpk } from '@/bootstrap/kernel';
import { job } from '@/resources/job';
function JobsAdminScreenContent() {
const runtime = useWPKernelUI();
return (
<ResourceDataView
resource={job}
config={job.ui?.admin?.dataviews}
runtime={runtime}
/>
);
}
export function JobsAdminScreen() {
const runtime = kernel.getUIRuntime?.();
if (!runtime) {
throw new [`WPKernelError`](/api/@wpkernel/core/classes/WPKernelError.md)('DeveloperError', {
message: 'UI runtime not attached.',
context: { resourceName },
});
}
return (
<WPKernelUIProvider runtime={runtime}>
<JobsAdminScreenContent />
</WPKernelUIProvider>
);
}How Artifacts are Generated
The wpk generate command reads your wpk.config.ts file and uses a series of "builders" to generate the artifacts. Each builder is responsible for creating a specific type of file (e.g., a PHP controller, a TypeScript type definition).
The builders use the information in your wpk.config.ts file to construct an Abstract Syntax Tree (AST) for the code they need to generate. This AST is then printed as a string to the corresponding artifact file. This approach allows WPKernel to generate complex, well-structured code that is easy to read and debug.
