NetsonCloud
Netson\Platform\NetsonCloud · All classes
Entry point for apps on the hosting platform. Every method fails open: trouble with the platform turns features off, it never throws.
Get one per app: in Laravel through the container (app(NetsonCloud::class), bound
by PlatformServiceProvider), in plain PHP with forPath(). Then ask it for a feature
(security(), cache()); each one is always an object, never null, and reports
itself unavailable where it cannot work, so your code has no special cases for a
laptop, CI or a server without the feature.
Example
/**
* The platform for this app, or null where the library is not installed (your laptop,
* CI, another host). Call it once, early, e.g. from your front controller:
* connectToPlatform('/usr/local/share/netson-platform-php/v1/autoload.php', __DIR__).
*/
function connectToPlatform(string $library, string $appDir): ?NetsonCloud
{
if (!is_file($library)) {
return null; // not a platform server: the app runs exactly as before
}
require_once $library;
// Never throws. Where the platform is not usable, state() says why and every
// feature reports itself unavailable.
return NetsonCloud::forPath($appDir);
}
Constants
STATE_READY='ready'— The platform is usable here; features follow the site's settings.STATE_ABSENT='absent'— No platform settings found above the app: not a platform site (or not yet set up).STATE_UNREACHABLE='unreachable'— The platform settings exist but this app's PHP may not read them (open_basedir).STATE_NOT_PLATFORM_INSTALL='not_platform_install'— The library was loaded from somewhere other than the platform's own install.STATE_BAD_CONFIG='bad_config'— The platform settings could not be read or are invalid.STATE_FOREIGN_CONFIG='foreign_config'— The platform settings belong to another site (for example a copied site).
Methods
forPath()
public static function forPath(string $appPath): self
The platform as seen from this app: finds the site's platform/config.json above the given path.
Laravel apps do not call this: PlatformServiceProvider does, with base_path().
$appPath(string): a directory of your app, usually its root (__DIR__in a file there); the site's platform settings are looked up from there upwards
Returns self: always an object, never an exception; state() says whether the platform is usable here
See the example at the top of this page.
forTesting()
public static function forTesting(string $root): self
Read <root>/platform/config.json directly, skipping the install check and the directory walk. Not supported in production.
For your own tests: pass a directory without platform settings and every
feature is off (state absent), so code that uses the platform runs without it
and writes nothing. Bind the result in the container in place of the real one.
$root(string): a directory that holds noplatform/config.json, e.g.sys_get_temp_dir() . '/no-platform'; it need not exist
Returns self: an instance that reads only that directory
Example
/**
* In a test's setUp(): replace the platform with one that has every feature off, so
* the code under test runs without it and writes nothing. Any directory without
* platform settings will do; it does not even have to exist.
*/
function withoutPlatform(Application $app): void
{
$app->instance(NetsonCloud::class, NetsonCloud::forTesting(sys_get_temp_dir() . '/no-platform'));
}
supports()
public function supports(string $feature): bool
Whether a platform feature (e.g. 'security.feed', 'cache') is available for this site.
$feature(string):'security.feed'(report security events) or'cache'(the page cache); any other name is answered with false
Returns bool: true when the platform is ready here and the feature is switched on for this site
Example
$offers = [];
foreach (['security.feed', 'cache'] as $feature) {
$offers[$feature] = $cloud->supports($feature);
}
$state = $cloud->state(); // "ready", or why the platform is not usable here
$why = $cloud->stateReason();
security()
public function security(string $site = ''): Security\Feed
The security feed for this site. Never throws; when the cloud is not ready or
security.feed is off, the returned feed reports unavailable and writes nothing.
$site(string): leave empty; only a WordPress multisite network passes the name of one of its sites here
Returns Security\Feed: always an object; its record methods return unavailable when the feed is off
Example
/**
* Call this wherever a reset link is requested. Safe without the library, without a
* request address (console, queue) and when the platform is in trouble.
*/
function recordResetRequested(Request $request): void
{
$ip = $request->server('REMOTE_ADDR');
if (!class_exists(NetsonCloud::class) || !is_string($ip) || $ip === '') {
return; // no library, or no TCP peer: nothing to record
}
// REMOTE_ADDR, never $request->ip(): behind a proxy ip() follows X-Forwarded-For,
// which the client controls. Never record the email address, not even hashed.
app(NetsonCloud::class)->security()->record('auth.reset_requested', $ip);
}
cache()
public function cache(): Cache\Site
This site's page cache: ask for pages to be cleared after a change. Never
throws; when the cloud is not ready or cache is off, the returned site
reports unavailable and does nothing.
Returns Cache\Site: always an object; its available() says whether this site has a page cache
Example
/**
* Clear the cached news overview and the item that changed.
*/
function purgeNewsPages(string $base, string $slug): bool
{
if (!class_exists(NetsonCloud::class)) {
return false; // no library on this machine: nothing to purge
}
$cache = app(NetsonCloud::class)->cache();
if (!$cache->available()) {
return false; // this site has no page cache
}
$changes = new Intents();
$changes->addUrls(["{$base}/news/{$slug}", "{$base}/news"], 'news:published');
return $cache->flush($changes, '')->isOk();
}
state()
public function state(): string
One of ready, absent, unreachable, not_platform_install, bad_config, foreign_config.
Most apps never need it: ask supports() or a feature's available() instead. It is for a status page or a log line that explains why nothing happens.
Returns string: one of the STATE_* constants; only ready means usable. Compare with the constants, e.g. NetsonCloud::STATE_READY
Example
$offers = [];
foreach (['security.feed', 'cache'] as $feature) {
$offers[$feature] = $cloud->supports($feature);
}
$state = $cloud->state(); // "ready", or why the platform is not usable here
$why = $cloud->stateReason();
stateReason()
public function stateReason(): string
Why the platform is not ready, in one sentence; empty when it is ready.
Returns string: for people reading your log or status page, e.g. that no platform settings were found above the app; the wording may change, so never compare it in code
Example
$offers = [];
foreach (['security.feed', 'cache'] as $feature) {
$offers[$feature] = $cloud->supports($feature);
}
$state = $cloud->state(); // "ready", or why the platform is not usable here
$why = $cloud->stateReason();