Skip to content

Latest commit

 

History

History
201 lines (151 loc) · 13.6 KB

CLAUDE.md

File metadata and controls

201 lines (151 loc) · 13.6 KB

CLAUDE.md

This file provides guidance to Agents when working with code in this repository.

What This Is

Prado is a component-based, event-driven PHP web framework. All framework source lives under framework/ (PSR-4 namespace Prado\). Tests mirror that structure under tests/unit/. Current version: 4.3.3; next release: 4.4.0.

Commands

# Code style check (dry-run)
vendor/bin/php-cs-fixer fix --dry-run framework/

# Code style fix (apply)
vendor/bin/php-cs-fixer fix framework/

# Static analysis
vendor/bin/phpstan analyse --memory-limit=1G

# Run all unit tests
vendor/bin/phpunit --testsuite unit

# Run tests for a specific class, function, or directory
vendor/bin/phpunit --testsuite unit --filter <test function, class, or directory>

# Code coverage (phpunit.xml declares framework/ as the source; the script sets XDEBUG_MODE)
composer coverage            # text summary
composer coverage-html       # HTML report in build/coverage
composer coverage -- --filter <class> --coverage-filter <path>   # narrow a run

# javascript commands
composer jsfix           # js code style fix
composer jstest          # vitest unit test

# Generate API documentation
composer gendoc

# Playwright functional tests (PHP server on port 8037 starts automatically)
composer functest        # Chromium only (faster for local iteration)
composer functionaltest  # all three browsers (Chromium, Firefox, WebKit)

Full Check (required before git commit)

Run these four checks in order — all must pass:

  1. PHP compile check (php -l)
  2. vendor/bin/php-cs-fixer fix --dry-run framework/
  3. vendor/bin/phpstan analyse --memory-limit=1G
  4. vendor/bin/phpunit --testsuite unit

Never add or change phpunit command options when unit testing — only run project unit tests as specified above. When testing a single class or cluster, only run tests for that class/directory. Measuring coverage is the exception: use the composer coverage scripts, narrowing a run with --filter and --coverage-filter.

Architecture

Core Abstractions

  • TComponent (framework/TComponent.php) — base class for nearly everything. Implements the property system (getters/setters via magic __get/__set), event system (attachEventHandler/raiseEvent), priority based behavior attachment, __clone, __sleep/__wakeup, and _getZappableSleepProps.
  • TApplication (framework/TApplication.php) — top-level service container. Modules and services register with it via XML (or PHP) configuration; tests bootstrap one via tests/test_tools/phpunit_bootstrap.php.
  • TModule / TService — pluggable units registered with TApplication.

Layer Breakdown

TApplication
 └─ Web Layer      framework/Web/
      THttpRequest, THttpResponse, THttpSession
      TUrlManager / TUrlMapping (routing)
      TAssetManager
      Services/      (TPageService, TJsonService, TFeedService, TSoapService)
      UI/            (TControl, TPage, TTemplateControl, WebControls/, ActiveControls/, JuiControls/)
 └─ Data Layer     framework/Data/
      TDbConnection  (PDO wrapper)
      DataGateway/
 └─ Caching        framework/Caching/    (APC, Database, Etcd, Memcached, Redis, file-based)
 └─ Security       framework/Security/   (RBAC, TAuthManager, TSecurityManager, TPermissionsManager)
 └─ I18N           framework/I18N/       (TGlobalization, MessageSource, CultureInfo)
 └─ Xml            framework/Xml/        (TXmlDocument/TXmlElement with XPath, DOM compat, ArrayAccess)
 └─ Util           framework/Util/       (logging, behaviors, cron, mutexes, helpers, TCallChain, TVarDumper)
 └─ PHPStan        framework/PHPStan/    (custom static-analysis extensions for dynamic Prado properties)

Application Lifecycle

onConfiguration → onInitComplete → onBeginRequest → onLoadState → onLoadStateComplete → onAuthentication → onAuthenticationComplete → onAuthorization → onAuthorizationComplete → onPreRunService → runService → onSaveState → onSaveStateComplete → onPreFlushOutput → flushOutput → onEndRequest / onError

Page Lifecycle (via TPageService)

