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
78ab5c65
Commit
78ab5c65
authored
Dec 26, 2012
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added ability to create own "yiic create" templates
parent
d59f6c00
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
127 additions
and
55 deletions
+127
-55
CreateController.php
framework/console/controllers/CreateController.php
+99
-55
config.php
framework/console/create/config.php
+21
-0
index.php
framework/console/create/default/index.php
+7
-0
No files found.
framework/console/controllers/CreateController.php
View file @
78ab5c65
...
@@ -2,7 +2,6 @@
...
@@ -2,7 +2,6 @@
/**
/**
* CreateController class file.
* CreateController class file.
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 Yii Software LLC
* @copyright Copyright © 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @license http://www.yiiframework.com/license/
...
@@ -12,18 +11,40 @@ namespace yii\console\controllers;
...
@@ -12,18 +11,40 @@ namespace yii\console\controllers;
use
yii\console\Controller
;
use
yii\console\Controller
;
use
yii\util\FileHelper
;
use
yii\util\FileHelper
;
use
yii\base\Exception
;
/**
/**
* This command creates an Yii Web application at the specified location.
* This command creates an Yii Web application at the specified location.
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
* @since 2.0
*/
*/
class
CreateController
extends
Controller
class
CreateController
extends
Controller
{
{
const
EXIT_UNABLE_TO_LOCATE_SOURCE
=
1
;
private
$_rootPath
;
private
$_rootPath
;
private
$_config
;
/**
* @var string custom template path. If specified, templates will be
* searched there additionally to `framework/console/create`.
*/
public
$templatesPath
;
/**
* @var string application type. If not specified default application
* skeleton will be used.
*/
public
$type
=
'default'
;
public
function
init
()
{
parent
::
init
();
if
(
$this
->
templatesPath
&&
!
is_dir
(
$this
->
templatesPath
))
{
throw
new
Exception
(
'--templatesPath "'
.
$this
->
templatesPath
.
'" does not exist or can not be read.'
);
}
}
/**
/**
* Generates Yii application at the path specified via appPath parameter.
* Generates Yii application at the path specified via appPath parameter.
...
@@ -31,11 +52,11 @@ class CreateController extends Controller
...
@@ -31,11 +52,11 @@ class CreateController extends Controller
* @param string $path the directory where the new application will be created.
* @param string $path the directory where the new application will be created.
* If the directory does not exist, it will be created. After the application
* If the directory does not exist, it will be created. After the application
* is created, please make sure the directory has enough permissions.
* is created, please make sure the directory has enough permissions.
*
@param string $type application type. If not specified default application
*
*
skeleton will be used.
*
@throws \yii\base\Exception if path specified is not valid
* @return integer the exit status
* @return integer the exit status
*/
*/
public
function
actionIndex
(
$path
,
$type
=
'default'
)
public
function
actionIndex
(
$path
)
{
{
$path
=
strtr
(
$path
,
'/\\'
,
DIRECTORY_SEPARATOR
);
$path
=
strtr
(
$path
,
'/\\'
,
DIRECTORY_SEPARATOR
);
if
(
strpos
(
$path
,
DIRECTORY_SEPARATOR
)
===
false
)
{
if
(
strpos
(
$path
,
DIRECTORY_SEPARATOR
)
===
false
)
{
...
@@ -43,82 +64,104 @@ class CreateController extends Controller
...
@@ -43,82 +64,104 @@ class CreateController extends Controller
}
}
$dir
=
rtrim
(
realpath
(
dirname
(
$path
)),
'\\/'
);
$dir
=
rtrim
(
realpath
(
dirname
(
$path
)),
'\\/'
);
if
(
$dir
===
false
||
!
is_dir
(
$dir
))
{
if
(
$dir
===
false
||
!
is_dir
(
$dir
))
{
$this
->
usageError
(
"The directory '
$path
' is not valid. Please make sure the parent directory exists."
);
throw
new
Exception
(
"The directory '
$path
' is not valid. Please make sure the parent directory exists."
);
}
}
if
(
basename
(
$path
)
===
'.'
)
{
if
(
basename
(
$path
)
===
'.'
)
{
$this
->
_rootPath
=
$path
=
$dir
;
$this
->
_rootPath
=
$path
=
$dir
;
}
}
else
{
else
{
$this
->
_rootPath
=
$path
=
$dir
.
DIRECTORY_SEPARATOR
.
basename
(
$path
);
$this
->
_rootPath
=
$path
=
$dir
.
DIRECTORY_SEPARATOR
.
basename
(
$path
);
}
}
if
(
$this
->
confirm
(
"Create
\"
$type
\"
application under '
$path
'?"
))
{
$sourceDir
=
realpath
(
__DIR__
.
'/../create/'
.
$type
);
if
(
$this
->
confirm
(
"Create
\"
$this->type
\"
application under '
$path
'?"
))
{
if
(
$sourceDir
===
false
)
{
$sourceDir
=
$this
->
getSourceDir
();
echo
"
\n
Unable to locate the source directory for
\"
$type
\"
.
\n
"
;
$config
=
$this
->
getConfig
();
return
self
::
EXIT_UNABLE_TO_LOCATE_SOURCE
;
}
$list
=
FileHelper
::
buildFileList
(
$sourceDir
,
$path
);
$list
=
FileHelper
::
buildFileList
(
$sourceDir
,
$path
);
$list
[
'index.php'
][
'callback'
]
=
array
(
$this
,
'generateIndex'
);
$list
[
'index-test.php'
][
'callback'
]
=
array
(
$this
,
'generateIndex'
);
if
(
is_array
(
$config
))
{
$list
[
'protected/tests/bootstrap.php'
][
'callback'
]
=
array
(
$this
,
'generateTestBoostrap'
);
foreach
(
$config
as
$file
=>
$settings
)
{
$list
[
'protected/yiic.php'
][
'callback'
]
=
array
(
$this
,
'generateYiic'
);
if
(
isset
(
$settings
[
'handler'
]))
{
$list
[
$file
][
'callback'
]
=
$settings
[
'handler'
];
}
}
}
FileHelper
::
copyFiles
(
$list
);
FileHelper
::
copyFiles
(
$list
);
//@chmod($path.'/assets', 0777);
//@chmod($path.'/protected/runtime', 0777);
if
(
is_array
(
$config
))
{
//@chmod($path.'/protected/yiic', 0755);
foreach
(
$config
as
$file
=>
$settings
)
{
if
(
isset
(
$settings
[
'permissions'
]))
{
@
chmod
(
$path
.
'/'
.
$file
,
$settings
[
'permissions'
]);
}
}
}
echo
"
\n
Your application has been created successfully under
{
$path
}
.
\n
"
;
echo
"
\n
Your application has been created successfully under
{
$path
}
.
\n
"
;
}
}
}
}
/**
/**
* Generates index.php file contents
* @throws \yii\base\Exception if source directory wasn't located
*
* @return string
* @param string $source path to index.php template
* @param array $params
*
* @return string final index.php file contents
*/
*/
p
ublic
function
generateIndex
(
$source
,
$params
)
p
rotected
function
getSourceDir
(
)
{
{
$content
=
file_get_contents
(
$source
);
$customSource
=
realpath
(
$this
->
templatesPath
.
'/'
.
$this
->
type
);
$yii
=
realpath
(
dirname
(
__FILE__
)
.
'/../../yii.php'
);
$defaultSource
=
realpath
(
$this
->
getDefaultTemplatesPath
()
.
'/'
.
$this
->
type
);
$yii
=
$this
->
getRelativePath
(
$yii
,
$this
->
_rootPath
.
DIRECTORY_SEPARATOR
.
'index.php'
);
$yii
=
str_replace
(
'\\'
,
'\\\\'
,
$yii
);
if
(
$customSource
)
{
return
preg_replace
(
'/\$yii\s*=(.*?);/'
,
"
\$
yii=
$yii
;"
,
$content
);
return
$customSource
;
}
elseif
(
$defaultSource
)
{
return
$defaultSource
;
}
else
{
throw
new
Exception
(
'Unable to locate the source directory for "'
.
$this
->
type
.
'".'
);
}
}
}
/**
/**
* Generates index-test.php file contents
* @return string default templates path
*
* @param string $source path to index-test.php template
* @param array $params
*
* @return string final index-test.php file contents
*/
*/
p
ublic
function
generateTestBoostrap
(
$source
,
$params
)
p
rotected
function
getDefaultTemplatesPath
(
)
{
{
$content
=
file_get_contents
(
$source
);
return
realpath
(
__DIR__
.
'/../create'
);
$yii
=
realpath
(
dirname
(
__FILE__
)
.
'/../../yiit.php'
);
$yii
=
$this
->
getRelativePath
(
$yii
,
$this
->
_rootPath
.
DIRECTORY_SEPARATOR
.
'protected'
.
DIRECTORY_SEPARATOR
.
'tests'
.
DIRECTORY_SEPARATOR
.
'bootstrap.php'
);
$yii
=
str_replace
(
'\\'
,
'\\\\'
,
$yii
);
return
preg_replace
(
'/\$yiit\s*=(.*?);/'
,
"
\$
yiit=
$yii
;"
,
$content
);
}
}
/**
/**
* Generates yiic.php file contents
* @return array|null template configuration
*
*/
* @param string $source path to yiic.php template
protected
function
getConfig
()
* @param array $params
{
if
(
$this
->
_config
===
null
)
{
$this
->
_config
=
require
$this
->
getDefaultTemplatesPath
()
.
'/config.php'
;
if
(
$this
->
templatesPath
&&
file_exists
(
$this
->
templatesPath
))
{
$this
->
_config
=
array_merge
(
$this
->
_config
,
require
$this
->
templatesPath
.
'/config.php'
);
}
}
if
(
isset
(
$this
->
_config
[
$this
->
type
]))
{
return
$this
->
_config
[
$this
->
type
];
}
}
/**
* @param string $source path to source file
* @param string $pathTo path to file we want to get relative path for
* @param string $varName variable name w/o $ to replace value with relative path for
*
*
* @return string
final yiic.php file content
s
* @return string
target file contetn
s
*/
*/
public
function
generateYiic
(
$source
,
$params
)
public
function
replaceRelativePath
(
$source
,
$pathTo
,
$varName
)
{
{
$content
=
file_get_contents
(
$source
);
$content
=
file_get_contents
(
$source
);
$yiic
=
realpath
(
dirname
(
__FILE__
)
.
'/../../yiic.php'
);
$relativeFile
=
str_replace
(
$this
->
getSourceDir
(),
''
,
$source
);
$yiic
=
$this
->
getRelativePath
(
$yiic
,
$this
->
_rootPath
.
DIRECTORY_SEPARATOR
.
'protected'
.
DIRECTORY_SEPARATOR
.
'yiic.php'
);
$yiic
=
str_replace
(
'\\'
,
'\\\\'
,
$yiic
);
$relativePath
=
$this
->
getRelativePath
(
$pathTo
,
$this
->
_rootPath
.
$relativeFile
);
return
preg_replace
(
'/\$yiic\s*=(.*?);/'
,
"
\$
yiic=
$yiic
;"
,
$content
);
$relativePath
=
str_replace
(
'\\'
,
'\\\\'
,
$relativePath
);
return
preg_replace
(
'/\$'
.
$varName
.
'\s*=(.*?);/'
,
"
\$
"
.
$varName
.
"=
$relativePath
;"
,
$content
);
}
}
/**
/**
...
@@ -151,6 +194,6 @@ class CreateController extends Controller
...
@@ -151,6 +194,6 @@ class CreateController extends Controller
$up
.=
'/'
.
$segs1
[
$i
];
$up
.=
'/'
.
$segs1
[
$i
];
}
}
return
'
dirname(__FILE__)
.\''
.
$up
.
'/'
.
basename
(
$path1
)
.
'\''
;
return
'
__DIR__
.\''
.
$up
.
'/'
.
basename
(
$path1
)
.
'\''
;
}
}
}
}
\ No newline at end of file
framework/console/create/config.php
0 → 100644
View file @
78ab5c65
<?php
/** @var $controller \yii\console\controllers\CreateController */
$controller
=
$this
;
return
array
(
'default'
=>
array
(
'index.php'
=>
array
(
'handler'
=>
function
(
$source
)
use
(
$controller
)
{
return
$controller
->
replaceRelativePath
(
$source
,
realpath
(
YII_PATH
.
'/yii.php'
),
'yii'
);
},
'permissions'
=>
0777
,
),
'assets'
=>
array
(
'permissions'
=>
0777
,
),
'protected/runtime'
=>
array
(
'permissions'
=>
0755
,
),
),
);
\ No newline at end of file
framework/console/create/default/index.php
View file @
78ab5c65
<?php
<?php
// change the following paths if necessary
$yii
=
__DIR__
.
'/../framework/yii.php'
;
$config
=
__DIR__
.
'/protected/config/main.php'
;
require_once
(
$yii
);
Yii
::
createWebApplication
(
$config
)
->
run
();
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