Skip to content

Commit

Permalink
Added annotations for protected upload and databag properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
feuzeu committed Apr 14, 2022
1 parent 818b3ea commit 4afa6bd
Show file tree
Hide file tree
Showing 17 changed files with 567 additions and 70 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"pimple/pimple": "^3.0",
"nyholm/psr7": "^1.5",
"nyholm/psr7-server": "^1.0",
"jaxon-php/jaxon-utils": "^1.1.2"
"jaxon-php/jaxon-utils": "^1.1.2",
"mindplay/annotations": "^1.3"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
Expand Down
24 changes: 24 additions & 0 deletions src/Annotations/Annotation/AbstractAnnotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Jaxon\Annotations\Annotation;

use mindplay\annotations\Annotation;

abstract class AbstractAnnotation extends Annotation
{
/**
* Get the annotation name
*
* @return string
*/
abstract public function getName(): string;

/**
* Get the annotation attribute value
*
* @param mixed $xCurrValue The current value of the attribute
*
* @return mixed
*/
abstract public function getValue($xCurrValue);
}
67 changes: 67 additions & 0 deletions src/Annotations/Annotation/DataBagAnnotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* DataBagAnnotation.php
*
* Jaxon annotation for data bags.
*
* @package jaxon-core
* @copyright 2022 Thierry Feuzeu <thierry.feuzeu@gmail.com>
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @link https://github.com/jaxon-php/jaxon-core
*/

namespace Jaxon\Annotations\Annotation;

use mindplay\annotations\AnnotationException;

use function count;
use function is_array;
use function is_string;

/**
* Specifies a data bag stored in the browser and included in ajax requests to a method.
*
* @usage('method'=>true, 'multiple'=>true, 'inherited'=>true)
*/
class DataBagAnnotation extends AbstractAnnotation
{
/**
* @var string
*/
protected $sName = '';

/**
* @inheritDoc
* @throws AnnotationException
*/
public function initAnnotation(array $properties)
{
if(count($properties) != 1 || !isset($properties['name']) || !is_string($properties['name']))
{
throw new AnnotationException('UploadAnnotation requires a name property of type string');
}
$this->sName = $properties['name'];
}

/**
* @inheritDoc
*/
public function getName(): string
{
return 'bags';
}

/**
* @inheritDoc
*/
public function getValue($xCurrValue)
{
if(!is_array($xCurrValue))
{
return [$this->sName];
}
$xCurrValue[] = $this->sName;
return $xCurrValue;
}
}
61 changes: 61 additions & 0 deletions src/Annotations/Annotation/ExcludeAnnotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* AfterAnnotation.php
*
* Jaxon annotation for callbacks.
*
* @package jaxon-core
* @copyright 2022 Thierry Feuzeu <thierry.feuzeu@gmail.com>
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @link https://github.com/jaxon-php/jaxon-core
*/

namespace Jaxon\Annotations\Annotation;

use mindplay\annotations\AnnotationException;

use function count;

/**
* Specifies a method to be called after the one targeted by a Jaxon request.
*
* @usage('method'=>true, 'inherited'=>true)
*/
class ExcludeAnnotation extends AbstractAnnotation
{
/**
* @var bool
*/
protected $bValue;

/**
* @inheritDoc
* @throws AnnotationException
*/
public function initAnnotation(array $properties)
{
if(count($properties) !== 0 &&
(count($properties) !== 1 || !isset($properties[0]) || !is_bool($properties[0])))
{
throw new AnnotationException('the @exclude annotation does not have any property');
}
$this->bValue = $properties[0] ?? true;
}

/**
* @inheritDoc
*/
public function getName(): string
{
return 'protected';
}

/**
* @inheritDoc
*/
public function getValue($xCurrValue)
{
return $this->bValue;
}
}
59 changes: 59 additions & 0 deletions src/Annotations/Annotation/UploadAnnotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* UploadAnnotation.php
*
* Jaxon annotation for file upload.
*
* @package jaxon-core
* @copyright 2022 Thierry Feuzeu <thierry.feuzeu@gmail.com>
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @link https://github.com/jaxon-php/jaxon-core
*/

namespace Jaxon\Annotations\Annotation;

use mindplay\annotations\Annotation;
use mindplay\annotations\AnnotationException;

