Skip to content

Commit 9ea90c9

Browse files
authored
Merge pull request #25 from dereuromark/master
Add bake task for services.
2 parents 160eec3 + 873bc0a commit 9ea90c9

24 files changed

+222
-30
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
],
1616
"require": {
17-
"php": ">=7.0",
17+
"php": ">=7.2",
1818
"cakephp/cakephp": "^4.0"
1919
},
2020
"require-dev": {

docs/Bake.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Bake
2+
3+
## Bake your Service
4+
5+
Once the plugin is loaded, you can bake your service using:
6+
```
7+
bin/cake bake service MyFooBar
8+
```
9+
This would create a service class `src/Service/MyFooBarService.php`.
10+
11+
You can also bake inside sub namespaces (subfolders):
12+
```
13+
bin/cake bake service My/Foo/Bar
14+
```
15+
This would create a service class `src/Service/My/Foo/BarService.php`.

src/Annotator/ClassAnnotatorTask/ServiceAwareClassAnnotatorTask.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
declare(strict_types = 1);
2+
declare(strict_types=1);
33

44
namespace Burzum\CakeServiceLayer\Annotator\ClassAnnotatorTask;
55

src/Command/BakeServiceCommand.php

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7+
*
8+
* Licensed under The MIT License
9+
* For full copyright and license information, please see the LICENSE.txt
10+
* Redistributions of files must retain the above copyright notice.
11+
*
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13+
* @link http://cakephp.org CakePHP(tm) Project
14+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
15+
*/
16+
namespace Burzum\CakeServiceLayer\Command;
17+
18+
use Bake\Command\SimpleBakeCommand;
19+
use Cake\Console\Arguments;
20+
use Cake\Console\ConsoleIo;
21+
use Cake\Core\Configure;
22+
23+
/**
24+
* Command class for generating migration snapshot files.
25+
*/
26+
class BakeServiceCommand extends SimpleBakeCommand
27+
{
28+
/**
29+
* Task name used in path generation.
30+
*
31+
* @var string
32+
*/
33+
public $pathFragment = 'Service/';
34+
35+
/**
36+
* @var string
37+
*/
38+
protected $_name;
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
public static function defaultName(): string
44+
{
45+
return 'bake service';
46+
}
47+
48+
/**
49+
* @inheritDoc
50+
*/
51+
public function bake(string $name, Arguments $args, ConsoleIo $io): void
52+
{
53+
$this->_name = $name;
54+
55+
parent::bake($name, $args, $io);
56+
}
57+
58+
/**
59+
* @inheritDoc
60+
*/
61+
public function template(): string
62+
{
63+
return 'Burzum/CakeServiceLayer.Service/service';
64+
}
65+
66+
/**
67+
* @inheritDoc
68+
*/
69+
public function templateData(Arguments $arguments): array
70+
{
71+
$name = $this->_name;
72+
$namespace = Configure::read('App.namespace');
73+
$pluginPath = '';
74+
if ($this->plugin) {
75+
$namespace = $this->_pluginNamespace($this->plugin);
76+
$pluginPath = $this->plugin . '.';
77+
}
78+
79+
$namespace .= '\\Service';
80+
81+
$namespacePart = null;
82+
if (strpos($name, '/') !== false) {
83+
$parts = explode('/', $name);
84+
$name = array_pop($parts);
85+
$namespacePart = implode('\\', $parts);
86+
}
87+
if ($namespacePart) {
88+
$namespace .= '\\' . $namespacePart;
89+
}
90+
91+
return [
92+
'plugin' => $this->plugin,
93+
'pluginPath' => $pluginPath,
94+
'namespace' => $namespace,
95+
'name' => $name,
96+
];
97+
}
98+
99+
/**
100+
* @inheritDoc
101+
*/
102+
public function name(): string
103+
{
104+
return 'service';
105+
}
106+
107+
/**
108+
* @inheritDoc
109+
*/
110+
public function fileName(string $name): string
111+
{
112+
return $name . 'Service.php';
113+
}
114+
}

src/DomainModel/DomainModelAwareTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @since 1.0.0
1212
* @license https://opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
declare(strict_types = 1);
14+
declare(strict_types=1);
1515

1616
namespace Burzum\CakeServiceLayer\DomainModel;
1717

src/DomainModel/DomainModelLocator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @since 1.0.0
1212
* @license https://opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
declare(strict_types = 1);
14+
declare(strict_types=1);
1515

1616
namespace Burzum\CakeServiceLayer\DomainModel;
1717

@@ -77,7 +77,7 @@ protected function _throwMissingClassError(string $class, ?string $plugin): void
7777
protected function _create($class, $alias, $config)
7878
{
7979
if (empty($config)) {
80-
return new $class;
80+
return new $class();
8181
}
8282

8383
return new $class(...$config);

src/Generator/Task/ServiceTask.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @since 1.0.0
1111
* @license https://opensource.org/licenses/mit-license.php MIT License
1212
*/
13-
declare(strict_types = 1);
13+
declare(strict_types=1);
1414

1515
namespace Burzum\CakeServiceLayer\Generator\Task;
1616

src/Plugin.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
6+
*
7+
* Licensed under The MIT License
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11+
* @link http://cakephp.org CakePHP(tm) Project
12+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
namespace Burzum\CakeServiceLayer;
15+
16+
use Cake\Console\CommandCollection;
17+
use Cake\Core\BasePlugin;
18+
19+
/**
20+
* Plugin class for Burzum/CakeServiceLayer
21+
*/
22+
class Plugin extends BasePlugin
23+
{
24+
/**
25+
* @var string
26+
*/
27+
protected $name = 'Burzum/CakeServiceLayer';
28+
29+
/**
30+
* @var bool
31+
*/
32+
protected $routesEnabled = false;
33+
34+
/**
35+
* @var bool
36+
*/
37+
protected $middlewareEnabled = false;
38+
39+
/**
40+
* Add migrations commands.
41+
*
42+
* @param \Cake\Console\CommandCollection $collection The command collection to update
43+
* @return \Cake\Console\CommandCollection
44+
*/
45+
public function console(CommandCollection $collection): CommandCollection
46+
{
47+
if (class_exists('Bake\Command\SimpleBakeCommand')) {
48+
$commands = $collection->discoverPlugin($this->getName());
49+
50+
return $collection->addMany($commands);
51+
}
52+
53+
return $collection;
54+
}
55+
}

src/Service/PaginationService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @since 1.0.0
1212
* @license https://opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
declare(strict_types = 1);
14+
declare(strict_types=1);
1515

1616
namespace Burzum\CakeServiceLayer\Service;
1717

src/Service/ServiceAwareTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @since 1.0.0
1212
* @license https://opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
declare(strict_types = 1);
14+
declare(strict_types=1);
1515

1616
namespace Burzum\CakeServiceLayer\Service;
1717

src/Service/ServiceLocator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @since 1.0.0
1212
* @license https://opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
declare(strict_types = 1);
14+
declare(strict_types=1);
1515

1616
namespace Burzum\CakeServiceLayer\Service;
1717

@@ -32,7 +32,7 @@ class ServiceLocator extends ObjectRegistry
3232
* @param string $class The class to resolve.
3333
* @return string|null The resolved name or null for failure.
3434
*/
35-
protected function _resolveClassName(string$class): ?string
35+
protected function _resolveClassName(string $class): ?string
3636
{
3737
return App::className($class, 'Service', 'Service') ?: null;
3838
}

src/Service/ServicePaginatorTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @since 1.0.0
1212
* @license https://opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
declare(strict_types = 1);
14+
declare(strict_types=1);
1515

1616
namespace Burzum\CakeServiceLayer\Service;
1717

templates/bake/Service/service.twig

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace {{ namespace }};
5+
6+
class {{ name }}Service
7+
{
8+
}

tests/TestCase/DomainModel/DomainModelLocatorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @since 1.0.0
1212
* @license https://opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
declare(strict_types = 1);
14+
declare(strict_types=1);
1515

1616
namespace Burzum\CakeServiceLayer\Service;
1717

tests/TestCase/Generator/Task/ServiceTaskTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @since 1.0.0
1212
* @license https://opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
declare(strict_types = 1);
14+
declare(strict_types=1);
1515

1616
namespace Burzum\CakeServiceLayer\Test\TestCase\Generator\Task;
1717

tests/TestCase/Service/PaginationServiceTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @since 1.0.0
1212
* @license https://opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
declare(strict_types = 1);
14+
declare(strict_types=1);
1515

1616
namespace Burzum\CakeServiceLayer\Service;
1717

@@ -31,7 +31,7 @@ class PaginationServiceTest extends TestCase
3131
* @var array
3232
*/
3333
public $fixtures = [
34-
'core.Articles'
34+
'core.Articles',
3535
];
3636

3737
/**
@@ -69,8 +69,8 @@ public function testPaginate()
6969
'completeSort' => [],
7070
'start' => 1,
7171
'end' => 3,
72-
'requestedPage' => 1
73-
]
72+
'requestedPage' => 1,
73+
],
7474
];
7575

7676
$this->assertIsArray($params);

tests/TestCase/Service/ServiceLocatorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @since 1.0.0
1212
* @license https://opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
declare(strict_types = 1);
14+
declare(strict_types=1);
1515

1616
namespace Burzum\CakeServiceLayer\Service;
1717

@@ -84,7 +84,7 @@ public function testPassingClassName()
8484
{
8585
$locator = new ServiceLocator();
8686
$locator->load('Existing', [
87-
'className' => TestService::class
87+
'className' => TestService::class,
8888
]);
8989

9090
$this->assertInstanceOf(TestService::class, $locator->get('Existing'));

tests/TestCase/Service/ServicePaginatorTraitTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @since 1.0.0
1212
* @license https://opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
declare(strict_types = 1);
14+
declare(strict_types=1);
1515

1616
namespace Burzum\CakeServiceLayer\Service;
1717

@@ -31,7 +31,7 @@ class ServicePaginatorTraitTest extends TestCase
3131
* @var array
3232
*/
3333
public $fixtures = [
34-
'core.Articles'
34+
'core.Articles',
3535
];
3636

3737
/**

0 commit comments

Comments
 (0)