Skip to content

Commit b3838f7

Browse files
committed
init commit
1 parent b677e19 commit b3838f7

File tree

11 files changed

+1274
-0
lines changed

11 files changed

+1274
-0
lines changed

src/CommandCreator.php

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php
2+
3+
namespace DaverDalas\LaravelPostDeployCommands;
4+
5+
use Closure;
6+
use Illuminate\Support\Str;
7+
use InvalidArgumentException;
8+
use Illuminate\Filesystem\Filesystem;
9+
10+
class CommandCreator
11+
{
12+
/**
13+
* The filesystem instance.
14+
*
15+
* @var \Illuminate\Filesystem\Filesystem
16+
*/
17+
protected $files;
18+
19+
/**
20+
* The registered post create hooks.
21+
*
22+
* @var array
23+
*/
24+
protected $postCreate = [];
25+
26+
/**
27+
* Create a new migration creator instance.
28+
*
29+
* @param \Illuminate\Filesystem\Filesystem $files
30+
* @return void
31+
*/
32+
public function __construct(Filesystem $files)
33+
{
34+
$this->files = $files;
35+
}
36+
37+
/**
38+
* Create a new migration at the given path.
39+
*
40+
* @param string $name
41+
* @param string $path
42+
* @return string
43+
* @throws \Exception
44+
*/
45+
public function create($name, $path)
46+
{
47+
$this->ensureCommandDoesntAlreadyExist($name);
48+
49+
// First we will get the stub file for the migration, which serves as a type
50+
// of template for the migration. Once we have those we will populate the
51+
// various place-holders, save the file, and run the post create event.
52+
$stub = $this->getStub();
53+
54+
if(!$this->files->exists($path)){
55+
$this->files->makeDirectory($path);
56+
}
57+
58+
$this->files->put(
59+
$path = $this->getPath($name, $path),
60+
$this->populateStub($name, $stub)
61+
);
62+
63+
// Next, we will fire any hooks that are supposed to fire after a migration is
64+
// created. Once that is done we'll be ready to return the full path to the
65+
// migration file so it can be used however it's needed by the developer.
66+
$this->firePostCreateHooks();
67+
68+
return $path;
69+
}
70+
71+
/**
72+
* Ensure that a migration with the given name doesn't already exist.
73+
*
74+
* @param string $name
75+
* @return void
76+
*
77+
* @throws \InvalidArgumentException
78+
*/
79+
protected function ensureCommandDoesntAlreadyExist($name)
80+
{
81+
if (class_exists($className = $this->getClassName($name))) {
82+
throw new InvalidArgumentException("A {$className} class already exists.");
83+
}
84+
}
85+
86+
/**
87+
* Get the migration stub file.
88+
*
89+
* @return string
90+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
91+
*/
92+
protected function getStub()
93+
{
94+
return $this->files->get($this->stubPath() . '/blank.stub');
95+
}
96+
97+
/**
98+
* Populate the place-holders in the migration stub.
99+
*
100+
* @param string $name
101+
* @param string $stub
102+
* @return string
103+
*/
104+
protected function populateStub($name, $stub)
105+
{
106+
$stub = str_replace('DummyClass', $this->getClassName($name), $stub);
107+
108+
return $stub;
109+
}
110+
111+
/**
112+
* Get the class name of a migration name.
113+
*
114+
* @param string $name
115+
* @return string
116+
*/
117+
protected function getClassName($name)
118+
{
119+
return Str::studly($name);
120+
}
121+
122+
/**
123+
* Get the full path to the migration.
124+
*
125+
* @param string $name
126+
* @param string $path
127+
* @return string
128+
*/
129+
protected function getPath($name, $path)
130+
{
131+
return $path . '/' . $this->getDatePrefix() . '_' . $name . '.php';
132+
}
133+
134+
/**
135+
* Fire the registered post create hooks.
136+
*
137+
* @return void
138+
*/
139+
protected function firePostCreateHooks()
140+
{
141+
foreach ($this->postCreate as $callback) {
142+
call_user_func($callback);
143+
}
144+
}
145+
146+
/**
147+
* Register a post migration create hook.
148+
*
149+
* @param \Closure $callback
150+
* @return void
151+
*/
152+
public function afterCreate(Closure $callback)
153+
{
154+
$this->postCreate[] = $callback;
155+
}
156+
157+
/**
158+
* Get the date prefix for the migration.
159+
*
160+
* @return string
161+
*/
162+
protected function getDatePrefix()
163+
{
164+
return date('Y_m_d_His');
165+
}
166+
167+
/**
168+
* Get the path to the stubs.
169+
*
170+
* @return string
171+
*/
172+
public function stubPath()
173+
{
174+
return __DIR__ . '/stubs';
175+
}
176+
177+
/**
178+
* Get the filesystem instance.
179+
*
180+
* @return \Illuminate\Filesystem\Filesystem
181+
*/
182+
public function getFilesystem()
183+
{
184+
return $this->files;
185+
}
186+
}

0 commit comments

Comments
 (0)