onPreInit → initRecursive → onInitComplete → loadPageState (POST/Callback) → processPostData (POST/Callback) → onPreLoad → loadRecursive → processPostData (POST/Callback) → raiseChangedEvents (POST/Callback) → raisePostBackEvent (POST-only) → processCallbackEvent (Callback-only) → onLoadComplete → preRenderRecursive → onPreRenderComplete → savePageState → onSaveStateComplete → renderControl (GET/POST) / renderCallbackResponse (Callback-only) → unloadRecursive

Event / Property / Behavior System

  • Properties: defined via getXxx()/setXxx() — no public fields.
  • Events:
    • on prefix — onEventName(TEventParameter $param) raised with $this->raiseEvent('OnEventName', $this, $param).
    • dy prefix — dynamic events called on attached/active Behaviors (e.g., dyShouldContinue, dyClone, dyValidate).
    • fx prefix — global events, auto-registered depending on getAutoGlobalListen() (e.g., fxAttachClassBehavior).
  • Behaviors and on events are called in specified Collections Priority order.
  • @method PHPDoc tags document dynamic dy- events on classes.

Custom PHPStan Extensions

framework/PHPStan/DynamicMethodsClassReflectionExtension and TComponentIsaTypeSpecifyingExtension teach PHPStan about the dynamic property/event/behavior system. Update these when adding new dynamic accessors.

Naming Conventions

Thing Convention Example
Classes TPascalCase TComponent, TApplication
Methods camelCase getComponent
Variables camelCase $componentName
Class Constants SCREAMING_SNAKE_CASE MAX_RETRY_COUNT
Enumerated Constants PascalCase DeepSkyBlue
Class properties _camelCase _propertyOfClass, _styleFieldNames
Namespaces Prado\{Module} Prado\Web\UI\TControl
Unit test namespaces Prado\Test\Unit\{Directory} Prado\Test\Unit\Web\UI\TControlTest
Web Page templates .page with .php backing Home.page, Home.php
MasterClass and Template files .tpl with .php backing MyPortlet.tpl, MyPortlet.php
TControl tag prefix <com: <com:TMain />

Important Rules

  • framework/classes.php — must be updated whenever a new class is added to the framework.
  • if statements always use a block ({}), never a single-line body.
  • @ error suppression — Prado::phpErrorHandler throws TPhpErrorException for every unmasked warning. Keep @ on a call whose failure is expected and whose return value is checked; see Error Suppression.
  • Optional extensions — composer.json requires ctype, dom, intl, json, pcre and spl; every other extension, mbstring and iconv included, is only suggested and may be absent. Never call mb_* directly: measure, cut, and case-map UTF-8 text through Prado\Util\TMultibyteString, which uses mbstring when loaded and pcre and intl otherwise. Never call iconv* directly: convert encodings through Prado\Util\TUtf8Converter, which uses iconv when loaded and intl's \UConverter otherwise. Any other suggested extension (iconv, openssl, zlib, ...) is checked with extension_loaded() once and has a fallback or a clear exception.
  • Error codes for PRADO exceptions are defined in framework/Exceptions/messages/messages.txt (English master). Framework messages are English; messages-<lang>.txt in the same directory holds only the keys thrown as THttpException, which the visitor sees (IUserException marks visitor-facing exceptions). Translations of the developer-facing messages are the pradosoft/prado-dev-errormessages package. In this repository, create, update, and delete keys in the English master only; the package is synced in one batch before each PRADO tag and release, never per change.
  • Exception placement — Prado\Exceptions holds exception kinds: classes that classify an error (TInvalidDataValueException, TConfigurationException, THttpException, TIOException) and that code across the framework throws or catches without knowing which component raised it. An exception that belongs to one component lives in that component's namespace beside the class that throws it: its name carries the component's name, only that component throws it, or it carries the component's data (THttpClientException, TMutexException, TRestException, TInputValidationException). Every exception extends the closest kind in Prado\Exceptions, never \Exception directly. No per-directory Exception/ subfolders. See Placement.
  • Backward compatibility — all changes must be backward compatible for point releases. Minor releases can have breaking changes, but minimize the breaks.
  • HISTORY.md entries — an entry's length is proportional to the change's importance. A bug fix or a small or internal change is one short sentence; a major feature takes a few sentences naming its main classes and properties. Keep issue numbers, removed or renamed API, and UPGRADE.md pointers; implementation reasoning belongs in docblocks and agents/.
  • @since tag — use the next release version (4.4.0) when adding new methods or classes; omit the method tag when it matches the class tag.
  • Uniform Access Principal - Self Encapsulation is required; for an example see framework/TApplication.php
  • **Extract Method → Predicate/Guard Clause (Fowler) is suggested
  • Method Doc Blocks must be tight, and have at minimum one sentence in the description.
  • Documentation additions/changes/removals should be integrated into the whole, at each level (of detail).
  • The per directory "<dir_path>/CLAUDE.md" is found at "agents/<dir_path>/INDEX.md" to keep the framework uncluttered.
  • All new and changed WebControls must be audited for accessibility at least once during development.

