Skip to content

Commit

Permalink
Separated factory functions for js calls and ajax requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
feuzeu committed May 17, 2024
1 parent fe38d46 commit 40571b6
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 173 deletions.
15 changes: 11 additions & 4 deletions HOWTO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Jaxon 3.x :: Quick start
------------------------

**Import the functions**

```php
use Jaxon\jaxon;
use Jaxon\rq;
```

**Register a single class**

```php
Expand Down Expand Up @@ -49,18 +56,18 @@ jaxon()->register(Jaxon::CALLABLE_FUNCTION, 'sayHello', [
**Call a registered class**

```php
<button onclick="<?php echo rq('HelloWorld')->call('sayHello') ?>" >Click Me</button>
<button onclick="<?php echo rq('HelloWorld')->sayHello() ?>" >Click Me</button>
```

**Call a registered class with a parameter**

```php
<button onclick="<?php echo rq('HelloWorld')->call('sayHello', 0) ?>" >Click Me</button>
<button onclick="<?php echo rq('HelloWorld')->call('setColor', pm()->select('color')) ?>" >Click Me</button>
<button onclick="<?php echo rq('HelloWorld')->sayHello(0) ?>" >Click Me</button>
<button onclick="<?php echo rq('HelloWorld')->setColor(pm()->select('color')) ?>" >Click Me</button>
```

**Call a registered function**

```php
<button onclick="<?php echo rq()->call('sayHello') ?>" >Click Me</button>
<button onclick="<?php echo rq()->sayHello() ?>" >Click Me</button>
```
10 changes: 0 additions & 10 deletions src/App/AppInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Jaxon\Plugin\Package;
use Jaxon\Plugin\ResponsePlugin;
use Jaxon\Request\Factory\Factory;
use Jaxon\Request\Factory\RequestFactory;
use Jaxon\Request\Handler\CallbackManager;
use Jaxon\Response\ResponseInterface;
use Jaxon\Utils\Http\UriException;
Expand Down Expand Up @@ -84,15 +83,6 @@ public function getContentType(): string;
*/
public function factory(): Factory;

/**
* Get a request to a registered class
*
* @param string $sClassName The class name
*
* @return RequestFactory|null
*/
public function request(string $sClassName = ''): ?RequestFactory;

/**
* Get the HTML tags to include Jaxon javascript files into the page.
*
Expand Down
8 changes: 4 additions & 4 deletions src/App/CallableClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Jaxon\Plugin\Request\CallableClass\CallableClassHelper;
use Jaxon\Plugin\Response\DataBag\DataBagContext;
use Jaxon\Plugin\Response\Pagination\Paginator;
use Jaxon\Request\Factory\RequestFactory;
use Jaxon\Request\Factory\JsCallFactory;
use Jaxon\Request\Js\Selector;
use Jaxon\Request\TargetInterface;
use Jaxon\Response\Response;
Expand Down Expand Up @@ -50,13 +50,13 @@ public function cl(string $sClassName)
}

/**
* Get the request factory.
* Get the js call factory.
*
* @param string $sClassName
*
* @return RequestFactory
* @return JsCallFactory
*/
public function rq(string $sClassName = ''): RequestFactory
public function rq(string $sClassName = ''): JsCallFactory
{
return $this->xCallableClassHelper->rq($sClassName);
}
Expand Down
28 changes: 0 additions & 28 deletions src/App/Traits/AjaxTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Jaxon\Plugin\Request\CallableClass\CallableRegistry;
use Jaxon\Plugin\ResponsePlugin;
use Jaxon\Request\Factory\Factory;
use Jaxon\Request\Factory\RequestFactory;
use Jaxon\Request\Handler\CallbackManager;
use Jaxon\Response\Manager\ResponseManager;
use Jaxon\Utils\Http\UriException;
Expand Down Expand Up @@ -170,33 +169,6 @@ public function cl(string $sClassName)
return !$xCallableClass ? null : $xCallableClass->getRegisteredObject();
}

/**
* Get the request factory to a registered class
*
* @param string $sClassName The class name
*
* @return RequestFactory|null
* @throws SetupException
*/
public function rq(string $sClassName = ''): ?RequestFactory
{
$sClassName = trim($sClassName);
return $this->factory()->request($sClassName);
}

/**
* Get the request factory to a registered class
*
* @param string $sClassName The class name
*
* @return RequestFactory|null
* @throws SetupException
*/
public function request(string $sClassName = ''): ?RequestFactory
{
return $this->rq($sClassName);
}

/**
* Get the HTML tags to include Jaxon javascript files into the page.
*
Expand Down
10 changes: 5 additions & 5 deletions src/Di/Traits/PsrTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ private function registerPsr()
return new Psr17Factory();
});
$this->set(ServerRequestCreator::class, function($di) {
$xRequestFactory = $di->g(Psr17Factory::class);
$xPsr17Factory = $di->g(Psr17Factory::class);
return new ServerRequestCreator(
$xRequestFactory, // ServerRequestFactory
$xRequestFactory, // UriFactory
$xRequestFactory, // UploadedFileFactory
$xRequestFactory // StreamFactory
$xPsr17Factory, // ServerRequestFactory
$xPsr17Factory, // UriFactory
$xPsr17Factory, // UploadedFileFactory
$xPsr17Factory, // StreamFactory
);
});
$this->set(ServerRequestInterface::class, function($di) {
Expand Down
18 changes: 9 additions & 9 deletions src/Di/Traits/RegisterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Jaxon\Plugin\Request\CallableClass\CallableObject;
use Jaxon\Plugin\Request\CallableClass\CallableRepository;
use Jaxon\Request\Factory\Factory;
use Jaxon\Request\Factory\RequestFactory;
use Jaxon\Request\Factory\JsCallFactory;
use Jaxon\Request\Handler\CallbackManager;
use Jaxon\Request\Target;
use Jaxon\Utils\Config\Config;
Expand Down Expand Up @@ -85,7 +85,7 @@ public function getRegisteredObject(CallableObject $xCallableObject, Target $xTa
*/
public function registerCallableClass(string $sClassName, array $aOptions)
{
$sRequestFactory = $sClassName . '_RequestFactory';
$sJsCallFactory = $sClassName . '_JsCallFactory';
$sCallableObject = $sClassName . '_CallableObject';
$sReflectionClass = $sClassName . '_ReflectionClass';

Expand Down Expand Up @@ -120,12 +120,12 @@ public function registerCallableClass(string $sClassName, array $aOptions)
$di->g($sReflectionClass), $aOptions, $aProtectedMethods);
});

// Register the request factory
$this->set($sRequestFactory, function($di) use($sCallableObject) {
// Register the js call factory
$this->set($sJsCallFactory, function($di) use($sCallableObject) {
$xConfigManager = $di->g(ConfigManager::class);
$xCallable = $di->g($sCallableObject);
$sJsClass = $xConfigManager->getOption('core.prefix.class') . $xCallable->getJsName() . '.';
return new RequestFactory($sJsClass, $di->g(DialogManager::class));
return new JsCallFactory($sJsClass, $di->g(DialogManager::class));
});

// Register the user class, but only if the user didn't already.
Expand Down Expand Up @@ -176,15 +176,15 @@ public function getCallableObject(string $sClassName): CallableObject
}

/**
* Get the request factory for a given class
* Get the js call factory for a given class
*
* @param string $sClassName
*
* @return RequestFactory
* @return JsCallFactory
*/
public function getRequestFactory(string $sClassName): RequestFactory
public function getJsCallFactory(string $sClassName): JsCallFactory
{
return $this->g($sClassName . '_RequestFactory');
return $this->g($sClassName . '_JsCallFactory');
}

/**
Expand Down
13 changes: 4 additions & 9 deletions src/Di/Traits/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Jaxon\Plugin\Response\DataBag\DataBagPlugin;
use Jaxon\Request\Factory\Factory;
use Jaxon\Request\Factory\ParameterFactory;
use Jaxon\Request\Factory\RequestFactory;
use Jaxon\Request\Handler\CallbackManager;
use Jaxon\Request\Handler\ParameterReader;
use Jaxon\Request\Handler\RequestHandler;
Expand Down Expand Up @@ -46,15 +45,11 @@ private function registerRequests()
return new RequestHandler($di->g(Container::class), $di->g(PluginManager::class),
$di->g(ResponseManager::class), $di->g(CallbackManager::class), $di->g(DataBagPlugin::class));
});
// Request Factory
// Requests and calls Factory
$this->set(Factory::class, function($di) {
return new Factory($di->g(CallableRegistry::class), $di->g(RequestFactory::class),
$di->g(ParameterFactory::class));
});
// Factory for requests to functions
$this->set(RequestFactory::class, function($di) {
$sPrefix = $di->g(ConfigManager::class)->getOption('core.prefix.function');
return new RequestFactory($sPrefix, $di->g(DialogManager::class));
$xConfigManager = $di->g(ConfigManager::class);
return new Factory($di->g(CallableRegistry::class), $di->g(DialogManager::class),
$di->g(ParameterFactory::class), $xConfigManager->getOption('core.prefix.function'));
});
// Parameter Factory
$this->set(ParameterFactory::class, function() {
Expand Down
4 changes: 2 additions & 2 deletions src/Plugin/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class Package implements CodeGeneratorInterface
protected $xPkgConfig;

/**
* The request factory
* The factory
*
* @var Factory
*/
Expand Down Expand Up @@ -100,7 +100,7 @@ public function getOption(string $sOption, $xDefault = null)
}

/**
* Get the request factory
* Get the factory
*
* @return Factory
*/
Expand Down
16 changes: 8 additions & 8 deletions src/Plugin/Request/CallableClass/CallableClassHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Jaxon\Di\Container;
use Jaxon\Exception\SetupException;
use Jaxon\Request\Factory\Factory;
use Jaxon\Request\Factory\RequestFactory;
use Jaxon\Request\Factory\JsCallFactory;
use Jaxon\Request\Target;
use Jaxon\Request\Upload\UploadHandlerInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -34,9 +34,9 @@ class CallableClassHelper
public $xFactory;

/**
* @var RequestFactory
* @var JsCallFactory
*/
public $xRequestFactory;
public $xJsCallFactory;

/**
* @var ViewRenderer
Expand Down Expand Up @@ -79,7 +79,7 @@ class CallableClassHelper
public function __construct(Container $di, string $sClassName)
{
$this->xFactory = $di->getFactory();
$this->xRequestFactory = $this->xFactory->request($sClassName);
$this->xJsCallFactory = $this->xFactory->rq($sClassName);
$this->xCallableRegistry = $di->getCallableRegistry();
$this->xViewRenderer = $di->getViewRenderer();
$this->xLogger = $di->getLogger();
Expand All @@ -103,15 +103,15 @@ public function cl(string $sClassName)
}

/**
* Get the request factory.
* Get the js call factory.
*
* @param string $sClassName
*
* @return RequestFactory
* @return JsCallFactory
*/
public function rq(string $sClassName = ''): RequestFactory
public function rq(string $sClassName = ''): JsCallFactory
{
$sClassName = trim($sClassName);
return !$sClassName ? $this->xRequestFactory : $this->xFactory->request($sClassName);
return !$sClassName ? $this->xJsCallFactory : $this->xFactory->rq($sClassName);
}
}
10 changes: 5 additions & 5 deletions src/Plugin/Request/CallableClass/CallableRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Jaxon\App\I18n\Translator;
use Jaxon\Di\Container;
use Jaxon\Exception\SetupException;
use Jaxon\Request\Factory\RequestFactory;
use Jaxon\Request\Factory\JsCallFactory;

use Composer\Autoload\ClassLoader;
use RecursiveDirectoryIterator;
Expand Down Expand Up @@ -279,16 +279,16 @@ public function getCallableObject(string $sClassName): ?CallableObject
}

/**
* Get the request factory for a given class
* Get the js call factory for a given class
*
* @param string $sClassName The class name of the callable object
*
* @return RequestFactory|null
* @return JsCallFactory|null
* @throws SetupException
*/
public function getRequestFactory(string $sClassName): ?RequestFactory
public function getJsCallFactory(string $sClassName): ?JsCallFactory
{
return $this->di->getRequestFactory($this->checkCallableObject($sClassName));
return $this->di->getJsCallFactory($this->checkCallableObject($sClassName));
}

/**
Expand Down
Loading

0 comments on commit 40571b6

Please sign in to comment.