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
3814c5c3
Commit
3814c5c3
authored
Jan 05, 2014
by
Mark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improved fixture controller
parent
e050aeac
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
113 additions
and
5 deletions
+113
-5
FixtureController.php
framework/yii/console/controllers/FixtureController.php
+113
-5
No files found.
framework/yii/console/controllers/FixtureController.php
View file @
3814c5c3
...
@@ -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
;
...
@@ -55,6 +56,11 @@ class FixtureController extends Controller
...
@@ -55,6 +56,11 @@ 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.
*/
*/
public
$defaultAction
=
'apply'
;
public
$defaultAction
=
'apply'
;
...
@@ -110,14 +116,43 @@ class FixtureController extends Controller
...
@@ -110,14 +116,43 @@ 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
);
}
}
/**
/**
...
@@ -128,13 +163,34 @@ class FixtureController extends Controller
...
@@ -128,13 +163,34 @@ class FixtureController extends Controller
*/
*/
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
;
}
}
$transaction
=
Yii
::
$app
->
db
->
beginTransaction
();
try
{
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
false
)
->
execute
();
foreach
(
$tables
as
$table
)
{
foreach
(
$tables
as
$table
)
{
$this
->
getDbConnection
()
->
createCommand
()
->
truncateTable
(
$table
)
->
execute
();
$this
->
getDbConnection
()
->
createCommand
()
->
truncateTable
(
$table
)
->
execute
();
$this
->
stdout
(
"Table
\"
{
$table
}
\"
was successfully cleared.
\n
"
,
Console
::
FG_GREEN
);
$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 +236,18 @@ class FixtureController extends Controller
...
@@ -180,6 +236,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 +279,47 @@ class FixtureController extends Controller
...
@@ -211,7 +279,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