/**
* Specifies an upload form field id.
*
* @usage('method'=>true, 'inherited'=>true)
*/
class UploadAnnotation extends AbstractAnnotation
{
/**
* @var string
*/
protected $sField = '';

/**
* @inheritDoc
* @throws AnnotationException
*/
public function initAnnotation(array $properties)
{
if(count($properties) != 1 || !isset($properties['field']) || !is_string($properties['field']))
{
throw new AnnotationException('UploadAnnotation requires a field property of type string');
}
$this->sField = $properties['field'];
}

/**
* @inheritDoc
*/
public function getName(): string
{
return 'upload';
}

/**
* @inheritDoc
*/
public function getValue($xCurrValue)
{
return "'" . $this->sField . "'" ;
}
}
122 changes: 122 additions & 0 deletions src/Annotations/AnnotationReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

/**
* AnnotationReader.php
*
* Jaxon annotation manager.
*
* @package jaxon-core
* @copyright 2022 Thierry Feuzeu <thierry.feuzeu@gmail.com>
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @link https://github.com/jaxon-php/jaxon-core
*/

namespace Jaxon\Annotations;

use Jaxon\Annotations\Annotation\AbstractAnnotation;
// use Jaxon\Annotations\Annotation\AfterAnnotation;
// use Jaxon\Annotations\Annotation\BeforeAnnotation;
use Jaxon\Annotations\Annotation\DataBagAnnotation;
use Jaxon\Annotations\Annotation\ExcludeAnnotation;
use Jaxon\Annotations\Annotation\UploadAnnotation;
use mindplay\annotations\AnnotationException;
use mindplay\annotations\AnnotationManager;

use function array_filter;
use function count;
use function is_a;

class AnnotationReader
{
/**
* @var AnnotationManager
*/
protected $xManager;

/**
* The constructor
*
* @param AnnotationManager $xManager
*/
public function __construct(AnnotationManager $xManager)
{
$this->xManager = $xManager;
$this->xManager->registry['upload'] = UploadAnnotation::class;
$this->xManager->registry['databag'] = DataBagAnnotation::class;
$this->xManager->registry['exclude'] = ExcludeAnnotation::class;
// $this->xManager->registry['before'] = BeforeAnnotation::class;;
// $this->xManager->registry['after'] = AfterAnnotation::class;;
// Missing standard annotations.
// We need to define this, otherwise they throw an exception, and make the whole processing fail.
$this->xManager->registry['inheritDoc'] = false;
}

/**
* @param string $sClass
* @param string $sMethod
*
* @return AbstractAnnotation[]
*/
private function getMethodAnnotations(string $sClass, string $sMethod): array
{
try
{
$aAnnotations = $this->xManager->getMethodAnnotations($sClass, $sMethod);
return array_filter($aAnnotations, function($xAnnotation) {
return is_a($xAnnotation, AbstractAnnotation::class);
});
}
catch(AnnotationException $e) {}
return [];
}

/**
* @param string $sClass
* @param string $sMethod
*
* @return array
*/
private function getMethodAttributes(string $sClass, string $sMethod): array
{
$aAnnotations = $this->getMethodAnnotations($sClass, $sMethod);
$aAttributes = [];
foreach($aAnnotations as $xAnnotation)
{
$sName = $xAnnotation->getName();
$xValue = $xAnnotation->getValue($aAttributes[$sName] ?? null);
if($sName === 'protected' && !$xValue)
{
continue; // Ignore annotation exclude with value false
}
$aAttributes[$sName] = $xValue;
}
return $aAttributes;
}

/**
* Get the class attributes from its methods annotations
*
* @param string $sClass
* @param array $aMethods
*
* @return array
*/
public function getClassAttributes(string $sClass, array $aMethods): array
{
$aProtected = [];
$aAttributes = [];
foreach($aMethods as $sMethod)
{
$aMethodAttrs = $this->getMethodAttributes($sClass, $sMethod);
if(isset($aMethodAttrs['protected']))
{
$aProtected[] = $sMethod;
}
elseif(count($aMethodAttrs) > 0)
{
$aAttributes[$sMethod] = $aMethodAttrs;
}
}
return [$aAttributes, $aProtected];
}
}
5 changes: 5 additions & 0 deletions src/App/Dialog/Library/DialogLibraryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ public function onChanges(Config $xConfig)
public function onChange(Config $xConfig, string $sName)
{
// Reset the default libraries any time the config is changed.
if($sName === 'dialogs.libraries')
{
$this->registerLibraries();
return;
}
if(substr($sName, 0, 15) === 'dialogs.default')
{
$this->setDefaultLibraries();
Expand Down
1 change: 1 addition & 0 deletions src/Di/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private function registerAll()
$this->registerCallables();
$this->registerViews();
$this->registerUtils();
$this->registerAnnotations();
}

/**
Expand Down
Loading

0 comments on commit 4afa6bd

Please sign in to comment.