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
Show 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 @@
/**
* CreateController class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
...
...
@@ -12,18 +11,40 @@ namespace yii\console\controllers;
use
yii\console\Controller
;
use
yii\util\FileHelper
;
use
yii\base\Exception
;
/**
* This command creates an Yii Web application at the specified location.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class
CreateController
extends
Controller
{
const
EXIT_UNABLE_TO_LOCATE_SOURCE
=
1
;
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.
...
...
@@ -31,11 +52,11 @@ class CreateController extends Controller
* @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
* 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
*/
public
function
actionIndex
(
$path
,
$type
=
'default'
)
public
function
actionIndex
(
$path
)
{
$path
=
strtr
(
$path
,
'/\\'
,
DIRECTORY_SEPARATOR
);
if
(
strpos
(
$path
,
DIRECTORY_SEPARATOR
)
===
false
)
{
...
...
@@ -43,82 +64,104 @@ class CreateController extends Controller
}
$dir
=
rtrim
(
realpath
(
dirname
(
$path
)),
'\\/'
);
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
)
===
'.'
)
{
$this
->
_rootPath
=
$path
=
$dir
;
}
else
{
$this
->
_rootPath
=
$path
=
$dir
.
DIRECTORY_SEPARATOR
.
basename
(
$path
);
}
if
(
$this
->
confirm
(
"Create
\"
$type
\"
application under '
$path
'?"
))
{
$sourceDir
=
realpath
(
__DIR__
.
'/../create/'
.
$type
);
if
(
$sourceDir
===
false
)
{
echo
"
\n
Unable to locate the source directory for
\"
$type
\"
.
\n
"
;
return
self
::
EXIT_UNABLE_TO_LOCATE_SOURCE
;
}
if
(
$this
->
confirm
(
"Create
\"
$this->type
\"
application under '
$path
'?"
))
{
$sourceDir
=
$this
->
getSourceDir
();
$config
=
$this
->
getConfig
();
$list
=
FileHelper
::
buildFileList
(
$sourceDir
,
$path
);
$list
[
'index.php'
][
'callback'
]
=
array
(
$this
,
'generateIndex'
);
$list
[
'index-test.php'
][
'callback'
]
=
array
(
$this
,
'generateIndex'
);
$list
[
'protected/tests/bootstrap.php'
][
'callback'
]
=
array
(
$this
,
'generateTestBoostrap'
);
$list
[
'protected/yiic.php'
][
'callback'
]
=
array
(
$this
,
'generateYiic'
);
if
(
is_array
(
$config
))
{
foreach
(
$config
as
$file
=>
$settings
)
{
if
(
isset
(
$settings
[
'handler'
]))
{
$list
[
$file
][
'callback'
]
=
$settings
[
'handler'
];
}
}
}
FileHelper
::
copyFiles
(
$list
);
//@chmod($path.'/assets', 0777);
//@chmod($path.'/protected/runtime', 0777);
//@chmod($path.'/protected/yiic', 0755);
if
(
is_array
(
$config
))
{
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
"
;
}
}
/**
* Generates index.php file contents
*
* @param string $source path to index.php template
* @param array $params
*
* @return string final index.php file contents
* @throws \yii\base\Exception if source directory wasn't located
* @return string
*/
p
ublic
function
generateIndex
(
$source
,
$params
)
p
rotected
function
getSourceDir
(
)
{
$content
=
file_get_contents
(
$source
);
$yii
=
realpath
(
dirname
(
__FILE__
)
.
'/../../yii.php'
);
$yii
=
$this
->
getRelativePath
(
$yii
,
$this
->
_rootPath
.
DIRECTORY_SEPARATOR
.
'index.php'
);
$yii
=
str_replace
(
'\\'
,
'\\\\'
,
$yii
);
return
preg_replace
(
'/\$yii\s*=(.*?);/'
,
"
\$
yii=
$yii
;"
,
$content
);
$customSource
=
realpath
(
$this
->
templatesPath
.
'/'
.
$this
->
type
);
$defaultSource
=
realpath
(
$this
->
getDefaultTemplatesPath
()
.
'/'
.
$this
->
type
);
if
(
$customSource
)
{
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
*
* @param string $source path to index-test.php template
* @param array $params
*
* @return string final index-test.php file contents
* @return string default templates path
*/
p
ublic
function
generateTestBoostrap
(
$source
,
$params
)
p
rotected
function
getDefaultTemplatesPath
(
)
{
$content
=
file_get_contents
(
$source
);
$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
);
return
realpath
(
__DIR__
.
'/../create'
);
}
/**
* Generates yiic.php file contents
*
* @param string $source path to yiic.php template
* @param array $params
* @return array|null template configuration
*/
protected
function
getConfig
()
{
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
);
$yiic
=
realpath
(
dirname
(
__FILE__
)
.
'/../../yiic.php'
);
$yiic
=
$this
->
getRelativePath
(
$yiic
,
$this
->
_rootPath
.
DIRECTORY_SEPARATOR
.
'protected'
.
DIRECTORY_SEPARATOR
.
'yiic.php'
);
$yiic
=
str_replace
(
'\\'
,
'\\\\'
,
$yiic
);
return
preg_replace
(
'/\$yiic\s*=(.*?);/'
,
"
\$
yiic=
$yiic
;"
,
$content
);
$relativeFile
=
str_replace
(
$this
->
getSourceDir
(),
''
,
$source
);
$relativePath
=
$this
->
getRelativePath
(
$pathTo
,
$this
->
_rootPath
.
$relativeFile
);
$relativePath
=
str_replace
(
'\\'
,
'\\\\'
,
$relativePath
);
return
preg_replace
(
'/\$'
.
$varName
.
'\s*=(.*?);/'
,
"
\$
"
.
$varName
.
"=
$relativePath
;"
,
$content
);
}
/**
...
...
@@ -151,6 +194,6 @@ class CreateController extends Controller
$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
// 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