Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
f8938a2b
Commit
f8938a2b
authored
Jan 05, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1793 from Ragazzo/fixture_controller_improved
improved fixture controller
parents
78977540
2eb91ace
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
112 additions
and
8 deletions
+112
-8
FixtureController.php
framework/yii/console/controllers/FixtureController.php
+112
-8
No files found.
framework/yii/console/controllers/FixtureController.php
View file @
f8938a2b
...
@@ -10,6 +10,7 @@ namespace yii\console\controllers;
...
@@ -10,6 +10,7 @@ namespace yii\console\controllers;
use
Yii
;
use
Yii
;
use
yii\console\Controller
;
use
yii\console\Controller
;
use
yii\console\Exception
;
use
yii\console\Exception
;
use
yii\helpers\FileHelper
;
use
yii\test\DbTestTrait
;
use
yii\test\DbTestTrait
;
use
yii\helpers\Console
;
use
yii\helpers\Console
;
...
@@ -53,6 +54,11 @@ use yii\helpers\Console;
...
@@ -53,6 +54,11 @@ use yii\helpers\Console;
class
FixtureController
extends
Controller
class
FixtureController
extends
Controller
{
{
use
DbTestTrait
;
use
DbTestTrait
;
/**
* type of fixture apply to database
*/
const
APPLY_ALL
=
'all'
;
/**
/**
* @var string controller default action ID.
* @var string controller default action ID.
...
@@ -110,14 +116,41 @@ class FixtureController extends Controller
...
@@ -110,14 +116,41 @@ class FixtureController extends Controller
throw
new
Exception
(
'Fixture manager is not configured properly. Please refer to official documentation for this purposes.'
);
throw
new
Exception
(
'Fixture manager is not configured properly. Please refer to official documentation for this purposes.'
);
}
}
if
(
!
$this
->
confirmApply
(
$fixtures
))
{
$foundFixtures
=
$this
->
findFixtures
(
$fixtures
);
if
(
!
$this
->
needToApplyAll
(
$fixtures
[
0
]))
{
$notFoundFixtures
=
array_diff
(
$fixtures
,
$foundFixtures
);
if
(
$notFoundFixtures
)
{
$this
->
notifyNotFound
(
$notFoundFixtures
);
}
}
if
(
!
$foundFixtures
)
{
throw
new
Exception
(
"No files were found by name:
\"
"
.
implode
(
', '
,
$fixtures
)
.
"
\"
.
\n
"
.
"Check that fixtures with these name exists, under fixtures path:
\n\"
"
.
Yii
::
getAlias
(
$this
->
fixturePath
)
.
"
\"
."
);
}
if
(
!
$this
->
confirmApply
(
$foundFixtures
))
{
return
;
return
;
}
}
$this
->
getFixtureManager
()
->
basePath
=
$this
->
fixturePath
;
$this
->
getFixtureManager
()
->
basePath
=
$this
->
fixturePath
;
$this
->
getFixtureManager
()
->
db
=
$this
->
db
;
$this
->
getFixtureManager
()
->
db
=
$this
->
db
;
$this
->
loadFixtures
(
$fixtures
);
$this
->
notifySuccess
(
$fixtures
);
$transaction
=
Yii
::
$app
->
db
->
beginTransaction
();
try
{
$this
->
loadFixtures
(
$foundFixtures
);
$transaction
->
commit
();
}
catch
(
\Exception
$e
)
{
$transaction
->
rollback
();
$this
->
stdout
(
"Exception occured, transaction rollback. Tables will be in same state.
\n
"
,
Console
::
BG_RED
);
throw
$e
;
}
$this
->
notifySuccess
(
$foundFixtures
);
}
}
/**
/**
...
@@ -127,14 +160,33 @@ class FixtureController extends Controller
...
@@ -127,14 +160,33 @@ class FixtureController extends Controller
* @param array|string $tables
* @param array|string $tables
*/
*/
public
function
actionClear
(
array
$tables
)
public
function
actionClear
(
array
$tables
)
{
{
if
(
$this
->
needToApplyAll
(
$tables
[
0
]))
{
$tables
=
$this
->
getDbConnection
()
->
schema
->
getTableNames
();
}
if
(
!
$this
->
confirmClear
(
$tables
))
{
if
(
!
$this
->
confirmClear
(
$tables
))
{
return
;
return
;
}
}
foreach
(
$tables
as
$table
)
{
$transaction
=
Yii
::
$app
->
db
->
beginTransaction
();
$this
->
getDbConnection
()
->
createCommand
()
->
truncateTable
(
$table
)
->
execute
();
$this
->
stdout
(
"Table
\"
{
$table
}
\"
was successfully cleared.
\n
"
,
Console
::
FG_GREEN
);
try
{
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
false
)
->
execute
();
foreach
(
$tables
as
$table
)
{
$this
->
getDbConnection
()
->
createCommand
()
->
truncateTable
(
$table
)
->
execute
();
$this
->
getDbConnection
()
->
createCommand
()
->
resetSequence
(
$table
)
->
execute
();
$this
->
stdout
(
" Table
\"
{
$table
}
\"
was successfully cleared.
\n
"
,
Console
::
FG_GREEN
);
}
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
true
)
->
execute
();
$transaction
->
commit
();
}
catch
(
\Exception
$e
)
{
$transaction
->
rollback
();
$this
->
stdout
(
"Exception occured, transaction rollback. Tables will be in same state.
\n
"
,
Console
::
BG_RED
);
throw
$e
;
}
}
}
}
...
@@ -180,6 +232,18 @@ class FixtureController extends Controller
...
@@ -180,6 +232,18 @@ class FixtureController extends Controller
}
}
/**
/**
* Notifies user that fixtures were not found under fixtures path.
* @param array $fixtures
*/
private
function
notifyNotFound
(
$fixtures
)
{
$this
->
stdout
(
"Some fixtures were not found under path:
\n
"
,
Console
::
BG_RED
);
$this
->
stdout
(
Yii
::
getAlias
(
$this
->
fixturePath
)
.
"
\n\n
"
,
Console
::
FG_GREEN
);
$this
->
outputList
(
$fixtures
);
$this
->
stdout
(
"
\n
"
);
}
/**
* Prompts user with confirmation if fixtures should be loaded.
* Prompts user with confirmation if fixtures should be loaded.
* @param array $fixtures
* @param array $fixtures
* @return boolean
* @return boolean
...
@@ -211,7 +275,47 @@ class FixtureController extends Controller
...
@@ -211,7 +275,47 @@ class FixtureController extends Controller
private
function
outputList
(
$data
)
private
function
outputList
(
$data
)
{
{
foreach
(
$data
as
$index
=>
$item
)
{
foreach
(
$data
as
$index
=>
$item
)
{
$this
->
stdout
((
$index
+
1
)
.
".
{
$item
}
\n
"
,
Console
::
FG_GREEN
);
$this
->
stdout
(
" "
.
(
$index
+
1
)
.
".
{
$item
}
\n
"
,
Console
::
FG_GREEN
);
}
}
}
}
/**
* Checks if needed to apply all fixtures.
* @param string $fixture
* @return bool
*/
public
function
needToApplyAll
(
$fixture
)
{
return
$fixture
==
self
::
APPLY_ALL
;
}
/**
* Returns array of found fixtures. These may differer from input parameter as not all fixtures may exists.
* @param array $fixtures
*/
private
function
findFixtures
(
array
$fixtures
)
{
$fixturesPath
=
Yii
::
getAlias
(
$this
->
fixturePath
);
$files
=
[];
if
(
$this
->
needToApplyAll
(
$fixtures
[
0
]))
{
$files
=
FileHelper
::
findFiles
(
$fixturesPath
,
[
'only'
=>
[
'.php'
]]);
}
else
{
$filesToSearch
=
[];
foreach
(
$fixtures
as
$fileName
)
{
$filesToSearch
[]
=
$fileName
.
'.php'
;
}
$files
=
FileHelper
::
findFiles
(
$fixturesPath
,
[
'only'
=>
$filesToSearch
]);
}
$foundFixtures
=
[];
foreach
(
$files
as
$fixture
)
{
$foundFixtures
[]
=
basename
(
$fixture
,
'.php'
);
}
return
$foundFixtures
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment