From b09546501f3b7761c58817f616ee444f3030293a Mon Sep 17 00:00:00 2001 From: Patrick Stewart Date: Sat, 15 Jun 2024 17:34:25 -0700 Subject: [PATCH] update: updating files with ported code --- .../lib/src/debug/exception_handler.dart | 49 ++++++ .../lib/src/encryption/decrypt_exception.dart | 12 ++ .../lib/src/encryption/encrypt_exception.dart | 9 + .../lib/src/encryption/encrypter.dart | 34 ++++ .../lib/src/encryption/string_encrypter.dart | 18 ++ .../contracts/lib/src/events/dispatcher.dart | 59 +++++++ .../events/should_dispatch_after_commit.dart | 3 + .../should_handle_events_after_commit.dart | 4 + .../contracts/lib/src/filesystem/cloud.dart | 8 + .../contracts/lib/src/filesystem/factory.dart | 9 + .../filesystem/file_not_found_exception.dart | 9 + .../lib/src/filesystem/filesystem.dart | 164 ++++++++++++++++++ .../filesystem/lock_timeout_exception.dart | 12 ++ .../lib/src/foundation/application.dart | 102 +++++++++++ .../src/foundation/caches_configuration.dart | 17 ++ .../lib/src/foundation/caches_routes.dart | 11 ++ .../src/foundation/exception_renderer.dart | 7 + .../lib/src/foundation/maintenance_mode.dart | 19 ++ .../contracts/lib/src/hashing/hasher.dart | 29 ++++ packages/contracts/lib/src/http/kernel.dart | 26 +++ 20 files changed, 601 insertions(+) diff --git a/packages/contracts/lib/src/debug/exception_handler.dart b/packages/contracts/lib/src/debug/exception_handler.dart index e69de29..63b609b 100644 --- a/packages/contracts/lib/src/debug/exception_handler.dart +++ b/packages/contracts/lib/src/debug/exception_handler.dart @@ -0,0 +1,49 @@ +import 'dart:async'; + +// TODO: Replace custom classes with dart equivalents. + +abstract class ExceptionHandler { + /// Report or log an exception. + /// + /// @param Throwable e + /// @return void + /// + /// @throws Throwable + Future report(Exception e); + + /// Determine if the exception should be reported. + /// + /// @param Throwable e + /// @return bool + bool shouldReport(Exception e); + + /// Render an exception into an HTTP response. + /// + /// @param Request request + /// @param Throwable e + /// @return Response + /// + /// @throws Throwable + Future render(Request request, Exception e); + + /// Render an exception to the console. + /// + /// @param OutputInterface output + /// @param Throwable e + /// @return void + /// + /// @internal This method is not meant to be used or overwritten outside the framework. + void renderForConsole(OutputInterface output, Exception e); +} + +class Request { + // Add your request properties and methods here. +} + +class Response { + // Add your response properties and methods here. +} + +class OutputInterface { + // Add your output interface properties and methods here. +} diff --git a/packages/contracts/lib/src/encryption/decrypt_exception.dart b/packages/contracts/lib/src/encryption/decrypt_exception.dart index e69de29..90e99e5 100644 --- a/packages/contracts/lib/src/encryption/decrypt_exception.dart +++ b/packages/contracts/lib/src/encryption/decrypt_exception.dart @@ -0,0 +1,12 @@ +import 'dart:core'; + +class DecryptException implements Exception { + final String message; + + DecryptException([this.message = '']); + + @override + String toString() { + return message.isEmpty ? 'DecryptException' : 'DecryptException: $message'; + } +} diff --git a/packages/contracts/lib/src/encryption/encrypt_exception.dart b/packages/contracts/lib/src/encryption/encrypt_exception.dart index e69de29..5e95ff9 100644 --- a/packages/contracts/lib/src/encryption/encrypt_exception.dart +++ b/packages/contracts/lib/src/encryption/encrypt_exception.dart @@ -0,0 +1,9 @@ + +class EncryptException implements Exception { + final String message; + + EncryptException([this.message = '']); + + @override + String toString() => 'EncryptException: $message'; +} diff --git a/packages/contracts/lib/src/encryption/encrypter.dart b/packages/contracts/lib/src/encryption/encrypter.dart index e69de29..6a87556 100644 --- a/packages/contracts/lib/src/encryption/encrypter.dart +++ b/packages/contracts/lib/src/encryption/encrypter.dart @@ -0,0 +1,34 @@ +import 'dart:async'; + +abstract class Encrypter { + /// Encrypt the given value. + /// + /// [value]: The value to be encrypted. + /// [serialize]: Whether to serialize the value before encryption. + /// Returns a string representing the encrypted value. + /// Throws an EncryptException if encryption fails. + Future encrypt(dynamic value, {bool serialize = true}); + + /// Decrypt the given value. + /// + /// [payload]: The encrypted payload to be decrypted. + /// [unserialize]: Whether to unserialize the value after decryption. + /// Returns the decrypted value. + /// Throws a DecryptException if decryption fails. + Future decrypt(String payload, {bool unserialize = true}); + + /// Get the encryption key that the encrypter is currently using. + /// + /// Returns the current encryption key as a string. + String getKey(); + + /// Get the current encryption key and all previous encryption keys. + /// + /// Returns a list of all encryption keys. + List getAllKeys(); + + /// Get the previous encryption keys. + /// + /// Returns a list of previous encryption keys. + List getPreviousKeys(); +} diff --git a/packages/contracts/lib/src/encryption/string_encrypter.dart b/packages/contracts/lib/src/encryption/string_encrypter.dart index e69de29..2a90155 100644 --- a/packages/contracts/lib/src/encryption/string_encrypter.dart +++ b/packages/contracts/lib/src/encryption/string_encrypter.dart @@ -0,0 +1,18 @@ + +abstract class StringEncrypter { + /// Encrypt a string without serialization. + /// + /// @param String value + /// @return String + /// + /// @throws EncryptException + String encryptString(String value); + + /// Decrypt the given string without unserialization. + /// + /// @param String payload + /// @return String + /// + /// @throws DecryptException + String decryptString(String payload); +} diff --git a/packages/contracts/lib/src/events/dispatcher.dart b/packages/contracts/lib/src/events/dispatcher.dart index e69de29..548c9b3 100644 --- a/packages/contracts/lib/src/events/dispatcher.dart +++ b/packages/contracts/lib/src/events/dispatcher.dart @@ -0,0 +1,59 @@ +abstract class Dispatcher { + /// Register an event listener with the dispatcher. + /// + /// @param Closure|string|array events + /// @param Closure|string|array|null listener + /// @return void + void listen(dynamic events, [dynamic listener]); + + /// Determine if a given event has listeners. + /// + /// @param string eventName + /// @return bool + bool hasListeners(String eventName); + + /// Register an event subscriber with the dispatcher. + /// + /// @param object|string subscriber + /// @return void + void subscribe(dynamic subscriber); + + /// Dispatch an event until the first non-null response is returned. + /// + /// @param string|object event + /// @param mixed payload + /// @return mixed + dynamic until(dynamic event, [dynamic payload]); + + /// Dispatch an event and call the listeners. + /// + /// @param string|object event + /// @param mixed payload + /// @param bool halt + /// @return List|null + List? dispatch(dynamic event, [dynamic payload, bool halt = false]); + + /// Register an event and payload to be fired later. + /// + /// @param string event + /// @param array payload + /// @return void + void push(String event, [List? payload]); + + /// Flush a set of pushed events. + /// + /// @param string event + /// @return void + void flush(String event); + + /// Remove a set of listeners from the dispatcher. + /// + /// @param string event + /// @return void + void forget(String event); + + /// Forget all of the queued listeners. + /// + /// @return void + void forgetPushed(); +} diff --git a/packages/contracts/lib/src/events/should_dispatch_after_commit.dart b/packages/contracts/lib/src/events/should_dispatch_after_commit.dart index e69de29..bb12497 100644 --- a/packages/contracts/lib/src/events/should_dispatch_after_commit.dart +++ b/packages/contracts/lib/src/events/should_dispatch_after_commit.dart @@ -0,0 +1,3 @@ +abstract class ShouldDispatchAfterCommit { + // Interface with no methods +} diff --git a/packages/contracts/lib/src/events/should_handle_events_after_commit.dart b/packages/contracts/lib/src/events/should_handle_events_after_commit.dart index e69de29..cc98d34 100644 --- a/packages/contracts/lib/src/events/should_handle_events_after_commit.dart +++ b/packages/contracts/lib/src/events/should_handle_events_after_commit.dart @@ -0,0 +1,4 @@ + +abstract class ShouldHandleEventsAfterCommit { + // Add any method signatures if needed in the future +} diff --git a/packages/contracts/lib/src/filesystem/cloud.dart b/packages/contracts/lib/src/filesystem/cloud.dart index e69de29..ad61e53 100644 --- a/packages/contracts/lib/src/filesystem/cloud.dart +++ b/packages/contracts/lib/src/filesystem/cloud.dart @@ -0,0 +1,8 @@ +import 'filesystem.dart'; +abstract class Cloud implements Filesystem { + /// Get the URL for the file at the given path. + /// + /// @param String path + /// @return String + String url(String path); +} \ No newline at end of file diff --git a/packages/contracts/lib/src/filesystem/factory.dart b/packages/contracts/lib/src/filesystem/factory.dart index e69de29..a57232e 100644 --- a/packages/contracts/lib/src/filesystem/factory.dart +++ b/packages/contracts/lib/src/filesystem/factory.dart @@ -0,0 +1,9 @@ +import 'filesystem.dart'; + +abstract class Factory { + /// Get a filesystem implementation. + /// + /// @param String? name + /// @return Filesystem + Filesystem disk([String? name]); +} diff --git a/packages/contracts/lib/src/filesystem/file_not_found_exception.dart b/packages/contracts/lib/src/filesystem/file_not_found_exception.dart index e69de29..f47a60e 100644 --- a/packages/contracts/lib/src/filesystem/file_not_found_exception.dart +++ b/packages/contracts/lib/src/filesystem/file_not_found_exception.dart @@ -0,0 +1,9 @@ + +class FileNotFoundException implements Exception { + final String message; + + FileNotFoundException([this.message = 'File not found']); + + @override + String toString() => 'FileNotFoundException: $message'; +} diff --git a/packages/contracts/lib/src/filesystem/filesystem.dart b/packages/contracts/lib/src/filesystem/filesystem.dart index e69de29..3efacb8 100644 --- a/packages/contracts/lib/src/filesystem/filesystem.dart +++ b/packages/contracts/lib/src/filesystem/filesystem.dart @@ -0,0 +1,164 @@ +import 'dart:typed_data'; +import 'dart:io'; + +abstract class Filesystem { + /// The public visibility setting. + static const String VISIBILITY_PUBLIC = 'public'; + + /// The private visibility setting. + static const String VISIBILITY_PRIVATE = 'private'; + + /// Get the full path to the file that exists at the given relative path. + /// + /// @param String path + /// @return String + String path(String path); + + /// Determine if a file exists. + /// + /// @param String path + /// @return bool + bool exists(String path); + + /// Get the contents of a file. + /// + /// @param String path + /// @return Uint8List? (null if file doesn't exist) + Uint8List? get(String path); + + /// Get a resource to read the file. + /// + /// @param String path + /// @return Stream>? (null if file doesn't exist) + Stream>? readStream(String path); + + /// Write the contents of a file. + /// + /// @param String path + /// @param dynamic contents + /// @param Map? options + /// @return bool + bool put(String path, dynamic contents, [Map? options]); + + /// Store the uploaded file on the disk. + /// + /// @param String path + /// @param dynamic file + /// @param Map? options + /// @return String|false + dynamic putFile(String path, dynamic file, [Map? options]); + + /// Store the uploaded file on the disk with a given name. + /// + /// @param String path + /// @param dynamic file + /// @param String? name + /// @param Map? options + /// @return String|false + dynamic putFileAs(String path, dynamic file, [String? name, Map? options]); + + /// Write a new file using a stream. + /// + /// @param String path + /// @param Stream> resource + /// @param Map? options + /// @return bool + bool writeStream(String path, Stream> resource, [Map? options]); + + /// Get the visibility for the given path. + /// + /// @param String path + /// @return String + String getVisibility(String path); + + /// Set the visibility for the given path. + /// + /// @param String path + /// @param String visibility + /// @return bool + bool setVisibility(String path, String visibility); + + /// Prepend to a file. + /// + /// @param String path + /// @param String data + /// @return bool + bool prepend(String path, String data); + + /// Append to a file. + /// + /// @param String path + /// @param String data + /// @return bool + bool append(String path, String data); + + /// Delete the file at a given path. + /// + /// @param dynamic paths + /// @return bool + bool delete(dynamic paths); + + /// Copy a file to a new location. + /// + /// @param String from + /// @param String to + /// @return bool + bool copy(String from, String to); + + /// Move a file to a new location. + /// + /// @param String from + /// @param String to + /// @return bool + bool move(String from, String to); + + /// Get the file size of a given file. + /// + /// @param String path + /// @return int + int size(String path); + + /// Get the file's last modification time. + /// + /// @param String path + /// @return DateTime + DateTime lastModified(String path); + + /// Get an array of all files in a directory. + /// + /// @param String? directory + /// @param bool recursive + /// @return List + List files([String? directory, bool recursive = false]); + + /// Get all of the files from the given directory (recursive). + /// + /// @param String? directory + /// @return List + List allFiles([String? directory]); + + /// Get all of the directories within a given directory. + /// + /// @param String? directory + /// @param bool recursive + /// @return List + List directories([String? directory, bool recursive = false]); + + /// Get all (recursive) of the directories within a given directory. + /// + /// @param String? directory + /// @return List + List allDirectories([String? directory]); + + /// Create a directory. + /// + /// @param String path + /// @return bool + bool makeDirectory(String path); + + /// Recursively delete a directory. + /// + /// @param String directory + /// @return bool + bool deleteDirectory(String directory); +} diff --git a/packages/contracts/lib/src/filesystem/lock_timeout_exception.dart b/packages/contracts/lib/src/filesystem/lock_timeout_exception.dart index e69de29..66f80bd 100644 --- a/packages/contracts/lib/src/filesystem/lock_timeout_exception.dart +++ b/packages/contracts/lib/src/filesystem/lock_timeout_exception.dart @@ -0,0 +1,12 @@ +import 'dart:io'; + +class LockTimeoutException implements IOException { + final String message; + + LockTimeoutException([this.message = '']); + + @override + String toString() { + return 'LockTimeoutException: $message'; + } +} diff --git a/packages/contracts/lib/src/foundation/application.dart b/packages/contracts/lib/src/foundation/application.dart index e69de29..791090c 100644 --- a/packages/contracts/lib/src/foundation/application.dart +++ b/packages/contracts/lib/src/foundation/application.dart @@ -0,0 +1,102 @@ +import 'container.dart'; // Assuming Container is defined in container.dart +import 'maintenance_mode.dart'; // Assuming MaintenanceMode is defined in maintenance_mode.dart + +// TODO: Replace missing imports with dar equivalents. + +abstract class Application extends Container { + /// Get the version number of the application. + String version(); + + /// Get the base path of the installation. + String basePath([String path = '']); + + /// Get the path to the bootstrap directory. + String bootstrapPath([String path = '']); + + /// Get the path to the application configuration files. + String configPath([String path = '']); + + /// Get the path to the database directory. + String databasePath([String path = '']); + + /// Get the path to the language files. + String langPath([String path = '']); + + /// Get the path to the public directory. + String publicPath([String path = '']); + + /// Get the path to the resources directory. + String resourcePath([String path = '']); + + /// Get the path to the storage directory. + String storagePath([String path = '']); + + /// Get or check the current application environment. + dynamic environment(List environments); + + /// Determine if the application is running in the console. + bool runningInConsole(); + + /// Determine if the application is running unit tests. + bool runningUnitTests(); + + /// Determine if the application is running with debug mode enabled. + bool hasDebugModeEnabled(); + + /// Get an instance of the maintenance mode manager implementation. + MaintenanceMode maintenanceMode(); + + /// Determine if the application is currently down for maintenance. + bool isDownForMaintenance(); + + /// Register all of the configured providers. + void registerConfiguredProviders(); + + /// Register a service provider with the application. + ServiceProvider register(dynamic provider, [bool force = false]); + + /// Register a deferred provider and service. + void registerDeferredProvider(String provider, [String? service]); + + /// Resolve a service provider instance from the class name. + ServiceProvider resolveProvider(String provider); + + /// Boot the application's service providers. + void boot(); + + /// Register a new boot listener. + void booting(Function callback); + + /// Register a new "booted" listener. + void booted(Function callback); + + /// Run the given array of bootstrap classes. + void bootstrapWith(List bootstrappers); + + /// Get the current application locale. + String getLocale(); + + /// Get the application namespace. + String getNamespace(); + + /// Get the registered service provider instances if any exist. + List getProviders(dynamic provider); + + /// Determine if the application has been bootstrapped before. + bool hasBeenBootstrapped(); + + /// Load and boot all of the remaining deferred providers. + void loadDeferredProviders(); + + /// Set the current application locale. + void setLocale(String locale); + + /// Determine if middleware has been disabled for the application. + bool shouldSkipMiddleware(); + + /// Register a terminating callback with the application. + Application terminating(dynamic callback); + + /// Terminate the application. + void terminate(); +} diff --git a/packages/contracts/lib/src/foundation/caches_configuration.dart b/packages/contracts/lib/src/foundation/caches_configuration.dart index e69de29..1075dfe 100644 --- a/packages/contracts/lib/src/foundation/caches_configuration.dart +++ b/packages/contracts/lib/src/foundation/caches_configuration.dart @@ -0,0 +1,17 @@ + +abstract class CachesConfiguration { + /// Determine if the application configuration is cached. + /// + /// Returns a boolean indicating if the configuration is cached. + bool configurationIsCached(); + + /// Get the path to the configuration cache file. + /// + /// Returns a string representing the path to the configuration cache file. + String getCachedConfigPath(); + + /// Get the path to the cached services file. + /// + /// Returns a string representing the path to the cached services file. + String getCachedServicesPath(); +} diff --git a/packages/contracts/lib/src/foundation/caches_routes.dart b/packages/contracts/lib/src/foundation/caches_routes.dart index e69de29..b81221a 100644 --- a/packages/contracts/lib/src/foundation/caches_routes.dart +++ b/packages/contracts/lib/src/foundation/caches_routes.dart @@ -0,0 +1,11 @@ +abstract class CachesRoutes { + /// Determine if the application routes are cached. + /// + /// @return bool + bool routesAreCached(); + + /// Get the path to the routes cache file. + /// + /// @return string + String getCachedRoutesPath(); +} diff --git a/packages/contracts/lib/src/foundation/exception_renderer.dart b/packages/contracts/lib/src/foundation/exception_renderer.dart index e69de29..d30817b 100644 --- a/packages/contracts/lib/src/foundation/exception_renderer.dart +++ b/packages/contracts/lib/src/foundation/exception_renderer.dart @@ -0,0 +1,7 @@ +abstract class ExceptionRenderer { + /// Renders the given exception as HTML. + /// + /// @param Exception throwable + /// @return String + String render(Exception throwable); +} diff --git a/packages/contracts/lib/src/foundation/maintenance_mode.dart b/packages/contracts/lib/src/foundation/maintenance_mode.dart index e69de29..00890d5 100644 --- a/packages/contracts/lib/src/foundation/maintenance_mode.dart +++ b/packages/contracts/lib/src/foundation/maintenance_mode.dart @@ -0,0 +1,19 @@ +abstract class MaintenanceMode { + /// Take the application down for maintenance. + /// + /// [payload] - The payload containing maintenance details. + void activate(Map payload); + + /// Take the application out of maintenance. + void deactivate(); + + /// Determine if the application is currently down for maintenance. + /// + /// Returns `true` if the application is in maintenance mode, `false` otherwise. + bool active(); + + /// Get the data map which was provided when the application was placed into maintenance. + /// + /// Returns a map containing maintenance data. + Map data(); +} diff --git a/packages/contracts/lib/src/hashing/hasher.dart b/packages/contracts/lib/src/hashing/hasher.dart index e69de29..66f3dc2 100644 --- a/packages/contracts/lib/src/hashing/hasher.dart +++ b/packages/contracts/lib/src/hashing/hasher.dart @@ -0,0 +1,29 @@ +abstract class Hasher { + /// Get information about the given hashed value. + /// + /// @param String hashedValue + /// @return Map + Map info(String hashedValue); + + /// Hash the given value. + /// + /// @param String value + /// @param Map options + /// @return String + String make(String value, {Map options = const {}}); + + /// Check the given plain value against a hash. + /// + /// @param String value + /// @param String hashedValue + /// @param Map options + /// @return bool + bool check(String value, String hashedValue, {Map options = const {}}); + + /// Check if the given hash has been hashed using the given options. + /// + /// @param String hashedValue + /// @param Map options + /// @return bool + bool needsRehash(String hashedValue, {Map options = const {}}); +} diff --git a/packages/contracts/lib/src/http/kernel.dart b/packages/contracts/lib/src/http/kernel.dart index e69de29..ab92548 100644 --- a/packages/contracts/lib/src/http/kernel.dart +++ b/packages/contracts/lib/src/http/kernel.dart @@ -0,0 +1,26 @@ +import 'package:symfony_http/symfony_http.dart' as symfony; +import 'package:illuminate_foundation/illuminate_foundation.dart'; + +// TODO: Find replacements for missing imports. + +abstract class Kernel { + /// Bootstrap the application for HTTP requests. + void bootstrap(); + + /// Handle an incoming HTTP request. + /// + /// @param symfony.Request request + /// @return symfony.Response + symfony.Response handle(symfony.Request request); + + /// Perform any final actions for the request lifecycle. + /// + /// @param symfony.Request request + /// @param symfony.Response response + void terminate(symfony.Request request, symfony.Response response); + + /// Get the application instance. + /// + /// @return Application + Application getApplication(); +}