Skip to content

Commit 06043f9

Browse files
committedJan 10, 2023
Use a CrontabAdapterInterface instead of the class to allow creation of new adapters
1 parent 9eba6bd commit 06043f9

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed
 

‎README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ $crontabJob->setEnabled(false);
155155
This will prepend your cron job with a `#` in your
156156
crontab when persisting it.
157157

158+
### Write your own adapter
159+
Additionally, if you cannot read another user's crontabs or if you are on a distributed architecture where crons are not
160+
run on the machine executing the jobs, you can create any other Adapter for your architecture
161+
by implementing the `CrontabAdapterInterface`.
162+
163+
You can then instantiate the `CrontabRepository` with your adapter.
164+
158165
Unit tests
159166
----------
160167

‎src/CrontabManager/CrontabAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* @author TiBeN
2626
*/
27-
class CrontabAdapter
27+
class CrontabAdapter implements CrontabAdapterInterface
2828
{
2929
private $userName;
3030

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2013 Benjamin Legendre
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace TiBeN\CrontabManager;
20+
21+
/**
22+
* Crontab adapter.
23+
* Retrieve and write cron jobs data using the "crontab" command line.
24+
*
25+
* @author TiBeN
26+
*/
27+
interface CrontabAdapterInterface
28+
{
29+
30+
/**
31+
* Read the crontab and return
32+
* raw data
33+
*
34+
* @return String $output the crontab raw data
35+
*/
36+
public function readCrontab();
37+
38+
/**
39+
* Write the raw crontab data to the crontab.
40+
*
41+
* @param String $crontabRawData
42+
*/
43+
public function writeCrontab($crontabRawData);
44+
}

‎src/CrontabManager/CrontabRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CrontabRepository
5555
*
5656
* @param CrontabAdapter $crontabAdapter
5757
*/
58-
public function __construct(CrontabAdapter $crontabAdapter)
58+
public function __construct(CrontabAdapterInterface $crontabAdapter)
5959
{
6060
$this->crontabAdapter = $crontabAdapter;
6161
$this->readCrontab();

0 commit comments

Comments
 (0)
Please sign in to comment.