Test Bootstrap

Tests require a running TApplication. The bootstrap (tests/test_tools/phpunit_bootstrap.php) instantiates one from tests/unit/Security/app/; it may change through the unit tests. Database tests need MySQL/PostgreSQL initialized from tests/initdb_mysql.sql / tests/initdb_pgsql.sql.

Testing

  • Unit test classes use the Prado\Test\Unit\ namespace, autoloaded by Composer (autoload-dev PSR-4 → tests/unit/).
    • The namespace follows the directory: tests/unit/Web/UI/TControlTest.php → Prado\Test\Unit\Web\UI\TControlTest.
    • A class used by another file lives in its own file named after the class. Fixtures used only by one test file stay in that file.
    • Directories holding classes use PascalCase, except where they mirror a lowercase framework namespace (I18N/core).
    • Class names in strings, templates (<com:…>), and XML configuration are fully qualified; use Foo::class in PHP.
    • Global-namespace fixtures (Security/app/prado3stubs/, Exceptions/TErrorHandlerTestGlobalClass.php) and PHPStan/Fixtures/ are excluded from the classmap and are not autoloaded.
    • Test classes are inside Prado\*: a default log category is the test class, and a record fixture needs const TABLE.
    • Helper classes in their own file must not end in Test; phpunit collects *Test.php files as tests.
  • Use Prado\Test\Unit\PradoUnit infrastructure for:
    • access to an object's protected/private properties
    • invoking protected/private methods
    • restore global variables to their initial state
    • save and restore all of an object's properties
    • Harness/ (Prado\Test\Unit\Harness\) holds test classes designed for reuse across the Prado tests
    • Traits for unit test classes are in Harness/Traits/

Code Style

  • Indentation: tabs (not spaces)
  • Line endings: Unix (\n)
  • PHP minimum: 8.2 (CI tests 8.2, 8.3, 8.4, 8.5, 8.6)
  • PSR-12 enforced via php-cs-fixer
  • Use ? for single nullable types and in doc blocks

Documentation Style (enforced)

Docblocks are technical documentation written with direct technical statements. Language and National Variety: English - American Qualities of the writing: clear, thorough, easy to comprehend, not verbose (brevity), timeless/always was, integrated, wholistic Tense: Present - tune for ease of comprehension Banned constructions:

  • Antithesis / "not merely X — it Ys": no "does not just X, it Ys", "is not a Y, it's a Z", "rather than X, it Ys". State what it does, once.
  • Em-dash dramatic asides used for emphasis or reveal ("— and that's the point", "— never stronger"). Use a period or plain clause.
  • Editorializing / filler is unnecessary.
  • Rule-of-three rhetorical lists and build-up sentences. One fact per sentence.

Prefer: subject–verb–object declaratives, tables and bullet lists of condition → result where appropriate. Docblocks inform and describe; it is not persuasive writing.

Working Knowledge (agents/)

Working Knowledge (agents/)

framework/ directory hierarchy maps to agents/framework/ for documentation and knowledge files. The typical CLAUDE.md file is named "INDEX.md". Classes have class knowledge files named ".md" in the same relative directory. Brief summaries of directories are named "SUMMARY.md". Scan documentation with find agents/ -type f -name '*.md' for relevant context. There may be other useful ".md" files.

Anti-Patterns (Required Safeguards)

  • Never run rm on any path without developer approval first; except to clean up prior work.
  • Never remove composer --dev dependencies.
  • Never erase or overwrite files during unit testing — the file changes being tested must be preserved.