-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added annotations for protected upload and databag properties.
- Loading branch information
Showing
17 changed files
with
567 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 . "'" ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.