Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F1050446
factory.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Jul 2, 10:05 AM
Size
12 KB
Mime Type
text/x-php
Expires
Fri, Jul 4, 10:05 AM (17 h, 8 m)
Engine
blob
Format
Raw Data
Handle
678700
Attached To
rINP In-Portal
factory.php
View Options
<?php
/**
* @version $Id: factory.php 16181 2015-05-15 16:54:20Z alex $
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
use
Intechnic\InPortal\Core\kernel\utility\ClassDiscovery\ClassMapBuilder
;
defined
(
'FULL_PATH'
)
or
die
(
'restricted access!'
);
class
kFactory
extends
kBase
implements
kiCacheable
{
const
TYPE_CLASS
=
1
;
const
TYPE_INTERFACE
=
2
;
const
TYPE_TRAIT
=
3
;
const
MODIFIER_ABSTRACT
=
1
;
const
MODIFIER_FINAL
=
2
;
/**
* Mapping between class name and file name
* where class definition can be found.
* File path absolute!
*
* @var Array
* @access protected
*/
protected
$classMap
=
Array
();
/**
* Class information.
*
* @var array
*/
protected
$classInfo
=
array
();
/**
* Information about class usage among other classes.
*
* @var array
*/
protected
$classTree
=
array
();
/**
* The PSR-4 compliant namespace-to-folder mapping.
*
* @var array
*/
protected
$namespaceMap
=
array
();
/**
* Map class names to their pseudo
* class names, e.g. key=pseudo_class,
* value=real_class_name
*
* @var Array
* @access protected
*/
protected
$realClasses
=
Array
();
/**
* Where all created objects are stored
*
* @var Array|kBase[]
* @access protected
*/
protected
$Storage
=
Array
();
public
function
__construct
()
{
parent
::
__construct
();
spl_autoload_register
(
array
(
$this
,
'autoload'
),
true
,
true
);
}
/**
* Configures module-based autoloader.
*
* @return void
*/
public
function
configureAutoloader
()
{
if
(
!
$this
->
Application
->
ModuleInfo
)
{
$error_msg
=
'Autoloader configuration can be only performed after module information is available'
;
throw
new
LogicException
(
$error_msg
);
}
$this
->
namespaceMap
=
array
();
foreach
(
$this
->
Application
->
ModuleInfo
as
$module_name
=>
$module_info
)
{
if
(
$module_name
==
'In-Portal'
)
{
continue
;
}
$this
->
namespaceMap
[
$module_info
[
'ClassNamespace'
]]
=
rtrim
(
$module_info
[
'Path'
],
'/'
);
}
if
(
defined
(
'IS_INSTALL'
)
&&
IS_INSTALL
)
{
// During installation process all modules, because unit configs from all modules are scanned too.
$class_map_builders
=
ClassMapBuilder
::
createBuilders
();
}
else
{
$class_map_builders
=
ClassMapBuilder
::
createBuilders
(
$this
->
Application
->
ModuleInfo
);
}
foreach
(
$class_map_builders
as
$class_map_builder
)
{
list
(
$class_map
,
$class_info
)
=
$class_map_builder
->
get
();
$class_names
=
array_keys
(
$class_map
);
$this
->
classMap
=
array_merge
(
$this
->
classMap
,
$class_map
);
$this
->
classInfo
=
array_merge
(
$this
->
classInfo
,
$class_info
);
$this
->
realClasses
=
array_merge
(
$this
->
realClasses
,
array_combine
(
$class_names
,
$class_names
));
foreach
(
$class_info
as
$class
=>
$class_data
)
{
if
(
isset
(
$class_data
[
'extends'
])
)
{
foreach
(
$class_data
[
'extends'
]
as
$extends_class
)
{
if
(
!
isset
(
$this
->
classTree
[
$extends_class
])
)
{
$this
->
classTree
[
$extends_class
]
=
array
();
}
$this
->
classTree
[
$extends_class
][]
=
$class
;
}
}
}
}
}
/**
* Sets data from cache to object
*
* @param Array $data
* @return void
* @access public
*/
public
function
setFromCache
(&
$data
)
{
$this
->
classMap
=
$data
[
'Factory.Files'
];
$this
->
classInfo
=
$data
[
'Factory.ClassInfo'
];
$this
->
classTree
=
$data
[
'Factory.ClassTree'
];
$this
->
namespaceMap
=
$data
[
'Factory.Namespaces'
];
$this
->
realClasses
=
$data
[
'Factory.realClasses'
];
}
/**
* Performs automatic loading of classes registered with the factory
*
* @param string $class
* @return bool|null
* @access public
*/
public
function
autoload
(
$class
)
{
$file
=
$this
->
findFile
(
$class
);
if
(
$file
)
{
kUtil
::
includeOnce
(
FULL_PATH
.
$file
);
return
true
;
}
return
null
;
}
/**
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
* @return string|bool The path if found, false otherwise
* @access protected
*/
protected
function
findFile
(
$class
)
{
if
(
$class
[
0
]
==
'
\\
'
)
{
$class
=
substr
(
$class
,
1
);
}
if
(
isset
(
$this
->
classMap
[
$class
])
)
{
return
$this
->
classMap
[
$class
];
}
$pos
=
strrpos
(
$class
,
'
\\
'
);
if
(
$pos
!==
false
)
{
// namespaced class name
$class_path
=
str_replace
(
'
\\
'
,
DIRECTORY_SEPARATOR
,
substr
(
$class
,
0
,
$pos
))
.
DIRECTORY_SEPARATOR
;
$class_name
=
substr
(
$class
,
$pos
+
1
);
}
else
{
// PEAR-like class name
$class_path
=
null
;
$class_name
=
$class
;
}
$class_path
.=
str_replace
(
'_'
,
DIRECTORY_SEPARATOR
,
$class_name
)
.
'.php'
;
foreach
(
$this
->
namespaceMap
as
$namespace_prefix
=>
$namespace_path
)
{
if
(
strpos
(
$class
,
$namespace_prefix
)
===
0
)
{
$test_class_path
=
str_replace
(
str_replace
(
'
\\
'
,
DIRECTORY_SEPARATOR
,
$namespace_prefix
),
$namespace_path
,
$class_path
);
if
(
file_exists
(
FULL_PATH
.
DIRECTORY_SEPARATOR
.
$test_class_path
)
)
{
return
DIRECTORY_SEPARATOR
.
$test_class_path
;
}
}
}
return
$this
->
classMap
[
$class
]
=
false
;
}
/**
* Gets object data for caching
*
* @return Array
* @access public
*/
public
function
getToCache
()
{
ksort
(
$this
->
classMap
);
ksort
(
$this
->
classInfo
);
ksort
(
$this
->
classTree
);
ksort
(
$this
->
namespaceMap
);
ksort
(
$this
->
realClasses
);
return
Array
(
'Factory.Files'
=>
$this
->
classMap
,
'Factory.ClassInfo'
=>
$this
->
classInfo
,
'Factory.ClassTree'
=>
$this
->
classTree
,
'Factory.Namespaces'
=>
$this
->
namespaceMap
,
'Factory.realClasses'
=>
$this
->
realClasses
,
);
}
/**
* Splits any mixing of prefix and
* special into correct ones
*
* @param string $prefix_special
* @return Array
* @access public
*/
public
function
processPrefix
(
$prefix_special
)
{
// l.pick, l, m.test_TagProcessor
//preg_match("/(.*)\.*(.*)(_*)(.*)/", $prefix_special, $regs);
//return Array('prefix'=>$regs[1].$regs[3].$regs[4], 'special'=>$regs[2]);
$tmp
=
explode
(
'_'
,
$prefix_special
,
2
);
$tmp
[
0
]
=
explode
(
'.'
,
$tmp
[
0
]);
$prefix
=
$tmp
[
0
][
0
];
$prefix_special
=
$prefix
;
// new1
if
(
isset
(
$tmp
[
1
])
)
{
$prefix
.=
'_'
.
$tmp
[
1
];
}
$special
=
isset
(
$tmp
[
0
][
1
])
?
$tmp
[
0
][
1
]
:
''
;
$prefix_special
.=
'.'
.
$special
;
// new2
return
Array
(
'prefix'
=>
$prefix
,
'special'
=>
$special
,
'prefix_special'
=>
$prefix_special
);
}
/**
* Returns object using params specified, creates it if is required.
*
* @param string $name Object name in factory.
* @param string $pseudo_class Pseudo class.
* @param Array $event_params Event params.
* @param Array $arguments Constructor arguments.
*
* @return kBase
*/
public
function
getObject
(
$name
,
$pseudo_class
=
''
,
$event_params
=
Array
(),
$arguments
=
Array
())
{
$name
=
rtrim
(
$name
,
'.'
);
if
(
isset
(
$this
->
Storage
[
$name
])
)
{
return
$this
->
Storage
[
$name
];
}
$ret
=
$this
->
processPrefix
(
$name
);
if
(
!
$pseudo_class
)
{
$pseudo_class
=
$ret
[
'prefix'
];
}
if
(
defined
(
'DEBUG_MODE'
)
&&
defined
(
'DBG_FACTORY'
)
&&
DBG_FACTORY
&&
$this
->
Application
->
isDebugMode
()
)
{
$this
->
Application
->
Debugger
->
appendHTML
(
'<b>Creating object:</b> Pseudo class: '
.
$pseudo_class
.
' Prefix: '
.
$name
);
$this
->
Application
->
Debugger
->
appendTrace
();
}
$this
->
Storage
[
$name
]
=
$this
->
makeClass
(
$pseudo_class
,
$arguments
);
$this
->
Storage
[
$name
]->
Init
(
$ret
[
'prefix'
],
$ret
[
'special'
]);
$this
->
Application
->
EventManager
->
runBuildEvent
(
$ret
[
'prefix_special'
],
$pseudo_class
,
$event_params
);
return
$this
->
Storage
[
$name
];
}
/**
* Removes object from storage, so next time it could be created from scratch
*
* @param string $name Object's name in the Storage
* @return void
* @access public
*/
public
function
DestroyObject
(
$name
)
{
unset
(
$this
->
Storage
[
$name
]);
}
/**
* Checks if object with prefix passes was already created in factory
*
* @param string $name object pseudo_class, prefix
* @return bool
* @access public
*/
public
function
hasObject
(
$name
)
{
return
isset
(
$this
->
Storage
[
$name
]);
}
/**
* Get's real class name for pseudo class, includes class file and creates class instance.
*
* Pattern: Factory Method
*
* @param string $pseudo_class Pseudo class.
* @param array $arguments Constructor arguments.
*
* @return kBase
* @throws kFactoryException When class not found.
*/
public
function
makeClass
(
$pseudo_class
,
$arguments
=
Array
())
{
if
(
!
isset
(
$this
->
realClasses
[
$pseudo_class
])
)
{
$error_msg
=
'RealClass not defined for "<strong>'
.
$pseudo_class
.
'</strong>" pseudo_class.'
;
$error_msg
.=
' Please use "<strong>in-portal classmap:rebuild</strong>" command to discover new classes.'
;
if
(
$this
->
Application
->
isInstalled
()
)
{
throw
new
kFactoryException
(
$error_msg
);
}
else
{
if
(
$this
->
Application
->
isDebugMode
()
)
{
$this
->
Application
->
Debugger
->
appendTrace
();
}
trigger_error
(
$error_msg
,
E_USER_WARNING
);
}
return
false
;
}
$real_class
=
$this
->
realClasses
[
$pseudo_class
];
$mem_before
=
memory_get_usage
();
$time_before
=
microtime
(
true
);
$arguments
=
(
array
)
$arguments
;
if
(
!
$arguments
)
{
$class
=
new
$real_class
();
}
else
{
$reflection
=
new
ReflectionClass
(
$real_class
);
$class
=
$reflection
->
newInstanceArgs
(
$arguments
);
}
if
(
defined
(
'DEBUG_MODE'
)
&&
DEBUG_MODE
&&
defined
(
'DBG_PROFILE_MEMORY'
)
&&
DBG_PROFILE_MEMORY
&&
$this
->
Application
->
isDebugMode
()
)
{
$mem_after
=
memory_get_usage
();
$time_after
=
microtime
(
true
);
$mem_used
=
$mem_after
-
$mem_before
;
$time_used
=
$time_after
-
$time_before
;
$this
->
Application
->
Debugger
->
appendHTML
(
'Factroy created <b>'
.
$real_class
.
'</b> - used '
.
round
(
$mem_used
/
1024
,
3
)
.
'Kb time: '
.
round
(
$time_used
,
5
));
$this
->
Application
->
Debugger
->
profilerAddTotal
(
'objects'
,
null
,
$mem_used
);
}
return
$class
;
}
/**
* Returns sub-classes of given ancestor class.
*
* @param string $ancestor_class Ancestor class.
* @param boolean $concrete_only Return only non-abstract classes.
*
* @return array
* @throws kFactoryException When ancestor class not found.
*/
public
function
getSubClasses
(
$ancestor_class
,
$concrete_only
=
true
)
{
if
(
!
isset
(
$this
->
classMap
[
$ancestor_class
])
)
{
throw
new
kFactoryException
(
'Class "<strong>'
.
$ancestor_class
.
'</strong>" is not registered in the Factory'
);
}
if
(
!
isset
(
$this
->
classTree
[
$ancestor_class
])
)
{
return
array
();
}
$all_sub_classes
=
array
();
foreach
(
$this
->
classTree
[
$ancestor_class
]
as
$sub_class
)
{
$real_sub_class
=
$this
->
realClasses
[
$sub_class
];
$all_sub_classes
[
$real_sub_class
]
=
$sub_class
;
$all_sub_classes
=
array_merge
(
$all_sub_classes
,
$this
->
getSubClasses
(
$sub_class
,
false
));
}
if
(
$concrete_only
)
{
$concrete_sub_classes
=
array
();
foreach
(
$all_sub_classes
as
$real_sub_class
=>
$sub_class
)
{
if
(
$this
->
classInfo
[
$sub_class
][
'type'
]
==
self
::
TYPE_CLASS
&&
!
$this
->
classHasModifier
(
$sub_class
,
self
::
MODIFIER_ABSTRACT
)
)
{
$concrete_sub_classes
[
$real_sub_class
]
=
$sub_class
;
}
}
return
$concrete_sub_classes
;
}
return
$all_sub_classes
;
}
/**
* Determines of class has modifier.
*
* @param string $class Class.
* @param integer $modifier Modifier.
*
* @return boolean
*/
protected
function
classHasModifier
(
$class
,
$modifier
)
{
return
(
$this
->
classInfo
[
$class
][
'modifiers'
]
&
$modifier
)
==
$modifier
;
}
/**
* Registers new class in the factory
*
* @param string $real_class Real name of class as in class declaration
* @param string $file Filename in what $real_class is declared
* @param string $pseudo_class Name under this class object will be accessed using getObject method
* @return void
* @access public
*/
public
function
registerClass
(
$real_class
,
$file
,
$pseudo_class
=
null
)
{
if
(
!
isset
(
$pseudo_class
)
)
{
$pseudo_class
=
$real_class
;
}
if
(
!
isset
(
$this
->
classMap
[
$real_class
])
)
{
$this
->
classMap
[
$real_class
]
=
preg_replace
(
'/^'
.
preg_quote
(
FULL_PATH
,
'/'
)
.
'/'
,
''
,
$file
,
1
);
}
$this
->
realClasses
[
$pseudo_class
]
=
$real_class
;
}
/**
* Unregisters existing class from factory
*
* @param string $real_class Real name of class as in class declaration
* @param string $pseudo_class Name under this class object is accessed using getObject method
* @return void
* @access public
*/
public
function
unregisterClass
(
$real_class
,
$pseudo_class
=
null
)
{
unset
(
$this
->
classMap
[
$real_class
]);
}
}
class
kFactoryException
extends
Exception
{
}
Event Timeline
Log In to Comment