Skip to content

Commit

Permalink
append code / refactor (#37)
Browse files Browse the repository at this point in the history
* append code / refactor

* cs

* for review

* for php5.5

* review

* append annotation register

* for 5.5

* for testing

* create new instance
  • Loading branch information
ytake committed Dec 3, 2016
1 parent a2c36c6 commit 3340942
Show file tree
Hide file tree
Showing 44 changed files with 1,090 additions and 130 deletions.
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ $ php artisan ytake:aspect-clear-cache
### @Transactional
for database transaction(illuminate/database)

you must use the TransactionalModule

* option

| params | description |
Expand All @@ -159,6 +161,8 @@ public function save(array $params)
### @Cacheable
for cache(illuminate/cache)

you must use the CacheableModule

* option

| params | description |
Expand Down Expand Up @@ -187,6 +191,8 @@ public function namedMultipleKey($id, $value)
### @CacheEvict
for cache(illuminate/cache) / remove cache

you must use the CacheEvictModule

* option

| params | description |
Expand All @@ -213,6 +219,8 @@ public function removeCache()
### @CachePut
for cache(illuminate/cache) / cache put

you must use the CachePutModule

* option

| params | description |
Expand All @@ -239,6 +247,8 @@ public function throwExceptionCache()

for logger(illuminate/log, monolog)

you must use the LoggableModule / LogExceptionsModule

* option

| params | description |
Expand Down Expand Up @@ -293,6 +303,87 @@ class AspectLoggable

```

### @PostConstruct
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.

you must use the PostConstructModule

```php
use Ytake\LaravelAspect\Annotation\PostConstruct;

class Something
{
protected $abstract;

protected $counter = 0;

public function __construct(ExampleInterface $abstract)
{
$this->abstract = $abstract;
}

/**
* @PostConstruct
*/
public function init()
{
$this->counter += 1;
}

/**
* @return int
*/
public function returning()
{
return $this->counter;
}
}

```

**The method MUST NOT have any parameters**

### @RetryOnFailure

Retry the method in case of exception.

you must use the RetryOnFailureModule.

* option

| params | description |
|-----|-------|
| attempts (int) | How many times to retry. (default: 0) |
| delay (int) | Delay between attempts. (default: 0 / sleep(0) ) |
| types (array) | When to retry (in case of what exception types). (default: <\Exception::class> ) |
| ignore (string) | Exception types to ignore. (default: \Exception ) |

```php
use Ytake\LaravelAspect\Annotation\RetryOnFailure;

class ExampleRetryOnFailure
{
/** @var int */
public $counter = 0;

/**
* @RetryOnFailure(
* types={
* LogicException::class,
* },
* attempts=3,
* ignore=Exception::class
* )
*/
public function ignoreException()
{
$this->counter += 1;
throw new \Exception;
}
}

```

### @Async
Methods annotated with @Async will return immediately to its caller while its operation executes asynchronously.

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
"files": [
"tests/AspectTestCase.php",
"tests/helper.php",
"tests/MockApplication.php"
"tests/MockApplication.php",
"tests/ResolveMockInterface.php",
"tests/ResolveMockClass.php"
],
"psr-4": {
"__Test\\": "tests/src"
Expand Down
13 changes: 0 additions & 13 deletions src/Annotation/AnnotationReaderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,14 @@
*/
namespace Ytake\LaravelAspect\Annotation;

use Doctrine\Common\Annotations\Reader;

/**
* Class AnnotationReaderTrait
*/
trait AnnotationReaderTrait
{
/** @var Reader */
protected $reader;

/** @var string */
protected $annotation;

/**
* @param Reader $reader
*/
public function setReader(Reader $reader)
{
$this->reader = $reader;
}

/**
* @param string $annotation
*/
Expand Down
29 changes: 29 additions & 0 deletions src/Annotation/PostConstruct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*
* Copyright (c) 2015-2016 Yuuki Takezawa
*
*/
namespace Ytake\LaravelAspect\Annotation;

use Doctrine\Common\Annotations\Annotation;

/**
* Class PostConstruct
* @Annotation
* @Target("METHOD")
*/
final class PostConstruct extends Annotation
{
}
27 changes: 27 additions & 0 deletions src/Annotation/RequireAnnotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*
* Copyright (c) 2015-2016 Yuuki Takezawa
*
*/

require_once 'Async.php';
require_once 'Cacheable.php';
require_once 'CacheEvict.php';
require_once 'CachePut.php';
require_once 'LogExceptions.php';
require_once 'Loggable.php';
require_once 'PostConstruct.php';
require_once 'RetryOnFailure.php';
require_once 'Transactional.php';
43 changes: 43 additions & 0 deletions src/Annotation/RetryOnFailure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*
* Copyright (c) 2015-2016 Yuuki Takezawa
*
*/
namespace Ytake\LaravelAspect\Annotation;

use Doctrine\Common\Annotations\Annotation;

/**
* Class RetryOnFailure
*
* @Annotation
* @Target("METHOD")
*/
final class RetryOnFailure extends Annotation
{
/** @var int How many times to retry. */
public $attempts = 0;

/** @var int Delay between attempts. */
public $delay = 0;

/** @var string[] When to retry (in case of what exception types). */
public $types = [
\Exception::class,
];

/** @var string Exception types to ignore. */
public $ignore = \Exception::class;
}
8 changes: 8 additions & 0 deletions src/AnnotationConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Ytake\LaravelAspect;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;

/**
* Class AnnotationConfiguration
Expand All @@ -27,6 +28,7 @@ class AnnotationConfiguration
/** @var array */
protected $configuration;


/**
* AnnotationConfiguration constructor.
*
Expand All @@ -35,6 +37,7 @@ class AnnotationConfiguration
public function __construct(array $configuration)
{
$this->configuration = $configuration;
$this->registerAnnotations();
}

/**
Expand All @@ -51,4 +54,9 @@ public function ignoredAnnotations()
}
}
}

protected function registerAnnotations()
{
AnnotationRegistry::registerFile(__DIR__ . '/Annotation/RequireAnnotation.php');
}
}
2 changes: 2 additions & 0 deletions src/Console/ModulePublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class ModulePublishCommand extends Command
'TransactionalModule' => 'Ytake\LaravelAspect\Modules\TransactionalModule',
'LoggableModule' => 'Ytake\LaravelAspect\Modules\LoggableModule',
'LogExceptionsModule' => 'Ytake\LaravelAspect\Modules\LogExceptionsModule',
'PostConstructModule' => 'Ytake\LaravelAspect\Modules\PostConstructModule',
'RetryOnFailureModule' => 'Ytake\LaravelAspect\Modules\RetryOnFailureModule',
];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
namespace Ytake\LaravelAspect;

use Illuminate\Support\ServiceProvider;
use Ytake\LaravelAspect\Console\ClearCacheCommand;
use Ytake\LaravelAspect\Console\CompileCommand;
use Ytake\LaravelAspect\Console\ClearCacheCommand;
use Ytake\LaravelAspect\Console\ModulePublishCommand;

/**
Expand Down

0 comments on commit 3340942

Please sign in to comment.